Jump to content
  • Advertisement
Sign in to follow this  

2d rotation in DX9

This topic is 5004 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

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

Link to post
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)
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 this post

Link to post
Share on other sites

The matrix multiplication order should be


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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!