Question

I am trying to make a tank game. I have successfully loaded an OBJ model, and calculated its bounding box for the model at the origin.

I am now trying to apply the transformations done to my model in the game logic to the original coordinates for the bounding box. For this, I grab the modelview matrix right before drawing my model, then multiply this matrix for the two vectors that define the BBox.

Here is the code that draws my tank:

void drawTank()
{
    bBox = calcBBox(modelo, 1);

    glPushMatrix();
        glBindTexture(GL_TEXTURE_2D, texTank);
        glScalef(0.2, 0.2, 0.2);

        glTranslatef(posTank.x,posTank.y,posTank.z);
        glRotatef(anguloTanque, 0, 1, 0); // rotate around Y (horizontal)


        glRotatef(90, 0, 1, 0);
        glRotatef(-90, 1, 0, 0);
        glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
        glmDraw(modelo,  GLM_TEXTURE | GLM_MATERIAL);


        glColor3f(1,0,0);
        drawBBox(bBox);

    glPopMatrix();


}

With this snippet, my bbox is properly drawn over the tank model (transformations are applied in rendering by the glTranslate & glRotate functions). As you can see I also grab here my ModelView matrix.

Then I apply this matrix as follows (this is my entire display function):

void Display(void)  {


    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    glPushMatrix();
        camera(); 

        glEnable(GL_TEXTURE_2D);

        //glTranslatef(0,-40,150);

        //PLANE
        glBindTexture(GL_TEXTURE_2D, texArena);
            glBegin(GL_POLYGON);
            glTexCoord2f( 0.0f, 0.0f );
            glVertex3f(-500, 0, -500);
            glTexCoord2f( 5.0f, 5.0f );
            glVertex3f(500, 0, -500);
            glTexCoord2f(5.0f, 0.0f );
            glVertex3f(500, 0, 500);
            glTexCoord2f( 0.0f, 5.0f );
            glVertex3f(-500, 0, 500);
        glEnd();


        drawTank();

    glPopMatrix();



        point3D max = bBox.max;
        point3D min = bBox.min;
        point3D resultMax;
        point3D resultMin;
        //Transformacion
        multVectorByMatrix(matrix, max, resultMax);
        multVectorByMatrix(matrix, min, resultMin);
        bBox.max.x = resultMax.x; bBox.max.y = resultMax.y; bBox.max.z = resultMax.z;
        bBox.min.x = resultMin.x; bBox.min.y = resultMin.y; bBox.min.z = resultMin.z;

        glPushMatrix();
            glColor3f(1,1,1);
            drawBBox(bBox);
        glPopMatrix();




    glFlush();

    glutSwapBuffers();

}

The function that multiplies a vector by a matrix:

void multVectorByMatrix(float* matrix, point3D vector, point3D &result)
{
    result.x = (matrix[0] * vector.x) +
               (matrix[4] * vector.y) + 
               (matrix[8] * vector.z) +
               matrix[12];
    result.y = (matrix[1] * vector.x) +
               (matrix[5] * vector.y) + 
               (matrix[9] * vector.z) +
               matrix[13];
    result.z = (matrix[2] * vector.x) +
               (matrix[6] * vector.y) + 
               (matrix[10] * vector.z) +
               matrix[14];
}

If I draw the bounding box with this render loop, then my bounding box gets drawn but transformations are not applied properly. I can see the bounding box moving correctly with translations, but rotations are not done right.

What might be the problem here?

edit: some screenshots enter image description here enter image description here

Was it helpful?

Solution

Your problem is in this code.

    point3D max = bBox.max;
    point3D min = bBox.min;
    point3D resultMax;
    point3D resultMin;
    //Transformacion
    multVectorByMatrix(matrix, max, resultMax);
    multVectorByMatrix(matrix, min, resultMin);
    bBox.max.x = resultMax.x; bBox.max.y = resultMax.y; bBox.max.z = resultMax.z;
    bBox.min.x = resultMin.x; bBox.min.y = resultMin.y; bBox.min.z = resultMin.z;

    glPushMatrix();
        glColor3f(1,1,1);
        drawBBox(bBox);
    glPopMatrix();

You take two vertices from your box and then apply transformations to them, then you use this transformed vertices to display a box, which of course will be axis aligned, because that's the only box you can get from just two opposite vertices. And you can see on your screenshot, that you bbox and the correct bbox have common vertices - these are exactly the vertices you applied your transformations to. So, in order to get a correct bbox, you need to get all vertices of the bbox and apply these transformations to all of them. Then you'll get exactly what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top