Jump to content

  • Log In with Google      Sign In   
  • Create Account

Crowbar

Member Since 31 Jan 2013
Offline Last Active Mar 17 2013 09:27 PM

#5027756 Solution to DirectX9 Rotate Sprites

Posted by Crowbar on 31 January 2013 - 11:28 PM

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:

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);

 

Hope this helps,

 

Crowbar




PARTNERS