Archived

This topic is now archived and is closed to further replies.

Krusty95

How to move an object in 3d space

Recommended Posts

Krusty95    122
I''m using DirectX 8 and I can load meshes from .X files, but I don''t know how to move them around in 3d space. I tried looking in the documentation, but I couldn''t find anything about it. I just want to be able to press, let''s say, up, on the keyboard, and have my object move forward like it''s going into the screen and getting smaller. Could someone show me how this is done. Please. Thanks. Krusty95

Share this post


Link to post
Share on other sites
zennehoy    122
I believe that is what the world matrix is for. Your .x object has its own origin, and this never changes in relation to the rest of the object. When drawing primitives, d3d translates all vertecies according to 2 (3 if you count projection) matricies. First, it uses the world matrix to convert the vertecies from object space to world space. Then, it translates the vertecies according to the view matrix, which moves the vertecies into the correct orientation as seen with the camera. Lastly, the projection matrix is used to convert the vertecies from view space to screen space, basically by converting 3d-coordinates into 2d which can be displayed on the screen.
So to move your object, basically what you want to do is move your object space into world space while translating it a certain amount. This can easily be done by setting the worldmatrix as a translation matrix, and then drawing the object just as you have been.
I am fairly new at this myself, so if I got anything blateantly(sp?) wrong above, please correct me. If you want more info, the sdk documentation is actually fairly decent on directx specific matricies. To learn more about matricies in general, check out the gamedev articles,
Zen

Share this post


Link to post
Share on other sites
CrazySwede    122
------------------------------------------------------
D3DXMATRIX matWorld;
D3DXMatrixTranslation( &matWorld, fX, fY, fZ );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

------------------------------------------------------

Then just render as usual, the mesh will now be at fX, fY, fZ.

And if you want to rotate it to:

------------------------------------------------------

D3DXMATRIX matWorld;
D3DXMATRIX matRotation;

D3DXMATRIX* D3DXMatrixRotationYawPitchRoll(&matRotation, fYaw, fPitch, fRoll );
D3DXMatrixTranslation( &matWorld, fX, fY, fZ );

D3DXMatrixMultiply( &matWorld, &matRotation, &matWorld); //Order important rotation*translation

g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

------------------------------------------------------

Hope that helps some...

Share this post


Link to post
Share on other sites
Krusty95    122
Just one more quick question. Is there an easy way to make my .X file move along it''s forward vector? I can rotate it and move it in any direction, but I don''t quite know how to just make it move it along it''s forward vector. Any help will be appreciated. Thanks.

Share this post


Link to post
Share on other sites
Shannon Barber    1681
You need to keep some sort data structure around that has the objects position and orientation in it.

A 3D point for the ''center'' and a matrix for the rotation will get you started. Whenever the object needs to turn you update its rotation matrix. When it comes time to render you apply that rotation matrix to the object before you move it according to its center.

If you have the rotation matrix for an object, then the up/left/forward vectors are defined as follows
  
v3Left = D3DXVECTOR3(matRot(0,0), matRot(1,0), matRot(2,0));
v3Up = D3DXVECTOR3(matRot(0,1), matRot(1,1), matRot(2,1));
v3Forward = D3DXVECTOR3(matRot(0,2), matRot(1,2), matRot(2,2));


Multiply the v3Forward vector by your movement rate and add to the objects center, this will move the object in the direct it''s facing.


Magmai Kai Holmlor
- The disgruntled & disillusioned

Share this post


Link to post
Share on other sites