#### Archived

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

# How to move meshes in d3d

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

## Recommended Posts

so can someone explain me how to move a mesh - primitive box to be precise - in a d3d enviroment?

##### Share on other sites
what do you mean ''move''

translate the location?

##### Share on other sites
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 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 on other sites
do this method work with a ID3DXMesh ??
cous i didnt succeed in moving the mesh

##### 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 on other sites
okay but now when i have 2boxes they are both moving???????????
and i whant only 1 of them to move i.e. the player

##### 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 on other sites
When you want to move something (ie mesh) do the following:
// 1) make a temp matrixD3DMATRIX tmpmat;// 2) set the temp matrix to identityD3DXMATRIXIDENTITY(tmpmat);// 3) do whatever translationD3DXMATRIXTRANSLATION(tmpmat, mesh_x, mesh_y, mesh_z);// 4) multiply the world and translation matrixD3DXMATRIXMULTIPLY(matworld, matworld, tmpmat);// 5) set the transformD3DDEVICE->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]

1. 1
Rutin
70
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633422
• Total Posts
3011804
• ### Who's Online (See full list)

There are no registered users currently online

×