Jump to content
  • Advertisement
Sign in to follow this  
Portishead

How to rotate a square by an angle? [Image]

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

I know a point can be rotated if its position is (0, 0), with this method: x = cos(angle)*x - sin(angle)*y y = cos(angle)*x + sin(angle)*y But what if the point I want to rotate is not at (0, 0), and how do I apply the rotation on a whole square that starts at a certain point? (For example: (3, 0), (3, 1), (2, 1), (2, 0)) Thanks for any helpers, Portishead.

Share this post


Link to post
Share on other sites
Advertisement
First translate the points such that the point of rotation is at (0, 0). For example, if you want to rotate around the point (2, 3) you would subtract 2 from each of the x-coordinates and 3 from each of the y coordinates. Then apply the rotation to each of the points you want to rotate and then add back in the coordinates of the center point.

Share this post


Link to post
Share on other sites
When you said each of the x\y cordinates, you meant each one of the square?

If this is the square: (3, 0) , (3, 1) , (2, 1) , (2, 0)
That would be after the subtraction: (0, 0) , (0, 1) , (-1, 1) , (-1, 0) ? Or you meant somthing different?

[Sorry for being noob, haven't learned that in math yet]

Share this post


Link to post
Share on other sites
Correct, and the proper terms would be: move the object pivot point to the origin, rotate about the origin, move the object pivot point back to where it was.
In your case the pivot point of the object is likely to be the center. So you'll move the center to the object, rotate, and move it back.

Share this post


Link to post
Share on other sites
Heya portishead(awesome band btw)

But-t-t,I think the correct equation for rotation in 2-D is

X = (cos(angle) * Radius) + XOffset
Y = (sin(angle) * Radius) + YOffset

Be careful with the angle though in most languages they use "Radians" so you may have to convert the angle from "Degrees" to "Radians".

I'm sure you actually knew this I just wanted to compliment you on your user-name.

Share this post


Link to post
Share on other sites
Yeah, it's an awesome band, wish they would come to my country. :)

I found the equation on google, so I think it's the right one..

bzroom:
So I need to do the rotation equation on the center of the square?

That's the square after the subtraction: (Subtract X: 3, Subtract Y: 0)
(0, 0) , (0, 1) , (-1, 1) , (-1, 0)
So the center is:
(-0.5, 0.5)
After the equation: (Rotating by 90 degrees)
(0 - 1*(0.5), 1*(0.5)) => (-0.5, 0.5) //The same? Oo
What's the next step now?

Share this post


Link to post
Share on other sites
You have to 'bring' the point about which you want the rotation to happen to the origin.
Then Apply the rotation and finally 'undo' your first translation.

In homogenous coordinates we would have:



Where:

(Px, Py) is the point to rotate about
(x, y) is the point you want transformed
theta is the angle of rotation
(Rx, Ry) is the point rotated

If you do the Matrix multiplications you will find a formula for Rx and Ry.

Share this post


Link to post
Share on other sites
x=x*cos(angle)-y*sin(angle)
y=x*sin(angle)+x*cos(angle)

im pretty sure thats right... so thats

float newX=(x-originX)*cos(angle)-(y-originY)*sin(angle)+x;
float newY=(x-originX)*sin(angle)+(y-originY)*cos(angle)+y;

just off the top of my head...

Share this post


Link to post
Share on other sites
I use complex numbers to remember the formula for 2D rotation. Multiplying by exp(angle*i) does the rotation around the origin.

(x+y*i)*exp(angle*i) = (x+yi)*(cos(angle)+sin(angle)*i) = (x*cos(angle)-y*sin(angle) + (y*cos(angle)+x*sin(angle))*i = new_x+new_y*i

So your transformation rule is:

new_x = x*cos(angle)-y*sin(angle)
new_y = y*cos(angle)+x*sin(angle)

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!