# Rotating a 2d sprite...

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

## Recommended Posts

I'm working on developing a small 2d game, more as an excercise and learning experience than anything else, and I was trying to figure out if there's a way to take a 2d sprite and rotate it, so that if it were top down it would be able to face in any direction? I'm sure there is, but I'm having some difficulty finding any resources. Any help or suggestions would be greately appreciated.

##### Share on other sites
Depends on what API you're using to draw with really. If you're using a 2d api then it might have this built in, if not you'll have to generate a rotated version yourself and blit that to screen. (Although this is usually less than optimal and you might be better off just making a bunch of rotated versions in photoshop and switching between them).

If you're using a 3d api then you can rotate the vertices with a bit of trig (draw it out on paper and you should be able to figure out the right maths for each vertex) and just let texture mapping sort the rest out.

##### Share on other sites
Using an API that supports rotation would be the preferred method.

If you want to perform the rotations yourself, the key to most image warping and transformation is this: you have a (mostly continuous) function whose input is the location of a source pixel, and whose output is the location of the destination. What you really want to do is use the inverse of that function. For each pixel center in the resulting image, use that inverse function to find the center of the pixel that would end up there (using some sort of sampling, since you'll end up at some fractional value between pixels).

For rotations, if you work out the transformation using a rotation matrix and a translation, the math simplifies to just adding a few constants each iteration of a loop that scans over the pixels in each row of the image.

##### Share on other sites
Thanks guys, sorry about not mentioning the API, I was originially thinking of using DirectX, mainly just because it's what I've worked with mostly. The main reason I wanted the sprite to rotate is because I want to have the ship turning from a top down position, and use a thruster type control scheme. Sounds like I have some math to work out.

##### Share on other sites
I recommend you use the ID3DXSprite interface. It will take care of all the tedious housekeeping associated with sprites and give you a considerable amount of flexibility. Moreover, by way of ID3DXSprite::SetTransform, the mathematics can be kept to a minimum, as D3DX's matrix helper functions (in particular, D3DXMatrixRotation*) will do all the arithmetic for you.

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633311
• Total Posts
3011312
• ### Who's Online (See full list)

There are no registered users currently online

×