我不知道这是否是最有效的方法,但我只会计算顶点的新位置并根据该数据找出 AABB。比如,
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);
如果您不知道如何将矩阵乘以向量,请尝试使用谷歌搜索。
还要注意,由于矩阵维度是 4x4,因此顶点的向量也需要是 4 维的。您可以通过添加第三个分量 0(零)和第四个分量 1(一)将 2D 矢量转换为 4D 矢量。乘法完成后,您可以将得到的 4D 向量转换回 2D,方法是将 x 和 y 分量除以第四个分量,并且只需忽略第三个分量,因为您不需要第三维。
由于矩阵乘法可能是一个相当耗费处理器的运算,因此这种方法可能仅适用于您不需要经常更新大量 AABB 的情况。