I want to know how to move something "forward" in 3d space? If it has a direction vector and a position vector how do I accomplish this?

Thanks

Started by arrogantgod, Sep 13 1999 08:53 AM

2 replies to this topic

Sponsor:

Posted 13 September 1999 - 07:57 AM

This is accomplished through matrix multiplication. Every object in the scene will have its own transformation matrix. To move or rotate an object relative to its current position you have to create a second matrix that contains the relative movement/rotation. Then they are combined by multiplying the two matrices. The resulting matrix is the objects new transformation matrix this is what will be used to render the object.

For instance to move an object using the keyboard by a set vector distance:

//Get Keyboard Input

if(GetAsyncKeyState(VK_RIGHT))

object_matrix = MatrixMult(CreateTranslationMatrix(D3DVECTOR(-STEP,0.0,0.0)),object_matrix);

if(GetAsyncKeyState(VK_LEFT))

object_matrix = MatrixMult(CreateTranslationMatrix(D3DVECTOR(STEP,0.0,0.0)),object_matrix);

if(GetAsyncKeyState(VK_UP))

object_matrix = MatrixMult(CreateTranslationMatrix(D3DVECTOR(0.0,0.0,-STEP)),object_matrix);

if(GetAsyncKeyState(VK_DOWN))

object_matrix = MatrixMult(CreateTranslationMatrix(D3DVECTOR(0.0,0.0,STEP)),object_matrix);

The above code uses two functions:

MatrixMult - multiplys the two matrices together and returns the result.

CreateTranslationMatrix - creates a translation (position) matrix based on the given vector.

The above code would be a hell of a lot faster if flags were set if keys are pressed and then only call the matrixmult/createtranslationmatrix functions once with the final vector.

Also keep in mind that matrix multiplication is not commutative(sp?). It means that the order in which the multiplication takes place matters.

M1*M2 != M2*M1

This makes sense because if you turn right and take ten steps you will be in a different place than if you had taken 10 steps then turned right.

So basically you need to understand a little linear algebra. Also, Depending on if you are in a left or right hand coordinate system the construction of the matrix is a little different.

There is some matrix information in the Direct3D documentation or if you are using OpenGL take a look at chapter three in the Red Book [http://fly.cc.fer.hr/~unreal/theredbook/]

Good Luck

Oh yeah, If you have the DirectX SDK then do a search for 'D3DMath' it has all the functions you would need.