Search code examples
c++openglcollision-detectioncollisionaabb

Rotating object AABBs updating incorrectly


When rotating the object the AABBs around the legs dont move with the legs. Instead they stay in the same spot in relation to the object, and simply just increase in size to keep the leg in the AABB instead of moving with the legs. This is caused by each of these AABBs having the same position, which is the position of the main object itself.

Lets assume this code below is called every frame, is it possible to have these AABBs follow correctly. (Im not asking for the AABBs to rotate with the legs perfectly, i know they cant because they are AABBs but i just want the position to stay around the leg correctly)

        //Clear old AABBs           
        obj->ClearSecondaryAABBs();

        glm::mat4 rotMat = obj->GetRotationMatrix();
        glm::vec3 scale = obj->GetScale();

        //Calc min max for each mesh
        for (auto&& mesh : tempObj->GetMeshes())
        {
            glm::vec3 min = mesh.Vertices[0].Position * scale;
            glm::vec3 max = mesh.Vertices[0].Position * scale;
            glm::vec3 v;


            for (size_t i = 0; i < mesh.Vertices.size(); i++)
            {
                v = mesh.Vertices[i].Position * scale;
                v = rotMat * glm::vec4(v, 1);
                CalculateMinMax(v, min, max);
            }

            // create new aabb
            AABB* newBox = new AABB(min, max);

            newBox->min += obj->GetPosition();
            newBox->max += obj->GetPosition();

Here is a video of the problem, hopefully you can see whats happening, mainly look at what the AABBs of the legs do https://www.youtube.com/watch?v=B__1pLnjv7k


Solution

  • Is it at all caused by failing to rotate the initial min/max location?

    glm::vec3 min = mesh.Vertices[0].Position * scale;
    glm::vec3 max = mesh.Vertices[0].Position * scale;
    min = rotMat * glm::vec4(min, 1);
    max = rotMat * glm::vec4(max, 1);