Simple rotation

Started by
3 comments, last by lexs 15 years, 11 months ago
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 :)
Advertisement
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.
That should work for this specific case (a square) but not for rectangles.
Thanks anyways though!
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;
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!

This topic is closed to new replies.

Advertisement