Jump to content
  • Advertisement
Sign in to follow this  
P0jahn

Get position of rotated rectangle

This topic is 2025 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 am developing a 2D game where the top-left corner is (x:0, y:0) thus x increasing when moving right and y increasing when moving down.

 

 

Same applies to all the rectangles rendered in the game, their top-left corner is their position.

 

So I am rotating an rectangle around its position(orginX:0 relative and orginY:0 relative).

I want to calculate the rectangles top-right position, taking the rotation into account.

 

Share this post


Link to post
Share on other sites
Advertisement

Using trigonometry, you can see the sides as the hypotenuses of right angled triangles.

topLeft = (x, y);
topRight = (x + (width * cos(angle)), y + (width * sin(angle)));
bottomLeft = (x + (height * sin(angle)), y + (height * cos(angle)));
bottomRight = (x + (width * cos(angle)) + (height * sin(angle)), y + (width * sin(angle)) + (height * cos(angle)));

Edited to fix stupid mistakes.

Edited by dr01d3k4

Share this post


Link to post
Share on other sites

Though it should be noted that if the rectangle is rotated enough, the TopRight corner of the rectangle may not be the point closest to the top of the screen anymore.  If the rectangle is rotated 180 degrees, the TopRight corner is in the lower left.

Share this post


Link to post
Share on other sites

Using trigonometry, you can see the sides as the hypotenuses of right angled triangles.

topLeft = (x, y);
topRight = (x + (width * cos(angle)), y + (width * sin(angle)));
bottomLeft = (x + (height * sin(angle)), y + (height * cos(angle)));
bottomRight = (x + (width * cos(angle)) + (height * sin(angle)), y + (width * sin(angle)) + (height * cos(angle)));

Almost correct. But 2 of the sine terms need to be subtracted instead of added.

 

For example, if rotation with positive angle goes clockwise (and I made no mistake): 

topLeft = (x, y);
topRight = (x + (width * cos(angle)), y + (width * sin(angle)));
bottomLeft = (x - (height * sin(angle)), y + (height * cos(angle)));
bottomRight = (x + (width * cos(angle)) - (height * sin(angle)), y + (width * sin(angle)) + (height * cos(angle)));

 

EDIT: In general it is more comfortable to use matrices for such stuff.

Edited by haegarr

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!