Jump to content
  • Advertisement

Archived

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

hammer_bg_

How to move meshes in d3d

This topic is 5212 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

Advertisement
quote:
Original post by hammer_bg_
so can someone explain me how to move a mesh - primitive box to be precise - in a d3d enviroment?

Take a look at Tutorial 3 in the DirectX SDK. It explains how to rotate a mesh but you can use the same idea to move a mesh around your world.

The basic idea is that you can use matrices to represent an objects position and orientation inside your 3D world. Say you wanted to rotate an object by 30 degrees along the X axis and put it at position (25,30,10) in your 3D world. The code to create a matrix to do that would look like this:

D3DXMATRIX matTranslation, matRotation, matFinal;
D3DXMatrixRotationX(&matRotation, (D3DX_PI/180.0f)*30.0f);
D3DXMatrixTranslation(&matTranslation, 25.0f, 30.0f, 10.0f);
D3DXMatrixMultiply(&matFinal, &matRotation, &matTranslation);

Now, the matrix "matFinal" is the matrix you need to set to render a mesh at position (25, 30, 10) and rotated by 30 degrees.

Hope this helps,
neneboricua

Share this post


Link to post
Share on other sites
For clarification on the above post:

Specifically, "matFinal" is the WORLD MATRIX that you need to set.

pDevice->SetTransform(D3DTS_WORLD,&matFinal);

Share this post


Link to post
Share on other sites
Yes, this will work when rendering anything, be it from standard vertex buffers to D3DXMesh objects (which is just a vertex and index buffer on the inside anyway).

You need to call SetTransform and then loop through all the subsets of your model calling DrawSubset to render them.

neneboricua

Share this post


Link to post
Share on other sites
Calculate the transform matrix for the object, settransform and render your object.

Repeat for other objects.

It''s standard to set the world transform for every object again, since most of the time you have moving objects.

Share this post


Link to post
Share on other sites
When you want to move something (ie mesh) do the following:

// 1) make a temp matrix


D3DMATRIX tmpmat;

// 2) set the temp matrix to identity


D3DXMATRIXIDENTITY(tmpmat);

// 3) do whatever translation


D3DXMATRIXTRANSLATION(tmpmat, mesh_x, mesh_y, mesh_z);

// 4) multiply the world and translation matrix


D3DXMATRIXMULTIPLY(matworld, matworld, tmpmat);

// 5) set the transform


D3DDEVICE->SetTransform(D3DTS_WORLD, matworld);

// 6) render your mesh


// 7) now you have to do everything in reverse...


D3DXMATRIXIDENTITY(tmpmat);
D3DXMATRIXTRANSLATION(tmpmat, -mesh_x, -mesh_y, -mesh_z);
D3DXMATRIXMULTIPLY(matworld, matworld, tmpmat);
D3DDEVICE->SetTransform(D3DTS_WORLD, matworld);

// 8) and now you're set to do the exact same thing for all the other meshes that you may have.


Granted, this is my way to do it and it works for me. There are many other ways (probably more efficient, too)

EDIT: someone beat me to the catch >_<


"Yes... VB6 is here to steal your minds fps. Very slowly."~Mushu
Call .optimize(you.sig )


[edited by - Mushu on June 9, 2004 5:08:23 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!