# [SOLVED] Finding travel vector from a matrix

This topic is 4543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

All the objects in my graphics engine have a matrix property that controls their orientation. When you press left/right/up/down the correct matrix multiplication is applied in order to position the ship how I want it. My problem now is figuring out the travel vector for the object. ie. it's at X,Y,Z in space and needs to move forward by .1f However, using only this orientation matrix (a rotation on axis matrix) how do I figure out which way is forwards? [Edited by - KnyghtMare on August 12, 2006 8:49:12 PM]

##### Share on other sites
Basically, you multiply an unrotated direction vector by the orientation matrix. That will give you the rotated direction. However, if the unrotated direction vector is one of the X, Y, Z, -X, -Y, or -Z axes, then you can simply extract the rotated vector from the orientation matrix.

Assuming row vectors (DirectX), if your unrotated direction is the X axis, then your rotated vector is the first row of the matrix (first 3 elements), Y axis is the second row, and Z axis is the third row. If your unrotated vector points along a negative axis, then you negate the result.

If you are using column vectors (OpenGL), then you would do the same thing, but extract columns instead of rows.

##### Share on other sites
So the first 3 elements of the matrix (M11, M12 and M13) are the axis on which the object is being rotated on?

And then M21, M22 and M23 and the, well, what are they exactly?

##### Share on other sites
Wouldn't it be easier to have per object
x_angle, y_angle and z_angle to define the orientation?

You can build orientation matrix by these values.

I do it this way :)

##### Share on other sites
MarkThrush, that sounds like you are begging for gimble lock.

I agree with JohnBolton though, just use the rotation matrix to multiply a unrotated vector.
I do this when I want to find the direction something is facing while using ODE,
since ODE keeps track of all the motion
for me, I just extract the rotation matrix, and find out what way it points by multiplying a new vector with it.

##### Share on other sites
Ha, if you want a first person camera then all you need
is two angles:

Horizontal and vertical to build the orientation matrix.

Could someone provide a universal code sample to
build rotation matrix if I want to rotate my object

float X by x-axis
float Y by y-axis
float Z by z-axis

> Can it be so hard just to rotate your head ?

##### Share on other sites
I moved away from using x,y,z rotation axis seperately just recently. The reason being that it wasn't effecient for the control method I was employing for my objects.

I have since cracked this problem.

// Define the offset matrix
// This defines where the shape is relative to the ship object
Microsoft.DirectX.Matrix offsetMatrix = Microsoft.DirectX.Matrix.Translation(0, 45, 0);

// Build the entire matrix
// Basically we build our offset matrix into the ships position and orientation matrix
Microsoft.DirectX.Matrix shapeMatrix = offsetMatrix * spaceShip.Matrix * Microsoft.DirectX.Matrix.Translation(spaceShip.Position.X, spaceShip.Position.Y, spaceShip.Position.Z);

// Take our X,Y and Z co-ords from the matrix
shape.Position = new Position(shapeMatrix.M41, shapeMatrix.M42, shapeMatrix.M43);

[Edited by - KnyghtMare on August 12, 2006 8:00:37 PM]

##### Share on other sites
Quote:
 Original post by KnyghtMareSo the first 3 elements of the matrix (M11, M12 and M13) are the axis on which the object is being rotated on?And then M21, M22 and M23 and the, well, what are they exactly?

No, the first row is the rotated X axis, the second row is the rotated Y axis, and the third row is the rotated Z axis. If the ship travels in the direction of the X axis when it is not rotated, then it will travel in the direction of the rotated X axis when it is.

Quote:
 Original post by MarkThrushCould someone provide a universal code sample tobuild rotation matrix if I want to rotate my objectfloat X by x-axisfloat Y by y-axisfloat Z by z-axis

[google] Look up "rotation matrix" and you will get hundreds of hits describing how to set up a matrix to rotate around the X, Y, or Z axes.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634103
• Total Posts
3015539
×