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

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


Edited to fix stupid mistakes.

Edited by dr01d3k4

##### 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 on other sites

Thanks. I will try all this as soon as I get home.

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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11