我有这个我在互联网上找到的 AABB 课程,它在网格移动时有效,但是当我旋转网格时它当然停止工作,我也注意到它有一个变换功能,所以我认为这是我必须要做的使用所以它在我旋转网格后工作,但我对矩阵有点新,我不知道如何使用它:
#ifndef AABB_H
#define AABB_H
class aabb {
public:
aabb() { min.x = min.y = min.z = 1e24f; max.x = max.y = max.z = -1e24f; }
aabb &operator =(const aabb &a) { min = a.min; max = a.max; return *this; }
vec3d min, max;
vec3d center() { return ((min + max) * 0.5f); }
void empty() { min.x = min.y = min.z = 1e24f; max.x = max.y = max.z = -1e24f; }
void add(const vec3d &pnt) {
if (pnt.x < min.x) min.x = pnt.x;
if (pnt.x > max.x) max.x = pnt.x;
if (pnt.y < min.y) min.y = pnt.y;
if (pnt.y > max.y) max.y = pnt.y;
if (pnt.z < min.z) min.z = pnt.z;
if (pnt.z > max.z) max.z = pnt.z;
}
void transform(const mat3x3 &m, const vec3d &trans) {
vec3d oldmin = min, oldmax = max;
min = max = trans;
if (m.m11 > 0.0f) { min.x += m.m11 * oldmin.x; max.x += m.m11 * oldmax.x; }
else { min.x += m.m11 * oldmax.x; max.x += m.m11 * oldmin.x; }
if (m.m12 > 0.0f) { min.y += m.m21 * oldmin.x; max.y += m.m21 * oldmax.x; }
else { min.y += m.m21 * oldmax.x; max.y += m.m21 * oldmin.x; }
if (m.m13 > 0.0f) { min.z += m.m31 * oldmin.x; max.z += m.m31 * oldmax.x; }
else { min.z += m.m31 * oldmax.x; max.z += m.m31 * oldmin.x; }
if (m.m21 > 0.0f) { min.x += m.m12 * oldmin.y; max.x += m.m12 * oldmax.y; }
else { min.x += m.m12 * oldmax.y; max.x += m.m12 * oldmin.y; }
if (m.m22 > 0.0f) { min.y += m.m22 * oldmin.y; max.y += m.m22 * oldmax.y; }
else { min.y += m.m22 * oldmax.y; max.y += m.m22 * oldmin.y; }
if (m.m23 > 0.0f) { min.z += m.m32 * oldmin.y; max.z += m.m32 * oldmax.y; }
else { min.z += m.m32 * oldmax.y; max.z += m.m32 * oldmin.y; }
if (m.m31 > 0.0f) { min.x += m.m13 * oldmin.z; max.x += m.m13 * oldmax.z; }
else { min.x += m.m13 * oldmax.z; max.x += m.m13 * oldmin.z; }
if (m.m32 > 0.0f) { min.y += m.m23 * oldmin.z; max.y += m.m23 * oldmax.z; }
else { min.y += m.m23 * oldmax.z; max.y += m.m23 * oldmin.z; }
if (m.m33 > 0.0f) { min.z += m.m33 * oldmin.z; max.z += m.m33 * oldmax.z; }
else { min.z += m.m33 * oldmax.z; max.z += m.m33 * oldmin.z; }
}
bool contains(const vec3d &a) { return (a.x >= min.x) && (a.x <= max.x) && (a.y >= min.y) && (a.y <= max.y) && (a.z >= min.z) && (a.z <= max.z); }
vec3d closest(const vec3d &a) {
vec3d r;
if (a.x < min.x) r.x = min.x;
else if (a.x > max.x) r.x = max.x;
else r.x = a.x;
if (a.y < min.y) r.y = min.y;
else if (a.y > max.y) r.y = max.y;
else r.y = a.y;
if (a.z < min.z) r.z = min.z;
else if (a.z > max.z) r.z = max.z;
else r.z = a.z;
return r;
}
};
#endif
另外作为附加数据,我只想在 Y-axix 中旋转网格,因为它是一款海军游戏。
感谢您的回答。