• Advertisement
Sign in to follow this  

SDL sprite rotation and movement issue

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heyo, Im having an issue with moving a rotated sprite. If anyone is familiar with sdl there is an addon called sdl_graphics (sdl_gfx its sometimes labeled) that allows for rotating of surfaces. I am developing a 2d sdl engine with versatility in mind. One of the things Im adding is the ability to rotate images and move them along the rotated angle. Think space shooter where arrow keys turn you left or right (the rotation) and up and down move you forward and backward, but not up and down on screen, rather forward and backward in the direction you are facing (the rotation angle). Im using rotation 0 to mean up and sdl's rotation uses positive degrees as counter clockwise rotation. Now technically the rotation bit works, just a little jumpy and not really smooth, but if i use 1 degree increments it rotates too slowly. The main problem is with the movement. As far as what we know during runtime, we know the current position, and we know the rotation angle, as well as the movement speed (const float). When calculating the x and y offsets im using this float XDiff = abs(sin(Rotation%90))*MovementSpeed; float YDiff = abs(cos(Rotation%90))*MovementSpeed; then either adding or subtracting each value depending on which quadrant the angle is located in(+ + for south east, + - for north east, - - for north west, - + for south west...0,0 being top left corner of screen) heres the thing, when you get the angle and apply that method, the moventment isnt always in the right direction, you may be rotated at 30 degrees, but it would move you as tho you were at say 10 degrees, but if you hit 35 degrees you jump like you moving at 87 degrees. The ammount is is off, and the direction it is off is not a constant. any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
the C/C++ versions of cin and cos use radians, not degrees.

Share this post


Link to post
Share on other sites
wow thanks, that simplified my code for calculating new position from 20+ lines to only 2 lines :) THANKS!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement