# AABB of rotated sprite?

Say I have a sprite. Its 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.

Thanks

The straightforward approach is to

• find the extents of the unrotated bounding box
• apply the same rotation matrix to those extents as you did to the sprite (but on the CPU, so you may need to employ your own matrix math here)
• recompute the AABB of the rotated extents (which should be cheap since there are only four).

It's possible to perform the operation in fewer calculations, if you are concerned about (or interested in) those sorts of things. For example, one (potential) technique for doing so is discussed here.

• Is there possibly a greedy way of finding the AABB that satisfies any angle? – jmasterx Jul 11 '11 at 22:50
• This method works for any angle (as should Patrick's); I'm not sure what you mean by "greedy?" – user1430 Jul 11 '11 at 23:11
• I mean a bounding box I could compute at load time where the sprite could be at any angle and show up in this box. This would mean the object would be rendered sometimes when none of it is in fact visible but then I would not have to compute anything at runtime, only offset the AABB by the center which is fast. – jmasterx Jul 11 '11 at 23:13
• Oh, yes. You could precompute a box that would fully-enclose the object in all orientations -- offhand, in the 2D case, consider the longer of the two diagonals. A box of that width and height should be sufficient. – user1430 Jul 12 '11 at 1:10

Consider a box centered at some point in space. Imagine a line from the center to the top-right corner. This length is effectively the hypotenuse of a triangle formed from taking the center of the box and drawing a line right and then up. These sides of the triangle are half-extents. In the picture, the red lines are the half extents and the blue line is the hypotenuse -- the distance from the center of the box to the corner. Call this blue line's length 'dist' If you compute the arctangent of ratio of the half extents (Y / X), then you can get the angle. From there you can simply linearly add Pi/4 to the angle (45 degrees), then use:

 newx = cos(newAngle)*dist; newy = sin(newAngle)*dist; 

Repeat for each of the four points, and then find max/min boundaries of the AABB.

--

This is super tedious. The better way is to use 3x3 matrix representing rotation and translation. In effect, you take each of the four points, translate them back to origin by subtract the center's coordinates from them so that their new center is on (0,0), then rotate them. Finally, you translate them back to the real center. With the new set of points, once again, simply compute new the AABB for those points. Wikipedia has some great information about matrix math, especially how to do rotations, translations, and more using 3x3 matrices.

• I think you meant a 2x2 matrix representing rotation. Perform the rotation on the original AABB using the rotation matrix, then perform the translation. – Olhovsky Jul 12 '11 at 4:59
• @Olhovsky How do you perform a translation with a 2x2 Matrix? – Jonathan Connell Jul 12 '11 at 8:13
• 3nixios: You dont, which is why I said that you perform the translation after using the rotation matrix. The reason being that I was concerned that performing the translation before the rotation would be wrong. However, I was mistaken and using a 3x3 matrix multiplication should work out. – Olhovsky Jul 12 '11 at 10:00