# Rotating a mesh around it's centre of a mesh

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

## Recommended Posts

Hi all, I'm a bit embarrassed to ask about this, because it shows how little of a grasp I have of basic geometry :) But no matter, basically I want to be able to rotate a mesh around it's centre (i.e. spinning), sounds simple to me but I've had real trouble. With the meshes I'm using (generated 3d text from D3DXCreateText) the origin in local coordinates seems to be (when viewed from the front) the bottom, back, left of the text. I'm calculating the coordinates for the actual centre of the mesh by averaging all x coordinates of the vertices, and using that as the x component for the centre vertex - and so on for y and z. What I want to do is translate the mesh so that when rotated instead of rotating around the origin, it rotates around it's own centre. E.g. instead of rotating around this point (see '.'): .Mesh I want it to rotate around this point (see '.'): Me.sh Now, to do this I've used the following code, position is the origin and centre is the centre of the mesh, moveToPosition is just a translation to where it should be in world space: float transX, transY, transZ; transX = abs(position.x-centre.x); transY = abs(position.y-centre.y); transZ = abs(position.z-centre.z); //translate to centre, rotate around x axis D3DXMatrixTranslation(&Tx, 0, transY*-1.0f, transZ*-1.0f); D3DXMatrixRotationX(&rX, rotation.x); D3DXMatrixTranslation(&Txb, 0, transY, transZ); //same for y axis D3DXMatrixTranslation(&Ty, transX*-1.0f, 0, transZ*-1.0f); D3DXMatrixRotationY(&rY, rotation.y); D3DXMatrixTranslation(&Tyb, transX, 0, transZ); //same for z axis D3DXMatrixTranslation(&Tz, transX*-1.0f, transY*-1.0f, 0); D3DXMatrixRotationZ(&rZ, rotation.z); D3DXMatrixTranslation(&Tzb, transX, transY, 0); C1 = Tx*rX*Txb; C2 = Ty*rY*Tyb; C3 = Tz*rZ*Tzb; C = C1*C2*C3*moveToPosition; device->SetTransform(D3DTS_WORLD, &C); this->mesh->DrawSubset(0); Now, it seems to me like that should work... but it doesn't, I get it rotating in big circles around some random point, completely not what I wanted. Can someone help me out? Cheers, Duncan

##### Share on other sites
From what it looks like to me you're translating everything in the opposite direction. Try taking out the trans * -1.0f and see where that puts it.

But yeah, if you're rotating around a random point, you can jsut find the point, and voodoo it by offsetting by the location of that point. To find the point, you can just average the locations at each of the 90 degree cardinal points.

• 18
• 29
• 11
• 21
• 16