Jump to content
  • Advertisement
Sign in to follow this  
genericguy

C++ SDL: Draw at centre of surface after rotating

This topic is 3939 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

Hi, I'm in quite a predicament. I'm using a physics engine called Chipmunk (which is awesome, by the way), which has origin of objects at their centre. Unfortunately for me SDL has the origin at the left top corner. Now I rotate my SDL_Surfaces (with rotozoom), and then I'm not sure where the centre of the object is anymore (because surfaces are square)! In case you aren't following, I drew a picture: Red dot: object origin Blue rectangle: there for perspective Black Box: SDL_Surface size In those case, it seems simply drawing the surface at x-width, y-height would work, but will that work in every case? Thanks, Lucas

Share this post


Link to post
Share on other sites
Advertisement
Your new width and height are (if your object is in a box):

rotated_widht = cos(alpha) * original_width - sin(alpha) * original_height
rotated_height = cos(alpha) * original_height + sin(alpha) * original_width

(Take care about + and - )
Depending on where the center of the rotation is placed, you have to perform different translation. Assuming that the center of the rotation is the top left of the original object, you have to subtract

translation_x = (rotated_widht - original_width) / 2
translation_y = (rotated_height - original_height) / 2

from your original coordinates to keep the center of the object unchanged.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!