# How to rotate mesh by other point

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

## Recommended Posts

Hello, i'm having a little problem, I have mesh m_WheelMesh [pointer to mesh], i wan't to rotate this mesh by other point [mostly by its own center but not always...]

Here is how i draw my mesh
 gAppWizard.GetD3DDev()->SetMaterial( &m_WheelMeshMaterials ); gAppWizard.GetD3DDev()->SetTexture( 0, m_WheelTextures ); D3DXMatrixTranslation( &m_Translate, 0.0f, -30.0f, 0.0f ); D3DXMatrixRotationZ(&m_RotateZ, speed); gAppWizard.GetD3DDev()->SetTransform(D3DTS_WORLD, &m_RotateZ); m_WheelMesh->DrawSubset( i );

speed is static float and in each loop iteration I increment its value by 0.001f;

##### Share on other sites
To do this is very simple. All you have to do is first translate the mesh using a vector describing the difference between the meshes default center, to the point that you want to actually rotate around.

for example, you have a box, whose default center (the point (0, 0, 0) in local space) is actually the front, bottom, left corner of the box. You want the box to rotate around the boxes actual center, which we will pretend is (0.5, 0.5, 0.5) if the box is 1 cubic unit. First you will do a translation:

[color=#000000]D3DXMatrixTranslation[color=#666600]([color=#000000] [color=#666600]&[color=#000000]m_Translate[color=#666600],[color=#000000] [color=#006666]0.5f[color=#666600],[color=#000000] [color="#666600"]0.5[color=#006666]f[color=#666600],[color=#000000] [color=#006666]0.5f[color=#000000] [color=#666600]);

which will put the box's actual center at (0, 0, 0) in world space (since the rotation transformation will rotate around the point (0, 0, 0) in world space). Now that you have the boxes real center at the point (0, 0, 0) in world space, you can do the rotation:

[color=#000000]D3DXMatrixRotationZ[color=#666600](&[color=#000000]m_RotateZ[color=#666600],[color=#000000] speed[color=#666600]);

which will rotate the box around the point (0, 0, 0). Finally, it's very unlikely you will want this box to be placed at the very center of your world, so you will need to do another transformation to place the box where you want in the world, which i will assume is (0, -30, 0) because of this line:

[color=#000000]D3DXMatrixTranslation[color=#666600]([color=#000000] [color=#666600]&[color=#000000]m_Translate[color=#666600],[color=#000000] [color=#006666]0.0f[color=#666600],[color=#000000] [color=#666600]-[color=#006666]30.0f[color=#666600],[color=#000000] [color=#006666]0.0f[color=#000000] [color=#666600]);

remember the order that you do transformations matters.

hope that helps

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013538
×