Public Group

# Sprite Rotation

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

## Recommended Posts

I currently have a small sprite that I am trying to rotate dynamically. I have look around on google and it doesn't seem like there are very many good solutions without switching to opengl or something. I am now using SDL to render my 2d game. Up to this point I have been able to get away with just making a sprite sheet with different directions. However, I want to be able to rotate to track a moving target to shoot at it so pre-drawing all to possible angles no longer seems like the right solution. Looking on Google it seems like one option is http://www.ferzkopp.net/joomla/content/view/19/14/ but it also look like a lot of people aren't recommending this because its slow or something. Im not really sure what I should do to fix this problem. It seems like a relatively common task in a game that i surprisingly haven't found a good answer to yet. Any suggestions would be greatly appreciated.

##### Share on other sites
Creating your own rotation code is not that hard: First, rotate the rectangle that contains the sprite and learn how rasterize it (loop over scan lines). Then learn how to look up a pixel in the original sprite, applying the inverse of the rotation. If you implement this in a low-level language (e.g., C or C++), it should be pretty fast.

##### Share on other sites
Im not really sure where to even begin with that so I decided to just try the sdl_gfx to see if I could just get something to run so I can make some progress. I can change it later. However I am having some problems implementing it. My class for image that is being rotated has a SDL_Surface* image that is storing the image that needs to be displayed. In my update() method i am trying to do the follow: SDL_Surface * rotation= rotozoomSurface(image, 10.0, 1, 1); image = rotation; SDL_FreeSurface(rotation);

I know that the problems is in the image=rotation line. I'm fairly certain it is because they are both pointers and thus point to the address in memory of rotation, which is then being assigned to image so they both now point to the same location. That location is then subsequently released. Later when I try to draw to image the memory has been released resulting in the heap corruption error that is thrown. I think that to solve this I need to set image equal to a copy of rotation. However, I do not know how to do that. I am still trying to figure pointers and how they work so if any of the above logic is wrong please let me know. Is this what I need to do/ how do I make a copy?

##### Share on other sites
I would ask why you are trying to free/copy the rotated image at this point?

Might I recommend the following instead:
 SDL_FreeSurface(image); image = rotation; 

What this code does is frees the original image and sets the image SDL_Surface pointer to the address of the rotated image. My only worry with this code is the loss of image quality over multiple rotations (I am not positive that image quality would be lost, but it seems reasonable to me). It might instead be better to have something more like this:

 if(oldRotation != newRotation) { if(displayImage != NULL && displayImage != original_Image) SDL_FreeSurface(displayImage); displayImage = rotozoomSurface(original_Image, newRotation, 1, 1); oldRotation = newRotation; } 

This code makes new rotation images based off the originally loaded image, and only does so when the rotation has changed.

##### Share on other sites
I was trying to free the rotated image so that I didn't end up with a lot of extra rotations in memory that I don't need. I just needed it to display and rotate the image. So at the end of the call I would only have one SDL_Surface to hold on to. At least that was what I was thinking.

I used the code that Zael suggested and it works, kind of. Adding 10 to newRotation everytime the update function is called causes the image to rotate in a circle as desired and expected. However, it wobbles like crazy and jumps back and forth as is spins as if it were vibrating on a sifter or something. Is this a problem with the rotozoomSurface() or the way I am using it. The images are all circles so ideal as the rotate their position in space should remain constant but the features on the circle move around.

 if(oldRotation != newRotation) { if(image != NULL && image != original_Image) SDL_FreeSurface(image); image = rotozoomSurface(original_Image, newRotation, 1.0, 1); oldRotation = newRotation; }

Also out of curiosity, based on the link provided by alvaro I see that the way to make a copy of SDL_Surface* is by using SDL_Surface *Srfc2 = SDL_ConvertSurface(Srfc1, Srfc1->format, Srfc1->flags); Does this mean there is no generic way to make a copy of an object that is pointed to by an object?

##### Share on other sites
bd36, I was going through this myself. I eventually got SDL_gfx to work, and I could rotate my sprite, but it was SLOOOOWWWWW. It uses software to rotate, and, I couldn't cope with it.

That's when I tried SFML. SFML is similar to SDL, except it sits on top of OpenGL, and all it's graphics API's are done in hardware, including rotations and scaling. It's Audio functions sit on top of OpenAL, so there's some really good audio API's available too.

Please take a peek at http://sfml-dev.org you'll be glad you did. (BTW, my old blog (in my sig) details a game I made using SFML, and I rotate all my sprites with that game).

• 22
• 10
• 19
• 14
• 14