# Rotating using ID3DXSprite

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

## Recommended Posts

I have been looking for the answer to this question, and I can't find it anywhere. I am creating a small game using the ID3DXSprite interface and I can get the sprite to rotate except it rotates relative to it's top left point. How can I get it to rotate based on it's center position?
void Ship::Update(float elapsed)
{
m_force = m_accel * m_mass;
m_pos.x += (m_dir.x * m_force * elapsed);
m_pos.y += (m_dir.y * m_force * elapsed);

D3DXMATRIX rot, ptrans;
D3DXMatrixRotationYawPitchRoll(&rot, 0.0f, 0.0f, m_ZRotation);
D3DXMatrixTranslation(&ptrans, m_pos.x, m_pos.y, 0.0f);

m_world = rot * ptrans;
}



##### Share on other sites
Hi...
you could :
- translate it so that your center position is (0,0,0) : basically, for 2D sprites, would be applying a translation of (-sizeX/2, -sizeY/2)
- THEN rotate it

if i'm not wrong, this way it should be rotated around it's center.

the way to do this is :
- build a translation matrix (-sizeX/2, -sizeY/2, 0)
- build a rotation matrix (like the D3DXMatrixRotationYawPitchRoll(&rot, 0.0f, 0.0f, m_ZRotation); you provided)
- multiply them together... don't ask me in which order, I'm quite useless at this and generally have to try both ways to know in which order every time :p but order is important. Get the result of the mupltiplication in a matrix.
- next you can build your "real" translation matrix ( like D3DXMatrixTranslation(&ptrans, m_pos.x, m_pos.y, 0.0f); ) and muliply it with the previous result (yes order is important again. No I don't know in which order again :p)
- finally call ID3DXSprite::SetTransform() with the second resulting matrix

now you can call ID3DXSprite::Draw()

##### Share on other sites
There is a specific 2D rotation function that allows you to specify the center of the rotation. I believe it's called D3DMatrixTransformation2D and it takes in a variety of transformation vectors (translation, scale, rotation, rotational center, etc.) and outputs a matrix with all the transformations combined. Then all you do is simply call SetTransform() with your sprite class, draw your sprite, and you're done.

##### Share on other sites
Here, I found a great tutorial for you. It discusses what I talked about earlier.

http://www.toymaker.info/Games/html/sprites.html

##### Share on other sites
Quote:
 Original post by ChaosPhoenixHere, I found a great tutorial for you. It discusses what I talked about earlier.http://www.toymaker.info/Games/html/sprites.html

Perfect, that is exactly what I was looking for. Thanks a lot.

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633269
• Total Posts
3011145
• ### Who's Online (See full list)

There are no registered users currently online

×