Search code examples
c++glm-math

obb rotation calculation


i need some help whit obb rotation:

First, i check for each vertex of the 3d model and obtain the min and max values to find the obb min/max points.

void obb::checkVertex(vector3f& vertex)
{
    vLowerLeftFront.x = min(vLowerLeftFront.x, vertex.x);
    vLowerLeftFront.y = min(vLowerLeftFront.y, vertex.y);
    vLowerLeftFront.z = max(vLowerLeftFront.z, vertex.z);
    // Update upper-right-back corner of BB
    vUpperRightBack.x = max(vUpperRightBack.x, vertex.x);
    vUpperRightBack.y = max(vUpperRightBack.y, vertex.y);
    vUpperRightBack.z = min(vUpperRightBack.z, vertex.z);
}

Then, for each frame, update the obb (the eight corners) multiplying by the modelViewProjection matrix.

void obb::update()
{
    //vector3f is a struct whit 3 floats (x,y,z);
    vector3f tmpV = vector3f(0.0f,0.0f,0.0f);
    float *floatMatrix = glm::value_ptr(matrix::getInstance()->getModelViewProjectionMatrix());


    tmpV.x = (floatMatrix[0] * vLowerLeftFront.x) +
               (floatMatrix[4] * vLowerLeftFront.y) +
               (floatMatrix[8] * vLowerLeftFront.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vLowerLeftFront.x) +
               (floatMatrix[5] * vLowerLeftFront.y) +
               (floatMatrix[9] * vLowerLeftFront.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vLowerLeftFront.x) +
               (floatMatrix[6] * vLowerLeftFront.y) +
               (floatMatrix[10] * vLowerLeftFront.z) +
               floatMatrix[14];

    vLowerLeftFront = tmpV;


    tmpV.x = (floatMatrix[0] * vUpperRightBack.x) +
               (floatMatrix[4] * vUpperRightBack.y) +
               (floatMatrix[8] * vUpperRightBack.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vUpperRightBack.x) +
               (floatMatrix[5] * vUpperRightBack.y) +
               (floatMatrix[9] * vUpperRightBack.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vUpperRightBack.x) +
               (floatMatrix[6] * vUpperRightBack.y) +
               (floatMatrix[10] * vUpperRightBack.z) +
               floatMatrix[14];

    vUpperRightBack = tmpV;


    tmpV.x = (floatMatrix[0] * vLowerRightFront.x) +
               (floatMatrix[4] * vLowerRightFront.y) +
               (floatMatrix[8] * vLowerRightFront.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vLowerRightFront.x) +
               (floatMatrix[5] * vLowerRightFront.y) +
               (floatMatrix[9] * vLowerRightFront.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vLowerRightFront.x) +
               (floatMatrix[6] * vLowerRightFront.y) +
               (floatMatrix[10] * vLowerRightFront.z) +
               floatMatrix[14];

    vLowerRightFront = tmpV;

    tmpV.x = (floatMatrix[0] * vLowerRightBack.x) +
               (floatMatrix[4] * vLowerRightBack.y) +
               (floatMatrix[8] * vLowerRightBack.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vLowerRightBack.x) +
               (floatMatrix[5] * vLowerRightBack.y) +
               (floatMatrix[9] * vLowerRightBack.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vLowerRightBack.x) +
               (floatMatrix[6] * vLowerRightBack.y) +
               (floatMatrix[10] * vLowerRightBack.z) +
               floatMatrix[14];

    vLowerRightBack = tmpV;

    tmpV.x = (floatMatrix[0] * vLowerLeftBack.x) +
               (floatMatrix[4] * vLowerLeftBack.y) +
               (floatMatrix[8] * vLowerLeftBack.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vLowerLeftBack.x) +
               (floatMatrix[5] * vLowerLeftBack.y) +
               (floatMatrix[9] * vLowerLeftBack.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vLowerLeftBack.x) +
               (floatMatrix[6] * vLowerLeftBack.y) +
               (floatMatrix[10] * vLowerLeftBack.z) +
               floatMatrix[14];

    vLowerLeftBack = tmpV;


    tmpV.x = (floatMatrix[0] * vUpperRightFront.x) +
               (floatMatrix[4] * vUpperRightFront.y) +
               (floatMatrix[8] * vUpperRightFront.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vUpperRightFront.x) +
               (floatMatrix[5] * vUpperRightFront.y) +
               (floatMatrix[9] * vUpperRightFront.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vUpperRightFront.x) +
               (floatMatrix[6] * vUpperRightFront.y) +
               (floatMatrix[10] * vUpperRightFront.z) +
               floatMatrix[14];

    vUpperRightFront = tmpV;


    tmpV.x = (floatMatrix[0] * vUpperLeftBack.x) +
               (floatMatrix[4] * vUpperLeftBack.y) +
               (floatMatrix[8] * vUpperLeftBack.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vUpperLeftBack.x) +
               (floatMatrix[5] * vUpperLeftBack.y) +
               (floatMatrix[9] * vUpperLeftBack.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vUpperLeftBack.x) +
               (floatMatrix[6] * vUpperLeftBack.y) +
               (floatMatrix[10] * vUpperLeftBack.z) +
               floatMatrix[14];

    vUpperLeftBack = tmpV;



    tmpV.x = (floatMatrix[0] * vUpperLeftFront.x) +
               (floatMatrix[4] * vUpperLeftFront.y) +
               (floatMatrix[8] * vUpperLeftFront.z) +
               floatMatrix[12];
    tmpV.y = (floatMatrix[1] * vUpperLeftFront.x) +
               (floatMatrix[5] * vUpperLeftFront.y) +
               (floatMatrix[9] * vUpperLeftFront.z) +
               floatMatrix[13];
    tmpV.z = (floatMatrix[2] * vUpperLeftFront.x) +
               (floatMatrix[6] * vUpperLeftFront.y) +
               (floatMatrix[10] * vUpperLeftFront.z) +
               floatMatrix[14];

    vUpperLeftFront = tmpV;

}

The problem is that the rotation is not calculated properly.. i dont know why..

I need help to solve this problem and calculate correctly the obb rotation.

Example of the content of variables:

floatMatrix[0] = -7.91465e-008
floatMatrix[1] = -0.847687 
floatMatrix[2] = -0.936392 
floatMatrix[3] = -0.936329 
floatMatrix[4] = 1.81066 
floatMatrix[5] = -3.70536e-008 
floatMatrix[6] = -4.0931e-008 
floatMatrix[7] = -4.09282e-008
floatMatrix[8] = 0 
floatMatrix[9] = 2.2605
floatMatrix[10] = -0.351147
floatMatrix[11] = -0.351123
floatMatrix[12] = 1.0864
floatMatrix[13] = -7.68569
floatMatrix[14] = 45.4257
floatMatrix[15] = 45.6226

vLowerLeftFront: (-727.46, 84.64, 273.49)
vUpperRightBack: (-459.98, -19.68, 19.6)

Solution

  • Then, for each frame, update the obb (the eight corners) multiplying by the modelViewProjection matrix.

    This combination of matrices transforms the vertices from model space, to world space (which is probably what you want), then to camera space, then finally to clip space, which isn't very useful at all.

    Multiply by the model matrix only to convert the model coordinates to world coordinates.