Jump to content

  • Log In with Google      Sign In   
  • Create Account


Sprite Rotation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 bd36   Members   -  Reputation: 118

Like
0Likes
Like

Posted 19 March 2012 - 08:32 PM

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.

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 11906

Like
0Likes
Like

Posted 19 March 2012 - 08:35 PM

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.

#3 bd36   Members   -  Reputation: 118

Like
0Likes
Like

Posted 19 March 2012 - 10:34 PM

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?

#4 Álvaro   Crossbones+   -  Reputation: 11906

Like
0Likes
Like

Posted 20 March 2012 - 07:17 AM

https://www.google.com/search?q=copy+SDL_Surface

#5 Zael   Members   -  Reputation: 154

Like
1Likes
Like

Posted 20 March 2012 - 09:22 AM

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.

#6 bd36   Members   -  Reputation: 118

Like
0Likes
Like

Posted 20 March 2012 - 09:48 AM

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?

#7 BeerNutts   Crossbones+   -  Reputation: 2566

Like
0Likes
Like

Posted 22 March 2012 - 03:06 PM

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).
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS