Rotate entity between 2 entities

Hi, I have a character whose bones I want to rotate between two other entities.

For example, in the image provided, there are two cubes, one is fixed on the shoulder and the other(the one on the elbow) is controlled by the player in 3 dimensions. I want the characters arm to rotate according to the player controlled cube.

So far, I have tried

a = cubes[1].getPosition().sub(cubes[2].getPosition());
ax = a.normalize().dot(pc.Vec3.LEFT);
ax1 = Math.acos(ax);
ax2 = ax1 * pc.math.RAD_TO_DEG;
ay = a.normalize().dot(pc.Vec3.UP);
ay1 = Math.acos(ay);
ay2 = ay1 * pc.math.RAD_TO_DEG;
az = a.normalize().dot(pc.Vec3.FORWARD);
az1 = Math.acos(az);
az2 = az1 * pc.math.RAD_TO_DEG;

But I’m assuming something’s wrong with my logic because by doing this, the character’s arm disappears.

You may have a NaN in your calculations which can screw up the transformation matrix.

For the problem, I generally create a rotation matrix and either do a lookAt (if the local forward axis of the shoulder node is down the arm) or rebuild the matrix with up/right/forward vectors depending on the local axes of the shoulder.

Thanks for the reply, actually the calculations were correct but it became NaN after I did setRotation(). Changing setRotation to setEulerAngles stopped the arm from disappearing but now it’s moving in unexpected ways. I even tried the following but it still moves unexpectedly:

quat = new pc.Quat();
quat.setFromEulerAngles(ax2, ay2, az2);

Example: ( The character’s arm should be horizontal)

Example Video:(Blue cube is my shoulder and red cube is my elbow)

Basically I’m trying to make an app where it estimates the user’s pose via the webcam and transfers that pose to a character in real time.

You can try it yourself:

So far I have only added movement to the character’s left hand.

Oh yes, setRotation expects a pc.Quat, not euler angles.

Without spending so time digging into this, the XYZ axes on shoulder joint may be different to what you are expecting . eg the forward axis of the shoulder may not be going down the arm

Also, negative Z is forward in PlayCanvas (it looks like the elbow is pointing in the opposite direction to where it should be?) so that could be an issue?

1 Like