Jump to content
  • Advertisement
Sign in to follow this  
adam23

Rotating using ID3DXSprite

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

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


Link to post
Share on other sites
Advertisement
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 this post


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


Link to post
Share on other sites
Quote:
Original post by ChaosPhoenix
Here, 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.

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!