Sign in to follow this  

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

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this