# How to change a rotation matrix from its existing rotation to another?

## Recommended Posts

Weng    122
I would like to change a game object's rotation matrix to match the rotation of another game object. Currently, I am able to retrieve the rotation matrices of the 2 game objects. How should I change the rotation matrix of an object to enable it to have the same orientation as the other game object? The rotation changes will be along the x or z axis and the rotation matrix is a 3x3 matrix.

##### Share on other sites
Hodgman    51323
RotationA = RotationB;

##### Share on other sites
Weng    122
Yes I used this but what if the rotation effect needs to be gradual?

i.e. A camera view gradually rotating from 1 view to another view.

##### Share on other sites
Hodgman    51323
Ah, that's less obvious ;)

You can convert 3x3 rotation matrices into quaternions, then slerp between the two quaternion-rotations, then convert the result back into a 3x3 matrix.

##### Share on other sites
haegarr    7372
By interpolation using a weight that increases from 0 to 1 in the time you want to go from orientation A to orientation B.

That said, generally interpolating rotation matrices can be done but isn't a straight forward job. This is usually the time where quaternions should be uncased from the programmer's toolbox. So convert both orientation matrices to quaternions, interpolate the quaternions, convert the time current quaternion back into a matrix, and use that. (Just a tip: Don't try to understand quaternions as something other than a mathematical way of describing rotations.) EDIT: Yep, Hodgman already said it, and he's right :) /EDIT

However, if your rotations are really restricted to 1 common axis at a time (cited from the OP: "will be along the x or z axis") then you can also use the dot-product to get the needed rotation angle. Then interpolate the angle as mentioned above, and use a usual rotation matrix for the given axis and angle to compute the time current rotation matrix. (Not even axis/angle rotation conversion would be needed.)