AABB rotated sprite?

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 will be big enough to cover it, so I need a new AABB. How can I calculate the bounding rectangle of a rotary rectangle? (taking into account the center point, angle, width and height).

Note that OpenGL does the rotation, so I don't have access to vertex information.

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

Maybe there is a greedy way to find an AABB that suits any corner?

thanks

+8
c ++ algorithm
source share
3 answers

enter image description here

+29
source share

If you need one drawer that spans all corners, just take half the diagonal of your existing drawer as the radius of the circle. The new box should contain this circle, so it should be a square with a side size equal to twice the radius (equivalent to 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 need to calculate the new location of the center and move this field to the right place.

+1
source share

I don’t know if this is the most efficient method, but I would just calculate the new vertex positions and learn AABB based on this data. For example,

Vertex v0, v1, v2, v3; // in the local coordinates of the rectangle // so for example v0 is always 0,0 and width and height define the others // put some values to v0..v3 glLoadIdentity(); glTranslatef(the position of the rectangle); glTranslatef(center_point); glRotatef(angle, 0,0,1); glTranslatef(-center_point); GLfloat matrix[16]; glGetFloatv(GL_MODELVIEW_MATRIX, matrix); v0 = multiply_matrix_by_vector(matrix, v0); v1 = multiply_matrix_by_vector(matrix, v1); v2 = multiply_matrix_by_vector(matrix, v2); v3 = multiply_matrix_by_vector(matrix, v3); AABB = find_the_minimums_and_maximums(v0, v1, v2, v3); 

If you don’t know how to multiply the matrix by vector, try searching on it.

Also note that since the dimensions of the matrix are 4x4, the vectors for the vertices must also be 4-dimensional. You can convert a 2D vector to a 4D vector by adding the third component 0 (zero) and the fourth component 1 (one). After doing the multiplication, you can convert the resulting 4D vector back to 2D by dividing the x and y components into the fourth component and simply ignoring the third component because you don't need the third dimension.

Since matrix multiplications can be quite hard work with the processor, this approach can only be good if you do not need to update a large number of AABBs frequently.

0
source share

All Articles