# 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);
ay = a.normalize().dot(pc.Vec3.UP);
ay1 = Math.acos(ay);
az = a.normalize().dot(pc.Vec3.FORWARD);
az1 = Math.acos(az);
ybot.findByName('mixamorig:LeftArm').setRotation(ax2,ay2,az2);
``````

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);
ybot.findByName('mixamorig:LeftArm').setRotation(quat);
``````

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: https://github.com/WaelShaikh/PlayPose
https://waelshaikh.github.io/PlayPose/

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