Sign in to follow this  

Simple rotation

This topic is 3490 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'm having problems wrapping my head around why this produces wrong results. I have 4 corners of a 2d rectangle:
Vec2 bottom_left(-10.0, -10.0);
Vec2 bottom_right(10.0, -10.0);
Vec2 top_left(-10.0, 10.0);
Vec2 top_right(10.0, 10.0);



I want to rotate the rectangle say 35 degrees. So i do this for every corner: Vec2.rotate(35.0); rotate() looks like (I convert to radians):
x = std::cos(radians)*x - std::sin(radians)*y;
y = std::sin(radians)*x + std::cos(radians)*y;



But the result I'm getting is distorted: As you can see the corner angles isn't 90 degrees anymore and well, that's my question :)

Share this post


Link to post
Share on other sites
Try the following.

You know that right now the corners are X1-4. You can say that the corners are on a circle, with a radius of 10*sqrt(2) (the distance between the corners to (0,0)).
The current angles of each one of the corners (from the positive side of the x axis) are:
x1(10,10) = 45
x2(-10,10) = 135
x3(-10,-10) = 225
x4(10,-10) = 315
so you can get the formula for the rotation:
newAngle = lastAngle + rotation
radius = 10 * sqrt(2)
NewX = cos(newAngle) * radius - radius of the circle * cos of the new angle
NewY = sin(newAngle) * radius - radius of the circle * sin of the new angle

This should work.

Share this post


Link to post
Share on other sites
Assuming that the snippets shown in the OP are actual code, then the problem is simply that a overwriting of x happens. Use other variables instead:

tempx = std::cos(radians)*x - std::sin(radians)*y;
tempy = std::sin(radians)*x + std::cos(radians)*y;

Share this post


Link to post
Share on other sites
Quote:
Original post by haegarr
Assuming that the snippets shown in the OP are actual code, then the problem is simply that a overwriting of x happens. Use other variables instead:

tempx = std::cos(radians)*x - std::sin(radians)*y;
tempy = std::sin(radians)*x + std::cos(radians)*y;


Damn, I feel stupid now. ;(
Copypaste from internets whithout reflecting isn't always good.
Thanks!

Share this post


Link to post
Share on other sites

This topic is 3490 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.

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