Jump to content
  • Advertisement
Sign in to follow this  

[Matrices] Movement along direction vector

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, let's say I've got a situation like the following, where I have an object in 3d space, whose position and rotation is defined by a 4x4 matrix: I need to move this object accordingly to the direction it's facing, like illustrated here: What is the correct way to manipulate the matrix? Right now I am doing this with spaghetti code that certainly can't be the "elegant" solution. Thanks in advance, a.

Share this post

Link to post
Share on other sites
I don't know if the 4x4 transformation matrix is needed for that. That is what it is, right?

If you've got any way to represent the position of the object as a vector, just add the direction vector with it? That's what I'd do..

Mmmm.. spaghetti!

Share this post

Link to post
Share on other sites
If the translation vector is known, then a corresponding transformation matrix can be build of it. How this is done depends on whether you use column or else row vectors, and at which index the homogeneous co-ordinate is located. E.g. for column vectors and structure [ x y z w ], a translation by [ x y z ] will be given as

[ 1 0 0 x ]
[ 0 1 0 y ]
[ 0 0 1 z ]
[ 0 0 0 1 ]

To combine this translation matrix with a given transformation matrix, you'll need to multiply them. The correct way if using column vectors is
T * M
where T denotes the new translation and M the transformation matrix so far. While this is the correct mathematical way, some "spaghetti" code can speed up the product, i.e. you exploit the fact that you know that T denotes a translation, and hence know which scalars of the resulting matrix are affected.

Now, if you want to isolate the facing vector from the current transformation matrix, you can use a "pick" vector. Assuming that z is the (local) direction of viewing, then the pick vector would be
p := [ 0 0 1 0 ]T
when using the above example structure. Then the product
M * p
would pick out the 3rd column vector from the matrix. It would obviously be more efficient to read the vector out without performing a matrix vector product. But be aware that the picked vector will contain also some factor if scaling was performed. If so, you need a more elaborate way of so-called "matrix decomposition".

As usual, apply the transpose operation to above formulas if you deal with row vectors instead of column vectors. However, although there is a general way of doing things, exploiting a-priori knowledge can be used to speed up operations. Whether this weights more than elegant code is a matter of the use cases.

Share this post

Link to post
Share on other sites
Thanks for the detailed answers. Now it's my time to shed out more details so that you can better understand "where" I am now.. and what do I mean by spaghetti ;)

I am using Direct3d9, coming from OpenGL.
In the engine I am converting, I did the worst thing possible: 3d entities position and rotation where stored as euler angles. Thus my code to "move ahead" a certain entity was and IS this:

FastSinCos( rotation.x*(pi/180), cos_x, sin_x );
FastSinCos( rotation.y*(pi/180), cos_y, sin_y );
FastSinCos( rotation.z*(pi/180), cos_z, sin_z );


position.x:=position.x+G*Spin; <-- Spin is the amount of movement

My first rough porting to Matrix math, simply constructs a D3DMatrix out of the position[x,y,z] and rotation[x,y,z] triplets. So maybe I need to take a step back and store position/rotation differently from the beginning? If so, what do you suggest?

Thanks a lot.

Share this post

Link to post
Share on other sites
Well, if you have your transformation matrix, the orientation is probably stored like this:
................object position/translation (P)
X-> [ 1 0 0 x ]
Y-> [ 0 1 0 y ]
Z-> [ 0 0 1 z ]
....[ 0 0 0 1 ]

If X,Y,Z hold the orientation, and say, Y is your direction vector then, to move in that direction you'd do:

P = P + speed*Y // P={x,y,z} and Y={0,1,0}

Where 'speed' would be the scalar rate of displacement you want. I guess you can manipulate the matrix in the appropriate places (4th column here), as described in the previous post.

edit: some kind of formatting error(...)

[Edited by - glopen on November 18, 2008 5:58:19 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!