Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AABB of rotated sprite?

Tags:

c++

algorithm

Say I have a sprite. Its axis-aligned bounding box (AABB) is easy to find since I know the width and height. Say I rotate it 45 degrees, I don't think the AABB would be big enough to cover it, so I need a new AABB. How can I calculate the bounding rectangle of a rotated rectangle? (given a center point, an angle, and its width and height).

Note that OpenGL does the rotation so I do not have access to the vertex information.

What I'm trying to do is get AABBs so I can do 2D culling for rendering.

Is there possibly a greedy way of finding the AABB that satisfies any angle?

Thanks

like image 900
jmasterx Avatar asked Jul 11 '11 22:07

jmasterx


2 Answers

enter image description here

like image 200
Gareth Rees Avatar answered Sep 28 '22 04:09

Gareth Rees


If you want a single box that covers all angles, just take the half-diagonal of your existing box as the radius of a circle. The new box has to contain this circle, so it should be a square with side-length equal to twice the radius (equiv. the diagonal of the original AABB) and with the same center as the original.

In general the object will be rotated around an arbitrary point, so you have to compute the new location of the center and translate this box to the right place.

like image 39
Sumudu Fernando Avatar answered Sep 28 '22 03:09

Sumudu Fernando