# Simple rotation

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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


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 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
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 on other sites
That should work for this specific case (a square) but not for rectangles.
Thanks anyways though!

##### 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 on other sites
Quote:
 Original post by haegarrAssuming 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!

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633726
• Total Posts
3013570
×