Sprite Rotation? (DirectX 9 C++)

Started by
3 comments, last by Crazyfool 15 years, 5 months ago
How is sprite rotation accomplished in DirectX c++? Also, how can it be done on a sprite origin for a pivot?
Advertisement
If by DirectX you mean Direct3D, it depends how you are drawing sprites.

If you are using ID3DXSprite, you set a transform with ID3DXSprite::SetTransform prior to calling ID3DXSprite::Draw which allows you to specify the centre point.

If you are using an orthagonal projection, you can define your sprite vertices in local space around the centre, then multiply a rotation by a transformation to create the matrix to set the world transform before your render.

If you are using textured quads and pretransformed vertices, you need to manually rotate the vertices yourself with sin() and cos() prior to drawing.

If by DirectX you mean DirectDraw, hardware support for sprite rotating is limited. There are methods to do this but you need to check at runtime to see if it is supported. If not, your only option would be to rotate and draw the pixels manually by locking the buffer, which would be slow.
This is my code to rotating projectiles:

D3DXMATRIX matRotate;D3DXVECTOR2 vCenter(16.0f, 16.0f);D3DXVECTOR2 vPosition(position.x, position.y);D3DXMatrixTransformation2D(&matRotate, NULL, NULL, NULL, &vCenter, angle, &vPosition);d3dspt->SetTransform(&matRotate);d3dspt->Draw(artManager.getProjectile(iter->artId)->texture, NULL, NULL, NULL, fullColor);		D3DXMatrixRotationZ(&matRotate, 0);d3dspt->SetTransform(&matRotate);


Position points to a vector of the location in screen coordinates of where you want the texture to be drawn at (after rotation), and put into vPosition

vCenter is the center of the texture (size is 32x32)

angle is a float for the angle in radians.

d3dspt is my sprite object.
Many thanks. I'll try and get some results.
Glad to help. I had a LOT of problems with getting textures to rotate with the sprite interface.

This topic is closed to new replies.

Advertisement