Jump to content
  • Advertisement
Sign in to follow this  
mrWodoo

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!