2d rotation in DX9

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

Recommended Posts

Following a tutorial I found here, I wrote a class to display a textured quad that can be moved, scaled, and rotated. The problem is when I enter the angle for rotation (-1.0f to 1.0f) the texture seems to rotate around the center of the screen and not around its own center point. I looked around, but I can't seem to find out why. Here is the Drawing code:
void CTexture::Blit(IDirect3DTexture9 *texture, RECT *rDest, float rotate)
{
float X;
float Y;
D3DXMATRIX matTranslation;
D3DXMATRIX matScaling;
D3DXMATRIX matTransform;

//get coordinates
X = rDest->left - m_fHalfWidth;
Y = -rDest->top + m_fHalfHeight;

//setup translation and scaling matrices
D3DXMatrixScaling(&matScaling, (float)(rDest->right - rDest->left),
(float)(rDest->bottom - rDest->top), 1.0f);
D3DXMatrixTranslation(&matTranslation, X, Y, 0.0f);
matTransform = matScaling * matTranslation;

//check to see if quad is rotated
if(rotate)
{
D3DXMATRIX matRotate;
ZeroMemory(&matRotate, sizeof(D3DXMATRIX));

//create rotation matrix about the z-axis
D3DXMatrixRotationZ(&matRotate, rotate);

//multiply matrices together
matTransform *= matRotate;
}

m_pDevice->SetTransform(D3DTS_WORLD, &matTransform);
m_pDevice->SetTexture(0, texture);
m_pDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);

}


Share on other sites
You are rotating around the Z axis of world space. To rotate around another point in space either:

Translate to the origin then rotate than translate back to the point (multiply the mats together in that order)
OR
use a matrix generating function that allows you to specify a center point like AffineTransformation.

I do not recomend trying to accumulate your changes frame to frame in a matrix (thought it is no doubt possible) instead keep the position and rotation as vectors - modify those frame to frame use them to generate your matrix.

hope this helps.

Share on other sites
ok, I got it. Used the first method. Thanks for the help.

Share on other sites

The matrix multiplication order should be

scale*rotation*translation

If your quad is set up so that the quad centre is (0,0), u donn need to do any "translation to origin" before the rotation.

• 22
• 10
• 19
• 15
• 14