🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Clarification

Started by
1 comment, last by arrogantgod 24 years, 9 months ago
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.


"You know you're obsessed with computer graphics when you're outside and you look up at the trees and think, "Wow! That's spectacular resolution!""
Advertisement
Ok, I must have been smoking something wierd when I posted the message.

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

Wow!

Thanks!

That is what I needed to know. Now I understand fully. So, I just need to multiply the new matrix by the current one to get the result I need!

Much Thanks!!!!!!!

This topic is closed to new replies.

Advertisement