0
votes

I'm working on a 3D game using OpenGL and C++, I need some information and your help to understand how I can calculate collisions between OBB's (Oriented Bounding Box) (OBB vs OBB, OBB vs other early ).

I know 8 vertices that make up the bounding box for each object (Multiply this by the Model Matrix and got the position in the 'world'); and now would need to know how to calculate collisions between OBB's.

1
This is in Java, but is a thorough discussion that should help you. stackoverflow.com/a/10289567/2296458Cory Kramer
@Cyber: That answer describes the collision detection for axis-aligned bounding boxes (AABB), which is a very simple problem. It's considerably more complicated for arbitrarily oriented bounding boxes, which this poster is asking about.Reto Koradi
Hello, what I need is to calculate collisions using OBB (more expensive but more accurate) because AABB is not accurate, because it covers a large area of the object and not very faithful. Thanks anyway.Fabian Orue

1 Answers

0
votes

If you only need to know if the objects are colliding or not then a C++ code implementation of the separating axis theorem for simple collision detection between two 3D OBB would be this:

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;
}