PDA

View Full Version : DX9 Create Rotation Matrix around pivot point


Jas001
06-14-2006, 09:57 AM
Im having some trouble rotating around a pivot point.

Would the process be to create the following Matrices and multiply them:
Offset1: Create a translation matrix based on pivot.Multiply(-1)
Offset2: Create a translation matrix based on pivot to move it back.
Create a rotate matrix by multiplying
(Offset1 * myrotation * Offset2) * existing rotation matrix

This sample code takes the original matrix, pivot point, and a vector change and creates a new matrix based on the above logic:
private Matrix GetRotateMatrix(Matrix inmat, Vector3 pivot, Vector3 change)
{
Matrix offset1 = Matrix.Identity;
Matrix offset2 = Matrix.Identity;
Matrix rot = Matrix.Identity;

// Setup offsets.
offset1.Translate(Vector3.Multiply(pivot, -1));
offset2.Translate(pivot);

//Setup Rotation matrix.
rot.RotateYawPitchRoll(change.X, change.Y, change.Z);

//Combine Matrices.
Matrix final = Matrix.Identity;
final.Multiply(offset1);
final.Multiply(rot);
final.Multiply(offset2);
final.Multiply(inmat);

return final;
}

It seems to work but I have problems with objects that don't have a pivot at the origin. ANy ideas?