Hello!
(I’m a novice when it comes to graphics programming, so please bear with me. I’m a pretty experienced web dev, but my only brush with 3d graphics is a basics course in university over 5 years ago.)
I’m building a sort of a Legoman type of character, whose legs rotate forward and backward, with their pivot points close to the waist/torso.
This image shows how the Legoman’s legs should rotate:
I’d like to control the rotation of the legs programmatically, because I want them to move in relation to the waist/torso ONLY if I press a key (as opposed to them flapping around whenever they hit something). I also want to enable physics for them, i.e., they should collide with other meshes, and when they hit something they should transmit the impulse/force to the rest of the Legoman, if that makes sense.
My approach was to create a PhysicsBody with MotionType “DYNAMIC” for the legs, and connect them with a 6dof constraint to the waist/torso. I was then hoping to control the rotation with leg.physicsBody.transformNode.setTargetTransform()
, e.g., I’m expecting the following code to add 1 degree of rotation to the leg:
const transformNode = leg.physicsBody.transformNode;
const position = transformNode.position;
const rotation = transformNode.rotationQuaternion;
const rotationToAdd = BABYLON.Quaternion.RotationYawPitchRoll(new BABYLON.Vector3(0, Math.PI / 180, 0)); // This might be the wrong way to add 1 degree of pitch to a quaternion, but I've tried multiple other ways too
const targetRotation = rotation.multiply(rotationToAdd);
leg.physicsBody.setTargetTransform(position, targetRotation);
However, instead of moving the leg by adding only one degree to the rotation, the leg starts spinning around for a while, as if the target rotation was something more. I’m thinking it could be caused by me messing up the quaternion computations (I don’t really remember how they should work), but I’ve tried multiple different variations and nothing seems to fix it. Therefore, I’m also considering that maybe there’s a bug in the PhysicsBody.setTargetTransform()
method when you use it for setting rotation?
I’ve created a small playground to demonstrate the issue (kinda like the Legoman’s hip socket, and the leg rotating around it): https://playground.babylonjs.com/#LFP8R2#2 . Upon pressing a
, the box should rotate around the sphere by 1 degree (or if you hold the a
key pressed, it should slowly rotate until you lift the key). However, it just keeps on spinning for quite a while.
Please let me know if I’m doing something wrong here, and how I should fix it. Feel free to suggest alternative ways to design the legs too, I’m definitely not very sold on my own approach!