Game Development Community

How to make normal rotate?

by DieRich · in Torque 3D Beginner · 03/11/2011 (6:43 am) · 4 replies

hi all, I'm confusing about how to make a MatrixF object whose up-vector is VectorF(0, 0, 1) rotate to up-vector VectorF(0, 1, 0). I don't know how to get the rotation MatrixF to do this. any help will be very thankful.

#1
03/11/2011 (11:56 am)
If you just want to set the orientation to an explicit configuration, for example,

right = (1, 0, 0)
left = (0, 0, -1)
up = (0, 1, 0)

then you can simply set the tranformation matrix directly...

// Orientation matrix set to a -90 degree rotation about the
// x-axis which would produce the vectors above if applied to
// an identity matrix (default rotation)
static MatrixF orient(EulerF(-M_PI_F/2.0f, 0.0f, 0.0f));

// Save the current position temporarily
Point3F pos = MyObject->getPosition();

// Set the orientation (this clears the position)
MyObject->setTransform(orient);

// Put the position back
MyObject->setPosition(pos);

// Depending on what you are doing, you might need to force the server
// to update the client's orientation.
MyObject->inspectPostApply();

If you want something more general, for example, that rotates an object -90 degrees about the x-axis and doesn't care about the other axes...

// Orientation matrix set to a -90 degree rotation about the x-axis.
// Same matrix as above, but we won't use it as the transformation 
// matrix directly this time.
static MatrixF orient(EulerF(-M_PI_F/2.0f, 0.0f, 0.0f));

// Save the current position temporarily
Point3F pos = MyObject->getPosition();

// Get the world transformation matrix, M.  You can think of this as
// two matrices multiplied together.
// T: translation matrix
// R: rotation matrix
// In Torque, M = T * R
MatrixF Mat;
Mat = MyObject->getTransform();

// We just want the rotation matrix, so set translation part to default
Mat.setPosition(Point3F::Zero);

// Now Mat is the rotation matrix.  Form a new roation matrix by 
// rotating this one by our orientation matrix
Mat.mulL(orient);

// Now apply the new rotation back to the object.  Remember that we
// cleared the position above... this is purely rotation
MyObject->setTransform(Mat);

// Now put the position back
MyObject->setPosition(pos);

// Depending on what you are doing, you might need to force the server
// to update the client's orientation.
MyObject->inspectPostApply();

Hope that is helpful/understandable.
#2
03/12/2011 (4:26 am)
In addition to this information,when you rotate with M_PI/2 rotations it is better to set columns directly into your current transform.
The Up direction becomes the forward one.
The Forward direction becomes the negative up one.
You have to swap only unit vectors.

1.It works a lot faster.
2.You don't need a temporary storage.
3.You're not losing precision.
4.You don't have to worry about the matrix concatenation.
#3
03/12/2011 (6:47 am)
It works thanks all of you.
#4
03/12/2011 (5:52 pm)
Good stuff I never knew I did not know.