# Solution to DirectX9 Rotate Sprites

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

## Recommended Posts

Hello,

After a very difficult time trying to deal with the whole "scaling rotating and translating a sprite around its center (the translation part being the difficult part) I thought I'd post my solution to the benefit of anyone who needs to do the same.

This old post: http://www.gamedev.net/topic/520506-sprite-rotation/ was helpful, but I still had a lot of work to do.

Code:

[source]D3DXMATRIX mat, matCenter, matRot, matScale, matTrans;

//center:
D3DXMatrixIdentity(&matCenter);
matCenter(3, 0) = -64;//x trans
matCenter(3, 1) = -64;//y trans
matCenter(3, 2) = 0;//z trans

//init
D3DXMatrixIdentity(&matScale);
matScale._11 = 1;
matScale._22 = 1;

//init
D3DXMatrixIdentity(&matRot);

D3DXMatrixIdentity(&matTrans);
matTrans(3, 0) = 400;//x trans
matTrans(3, 1) = 400;//y trans
matTrans(3, 2) = 0;//z trans

//init final matrix
D3DXMatrixIdentity(&mat);

//Multiply them together:
D3DXMatrixMultiply(&mat,&mat,&matCenter);//This line is the key, translating the image -half of its width and height makes all the other operations happen around it's center.
D3DXMatrixMultiply(&mat,&mat,&matRot);
D3DXMatrixMultiply(&mat,&mat,&matScale);
D3DXMatrixMultiply(&mat,&mat,&matTrans);

d3dspt->SetTransform(&mat);

d3dspt->Draw(texture, NULL, NULL, NULL, D3DCOLOR_ARGB(255,50, 255, 255));

//reset matrix so it doesn't modify other sprites:
D3DXMatrixIdentity(&mat);
//reset matrix so it doesn't modify other sprites:
d3dspt->SetTransform(&mat);[/source]

Hope this helps,

Crowbar