How do you rotate individual primitives?
I was wondering how you rotated primitives invidually. I have found a tutorial which shows how to do it, but It doesn't really explain the logic behind it very well. Can anyone help me? I know that it's done by changing the world matrix, but I don't know how and why you can affect it in such a way that It will rotate shapes about their own origins, and not (0,0,0).
It's the order of the transformations that is important. Whenever you apply a rotation transformation it is around the world origin. To rotate something on its own y axis, for example, you place it at the origin, rotate it, then translate it out to where it should be.
For example:
D3DXMATRIXA16 WorldMatrix;
D3DXMATRIXA16 Rotation, Translation;
D3DXMatrixRotation(&Rotation, ... );
D3DXMatrixTranslation(&Translation, ... );
// The following order of multiplying the transforms will orbit the geometry around the origin at a distance:
D3DXMatrixIdentity(&WorldMatrix);
D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &Translation);
D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &Rotation);
// The following will rotate the geometry around its origin:
D3DXMatrixIdentity(&WorldMatrix);
D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &Rotation);
D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &Translation);
If you wanted to rotate an arbitrary triangle in a mesh about its origin you would have to work out where it's origin is (you could pick a vertex or average the vertex positions for example). You would then apply a translation matrix that 'moves' that triangles vertices to the origin, apply a rotation and then translate them back out again.
Hope that makes sense,
For example:
D3DXMATRIXA16 WorldMatrix;
D3DXMATRIXA16 Rotation, Translation;
D3DXMatrixRotation(&Rotation, ... );
D3DXMatrixTranslation(&Translation, ... );
// The following order of multiplying the transforms will orbit the geometry around the origin at a distance:
D3DXMatrixIdentity(&WorldMatrix);
D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &Translation);
D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &Rotation);
// The following will rotate the geometry around its origin:
D3DXMatrixIdentity(&WorldMatrix);
D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &Rotation);
D3DXMatrixMultiply(&WorldMatrix, &WorldMatrix, &Translation);
If you wanted to rotate an arbitrary triangle in a mesh about its origin you would have to work out where it's origin is (you could pick a vertex or average the vertex positions for example). You would then apply a translation matrix that 'moves' that triangles vertices to the origin, apply a rotation and then translate them back out again.
Hope that makes sense,
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement