This example shows how to rotate around the local x, y, and z axes of a mesh (body). Click in the canvas, and press 'x', 'y' or 'z' to rotate around body axes. Hold 'shift' to rotate around world axes.
After computing the angle of rotation, dTheta
, the following code rotates the model:
quat.setFromAxisAngle(axis,dTheta);
if (shiftOn) // world axes
mesh.quaternion.multiplyQuaternions(quat,mesh.quaternion);
else // body axes
mesh.quaternion.multiply(quat);
The first step is to convert rotation expressed as an angle around a particular axis (which for this application is always a unit vector) to a quaternion. This is accomplished by the quaternion method setFromAxisAngle
.
Rotation in the body (model) frame is accomplished by multiplying the quaternion representing the model's prior orientation with the rotation quaternion. Rotation in the parent frame is performed by multiplying the same two quaternions, but in the opposite order. Note in simple examples the parent frame is the world frame, but this need not be the case.
The model's quaternion represents the rotation necessary to go from the model frame to its parent's frame. If the additional rotation is added such that it is applied first, then the rotation happens in the model frame. If the additional rotation happens second, the conversion to the parent frame happens first, so the rotation is in the parent frame.