Search code examples
c++opengl3dglm-math

Transforming Bounding Boxes referenced to an object?


I'm trying to implement AABBs/OOBBs with MathGeoLib since the ease to operate with BBs (and because I wanted to test some things with that library).

The problem is that the engine's objects transformations are based on glm since we started with glm (and they work properly) but when it comes to transform the OOBBs according to an object, it doesn't work very well.

What I basically do is to pass to a function the game object's translation, orientation and scale (I tried to pass a global matrix but it doesn't work, it seems to 'add' the transformation instead of setting it, and I can't access the oobb's matrix). That function does the next:

glm::vec3 pos = passedPosition - OOBBPreviousPos;
glm::mat4 Transformation = glm::translate(glm::mat4(1.0f), pos) *
     glm::mat4_cast(passedRot) * glm::scale(glm::mat4(1.0f), passedScale);

glm::mat4 resMat = glm::transpose(Transformation);

math::float4x4 mat = math::float4x4::identity;
mat.Set(glm::value_ptr(resMat));

Which basically transposes the glm matrix (I have seen that that's they way of 'translating' them), passes it to a float* and then it constructs the MathGeoLib matrix with that. I have debugged it and the values seem to be right according to the object, so the next thing I do is actually transform the OOBB and then, enclose the AABB to have it inside, like this:

m_OBB.Transform(mat);
m_AABB.SetNegativeInfinity(); //Sets AABB to "null"
m_AABB.Enclose(m_OBB);

The final behaviour is pretty strange, believe me if I say that is the most close I've been from having it right, I've been some days testing different things and nothing works better (passing global/local matrices directly, trying different ways of passing/constructing transformation data, checking if the glm-MathGLib is correct...). It rotates but not around its own axis, and the scaling gets him crazy (although translation works). Its current behaviour can be seen here: https://gfycat.com/quarrelsomefineduck (blue cubes are AABBs, green ones are OOBBs).

Am I doing something wrong with the mathematics calculations or data transfer?


Solution

  • I still been looking on that but then some friend made me look into another direction, so I finally solved it (or better said: I "worked-around it") by storing an initial object's AABB and passing to the mentioned function the game object's global matrix. Then, inside the function, I used another MathGeoLib function to transform the OOBB.

    That function finally looks like:

    glm::mat4 resMat = glm::transpose(GlobalMatrixPassed);
    math::float4x4 mat = math::float4x4::identity;
    mat.Set(glm::value_ptr(resMat)); //"Translate" glm matrix passed into a MathGeoLib one
    
    m_OOBB.SetFrom(m_InitialAABB);  //Set OOBB from the initial aabb
    m_OOBB.Transform(mat); //Transform it
    m_AABB.SetFrom(m_OOBB); //Set the AABB in function of the transformed OOBB