Jump to content
  • Advertisement
Sign in to follow this  

Calculating coordinates after rotating

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

Hello,

I am trying to figure out how to calculate the new coordinates of a rectangle which has been rotated about its center. I know that I am supposed to be using the following equations:


//sx, sy is the coordinate of the rectangle before rotation
float new_xpos = (cos(angle) * sx) + (sin(angle) * sy);
float new_ypos = (cos(angle) * sy) - (sin(angle) * sx);



From what I understand, these equations assume that Y increases upward and that you are rotating about the origin. I was hoping someone could help me apply these equations to the screen coordinate system. More specifically, this is what I'm working with:

A 44x110 pixel rectangle which is bottom centered in an 800x600 pixel window.
It's coordinates are (378, 490) and I need to know the coordinates of the top center of that rectangle after rotating it about its center by some arbitrary angle. So I would like to know where coordinate (400, 490) will be located after rotation.

I'm really stuck on this and any help will be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
The formula you posted rotates a point around the origin. How do you rotate around any other point? Well, move the whole thing so the origin is where you want to rotate, rotate, then move everything back.

If you wrap the x and y coordinates in a class and you overload operators + and -, you can have code like this that does what you want:

  Point new_point = center + rotate(old_point - center, angle);


If you don't want to use a class, you can unwrap the operations and write equivalent code using floats only, but it will be harder to read and harder to get right.

Share this post


Link to post
Share on other sites
Thanks for your reply but I'm not quite what you meant. This is what I've been trying to do:

Move the rectangle so that it is centered on the origin (0, 0). So the top left corner of the rectangle is (-22, 55). Rotate the rectangle by some angle and then using the equations above I can calculate the new coordinates and move the rectangle back to its original position. This works great on paper using the standard math coordinate system but it doesn't quite work for the screen's coordinate system.

I have read on other forums that you have to switch things around a bit in the equations in order to compensate for the fact that the Y-axis increases downard instead of upward.

My results:
sx = -22; //378 - 400
sy = -55; //490 - 545

NewX = ((cos(a) * sx) + (sin(a) * sy)) + 378;
NewY = ((cos(a) * sy) - (sin(a) * sx)) + 490;

For a = 45, NewX, NewY should be (438 +- 1, 501 +- 1) but I get (319, 479)

Any ideas?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!