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;
}
Rotating using ID3DXSprite
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?
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()
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()
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.
Here, I found a great tutorial for you. It discusses what I talked about earlier.
http://www.toymaker.info/Games/html/sprites.html
http://www.toymaker.info/Games/html/sprites.html
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement