• 10
• 12
• 12
• 14
• 15

# Rotating objects in Direct3D

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

## Recommended Posts

Hi

Even though I'm fully aware of how to basically rotate an object in Direct3D I am currently having a problem rotating the object about its centre.

By default, Direct3D rotates from the corner of the object I'm wanting to rotate, how do I change it so Direct3D rotates about the objects centre?

Any help appreciated.

Andrew

##### Share on other sites
Rotations are always around the origin. You call it rotated from the corner because it so happens in your case that a corner sits at the origin.

If you want to rotate about an arbitrary point, you need to translate the model so that the point sits at the origin. Then rotate. Then translate back.

##### Share on other sites
An alternative is to first ensure that your models are all centered about the origin in local space. I don't have any suggestions on how to accomplish that however.

##### Share on other sites
As nobodynews said:
Quote:
 An alternative is to first ensure that your models are all centered about the origin in local space.

if it is not:
use D3DXComputeBoundingSphere to obtain mesh center
then translate object like:
D3DXMatrixTranslation(&transMat, -center.x, -center.y, -center.z);

then rotate.

Or simply center it in your modeling program.

##### Share on other sites
Could anyone provide a code example of this? I'm not sure how to do any of that in code.

I have tried what has been suggested but it keeps messing up or just rotating around the same point again :(

I have a model for a character in a bounding box, it rotates from the corner of the object and not the very centre of it, how do I go about rotating it from the centre of the mesh object?

Any code examples I might be able to go off or a tutorial or some sort?

Thank you.

##### Share on other sites
Yes, the problem is that my game character rotates around a point rather than on the point itself, how do I change it so it rotates around the centre of the game character?

##### Share on other sites
Ok. try something like this:
// in some init functionLPD3DXMESH mesh;//your character mesh previously loadedD3DXVECTOR3 center;float radius;D3DXVECTOR3* pPos;mesh->LockVertexBuffer(0, (LPVOID*)&pPos);D3DXComputeBoundingSphere(pPos, mesh->GetNumVertices(),mesh->GetNumBytesPerVertex(), ¢er, &radius);mesh->UnlockVertexBuffer();...// in your render functionfloat elapsedTime = getElapsedTime();//some timer functionfloat rotSpeed = 10.0f;D3DXMATRIX transMat, rotMat, worldMat;D3DXMatrixTranslation(&transMat, -center.x, -center.y, -center.z);D3DXMatrixRotationY(&rotMat, elapsedTime * rotSpeed);worldMat = transMat * rotMat;...dev->SetMatrix(D3DTS_WORLD, &worldMat);mesh->DrawSubset(0);

but you should recenter your mesh in some modeling program!

EDIT: i have made some mistakes in code it is fixed now

##### Share on other sites
is your "game character" some kind of humanoid model?
if so it might be that (local center) pivot point is just below its feet?
on witch axis are you rotating it?
it would make sense to rotate it only on Y axis!

##### Share on other sites
I have the same problem.

I want to ratate the object at model space,not the world space.I have tried some,like D3DXVec3TransformCoord and D3DXMatrixRotationAxis,but it doesn't work.

I don't want to D3DXMatrixIdentity then D3DXMatrixRotationX then D3DXMatrixTranslation(God save me).

Is there other easier way to do that?

##### Share on other sites
I have studied a little with OpenGL and I do not know if you have to stack and combine the same way but I would say check that first. Make sure you are stacking your matrices correctly, especially your translations or else your object will not rotate correctly. It is a little backwards with OpenGL....

(Translation back to point) * (XRotation) * (YRotation) * (ZRotation) * (Translation to origin)

I don't know if this will help, but I hope it gives you some insight.