Advertisement

Get position of rotated rectangle

Started by April 06, 2014 06:09 PM
3 comments, last by haegarr 10 years, 10 months ago

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.

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.

Falling block colour flood game thing I'm making: http://jsfiddle/dr01d3k4/JHnCV/

Advertisement

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.

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

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.

This topic is closed to new replies.

Advertisement