I'm trying to develop a ragdoll from a skinned mesh, in three.js using cannon.js as physics engine. I'm struggling with a lookAt problem for bones. Let me explain the method I used.

I made a skinned humanoid mesh with a skeleton consisting of "chains" of bones for spine and limbs. 1

I parse the model skeleton, for every bone I define a Cannon body, let's call it a cube, I assign it the same position of the bone (first joint), and constrain it to the previous bone, at fixed distance.

Then if i animate the simulation, the cubes are correctly simulated, and the rotations are not constrained, so they move freely like they were a chain of cubes suspended to a wire.

enter image description here

To the first bones of every chain, i assign the position of the corresponding cube, so the bones chain are aligned to the simulation.

Then, for every chain, i cycle through every bone and lookAt the next one, with this function:

rotateTowardsTarget = (source, target) => {

    const up = source.up.clone(); // it's 0, 1, 0

    const p1 = source.getWorldPosition(new THREE.Vector3());
    const p2 = target.getWorldPosition(new THREE.Vector3());

    const direction = new THREE.Vector3().subVectors(p2, p1).normalize();
    const align_q = new THREE.Quaternion().setFromUnitVectors( up, direction );
    // subtract rotation of the parent
    if (source.parent) {
        const _m1 = new THREE.Matrix4();
        const _q1 = new THREE.Quaternion();
        _m1.extractRotation( source.parent.matrixWorld );
        align_q.premultiply( _q1.invert() );


this makes the bones correctly align towards the following one, but then i go through something related to the lookAt methods, that keeping the object aligned to the UP vector, makes it "spin" when pointing at north and south poles, so when their target is up or down.


I'd like a way to rotate the bones towards the following one, keeping their initial orientation, or maybe limiting the rotation so they don't "spin".

It would be the same for a flight simulator: if the camera looksAt a target that's right down or up, we don't want it to "spin", just keep rotating and even invert (upside down)

thanks in advance.

do you have any suggestions?



You must log in to answer this question.

Browse other questions tagged .