计算边界框碰撞面向c++ opengl



我正在使用OpenGL和c++制作3D游戏,我需要一些信息和您的帮助来了解我如何计算OBB之间的碰撞(OBB vs OBB, OBB vs其他早期)。

我知道8个顶点构成了每个对象的边界框(将其乘以模型矩阵并获得在"世界"中的位置);现在需要知道如何计算黑洞之间的碰撞

如果你只需要知道对象是否发生碰撞,那么在两个3D OBB之间进行简单碰撞检测的分离轴定理的c++代码实现将是:

struct vec3 
{
    float x, y, z;
    vec3 operator- (const vec3 & rhs) const { return{ x - rhs.x, y - rhs.y, z - rhs.z }; }
    float operator* (const vec3 & rhs) const { return{ x * rhs.x + y * rhs.y + z * rhs.z }; } // DOT PRODUCT
    vec3 operator^ (const vec3 & rhs) const { return{ y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x }; } // CROSS PRODUCT
    vec3 operator* (const float& rhs)const { return vec3{ x * rhs, y * rhs, z * rhs }; }
};
struct OBB
{
    vec3 Pos, AxisX, AxisY, AxisZ, Half_size;
};
bool getSeparatingPlane(const vec3& RPos, const vec3 Plane, const OBB& box1, const OBB&box2)
{
    return (fabs(RPos*Plane) > (fabs((box1.AxisX*box1.Half_size.x)*Plane) + fabs((box1.AxisY*box1.Half_size.y)*Plane) + fabs((box1.AxisZ*box1.Half_size.z)*Plane) +
    fabs((box2.AxisX*box2.Half_size.x)*Plane) + fabs((box2.AxisY*box2.Half_size.y)*Plane) + fabs((box2.AxisZ*box2.Half_size.z)*Plane)));
}
bool getCollision(const OBB& box1, const OBB&box2)
{
    static vec3 RPos;
    RPos = box2.Pos - box1.Pos;
    if (getSeparatingPlane(RPos, box1.AxisX, box1, box2) || getSeparatingPlane(RPos, box1.AxisY, box1, box2) || getSeparatingPlane(RPos, box1.AxisZ, box1, box2) ||
getSeparatingPlane(RPos, box2.AxisX, box1, box2) || getSeparatingPlane(RPos, box2.AxisY, box1, box2) || getSeparatingPlane(RPos, box2.AxisZ, box1, box2) ||
getSeparatingPlane(RPos, box1.AxisX^box2.AxisX, box1, box2) || getSeparatingPlane(RPos, box1.AxisX^box2.AxisY, box1, box2) ||
getSeparatingPlane(RPos, box1.AxisX^box2.AxisZ, box1, box2) || getSeparatingPlane(RPos, box1.AxisY^box2.AxisX, box1, box2) ||
getSeparatingPlane(RPos, box1.AxisY^box2.AxisY, box1, box2) || getSeparatingPlane(RPos, box1.AxisY^box2.AxisZ, box1, box2) ||
getSeparatingPlane(RPos, box1.AxisZ^box2.AxisX, box1, box2) || getSeparatingPlane(RPos, box1.AxisZ^box2.AxisY, box1, box2) ||
getSeparatingPlane(RPos, box1.AxisZ^box2.AxisZ, box1, box2)) return false;
    return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
    OBB A, B;
    A.Pos = { 0.f, 0.f, 0.f };
    A.Half_size = {10.f, 1.f, 1.f};
    A.AxisX = { 1.f, 0.f, 0.f };
    A.AxisY = { 0.f, 1.f, 0.f };
    A.AxisZ = { 0.f, 0.f, 1.f };
    B.Pos = { 10.f, 0.f, 0.f };
    B.Half_size = {10.f, 1.f, 1.f};
    B.AxisX = { 1.f, 0.f, 0.f };
    B.AxisY = { 0.f, 1.f, 0.f };
    B.AxisZ = { 0.f, 0.f, 1.f };
    if (getCollision(A, B)) std::cout << "Collision!!!" << std::endl;
    else std::cout << "No collision." << std::endl;
    std::cout << std::endl;
    system("pause");
    return 0;
}

最新更新