Jump to content
  • Advertisement
Sign in to follow this  
lexs

Simple rotation

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

Share this post


Link to post
Share on other sites
That should work for this specific case (a square) but not for rectangles.
Thanks anyways though!

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
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!