# Rotating a sprite?

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

## Recommended Posts

I'm using the ID3DXSprite interface to control my sprites. Is there any tutorials, advice etc. on how to rotate such sprites?

##### Share on other sites
You can actually use Direct3D or OpenGL for 2d. Not only is this a lot faster, but you also get rotation, scaling, blending, alpha, etc for free. I don't see any strong reasons to continue using DirectDraw.

##### Share on other sites
ID3DXSprite is not DirectDraw.

I believe it also supports attaching a transformation matrix

##### Share on other sites
Oops, I completely misread that :P Sorry.

##### Share on other sites
I guess it's the transformation matrix that is confusing me.

##### Share on other sites
There should be something in there that creates a rotation matrix. Then you can apply that matrix to the sprite.

##### Share on other sites
Ah, ID3DXSprite::SetTransform() is what I'm looking for.

HRESULT SetTransform(
CONST D3DXMATRIX * pTransform
);

Describing the matrix is confusing, though. What do these variables mean?

typedef struct D3DXMATRIX {
FLOAT _11, FLOAT _12, FLOAT _13, FLOAT _14,
FLOAT _21, FLOAT _22, FLOAT _23, FLOAT _24,
FLOAT _31, FLOAT _32, FLOAT _33, FLOAT _34,
FLOAT _41, FLOAT _42, FLOAT _43, FLOAT _44 );
} D3DXMATRIX;

##### Share on other sites
That's just a 4x4 matrix of floats. The names of the variables ( _11, _23, etc ) are just indications of the positions - _11 means location 1,1 _23 means location 2,3 - or, row 2, column 3.

I'm pretty sure there's a function in D3DX that creates a rotation matrix for you, given the angle.

##### Share on other sites
Thanks for the thoughts, I'll look into that.

##### Share on other sites
Wikipedia has a couple articles about transformation matrices. I found them quite helpful, but they might be too generic for what you're doing.

##### Share on other sites
Here is a nice function for you

bool Blit(LPD3DXSPRITE pSprite, LPDIRECT3DTEXTURE9 pTexture, float DestX, float DestY, int SrcX, int SrcY, int Width, int Height, float XScale, float YScale, float rotation, D3DCOLOR Color){    // make sure a valid sprite was passed  if( pSprite == NULL )  	return false;   // make sure a valid texture was passed  if( m_Texture == NULL )    return false;  // create the rect that will copy from the texture  RECT Rect;  Rect.left = SrcX;  Rect.top  = SrcY;  Rect.right = Rect.left + Width;  Rect.bottom = Rect.top + Height;  	    // create the transformation  D3DXMATRIX  mat;  D3DXMatrixTransformation2D( &mat, NULL, 0.0f, &D3DXVECTOR2( XScale, YScale ), NULL, rotation, NULL );  pSprite->SetTransform(&mat); // draw it if(FAILED(pSprite->Draw(m_Texture, &Rect ,NULL ,&D3DXVECTOR3( DestX, DestY, 0.0f ),Color)))    return false;    return true; }

##### Share on other sites

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

## Create an account

Register a new account