Sign in to follow this  
Crowbar

Solution to DirectX9 Rotate Sprites

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);
D3DXMatrixRotationZ(&matRot, D3DXToRadian(180.0f));

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this