Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am using c++ in visual studio 2010 with opengl. I am trying to make a programme that has 2 boxes that are able to be stacked on top of each other but I am having some trouble with the collision detection. What I'm trying to do is just put bounding boxes around the box and a flat surface that can be moved up and down below the box to then move it up and down as well. However, I cannot get this to work. These are the variables for the flat surface that will be below the box:

float truckMaxX = 5;
float truckMinX = -5;
float truckMaxZ = 5;
float truckMinZ = -5;

This is then the surface being created:

void drawTruck()
{
    glBegin(GL_POLYGON);
        glColor3f(1,1,0);
        glVertex3f(truckMinX,-0.99,truckMinZ);
        glVertex3f(truckMinX,-0.99,truckMaxZ);
        glVertex3f(truckMaxX,-0.99,truckMaxZ);
        glVertex3f(truckMaxX,-0.99,truckMinZ);
    glEnd();
}

The variables for the box:

float box1MaxX = 2;
float box1MinX = 0;
float box1MaxY = 1;
float box1MinY = -1;
float box1MaxZ = 2;
float box1MinZ = 0;

And then drawing that box:

void box1()
{
    //back
    glBegin(GL_LINE_LOOP);
        glColor3f(0,0,0);
        glVertex3f(box1MinX,box1MinY,box1MinZ);
        glVertex3f(box1MinX,box1MaxY,box1MinZ);
        glVertex3f(box1MaxX,box1MaxY,box1MinZ);
        glVertex3f(box1MaxX,box1MinY,box1MinZ);
    glEnd();

    //front
    glBegin(GL_LINE_LOOP);
        glColor3f(0,0,0);
        glVertex3f(box1MinX,box1MinY,box1MaxZ);
        glVertex3f(box1MinX,box1MaxY,box1MaxZ);
        glVertex3f(box1MaxX,box1MaxY,box1MaxZ);
        glVertex3f(box1MaxX,box1MinY,box1MaxZ);
    glEnd();

    //top
    glBegin(GL_LINE_LOOP);
        glColor3f(0,0,0);
        glVertex3f(box1MinX,box1MaxY,box1MinZ);
        glVertex3f(box1MinX,box1MaxY,box1MaxZ);
        glVertex3f(box1MaxX,box1MaxY,box1MaxZ);
        glVertex3f(box1MaxX,box1MaxY,box1MinZ);
    glEnd();

    //bottom
    glBegin(GL_LINE_LOOP);
        glColor3f(0,0,0);
        glVertex3f(box1MinX,box1MinY,box1MinZ);
        glVertex3f(box1MinX,box1MinY,box1MaxZ);
        glVertex3f(box1MaxX,box1MinY,box1MaxZ);
        glVertex3f(box1MaxX,box1MinY,box1MinZ);
    glEnd();

    //left
    glBegin(GL_LINE_LOOP);
        glColor3f(0,0,0);
        glVertex3f(box1MinX,box1MinY,box1MinZ);
        glVertex3f(box1MinX,box1MinY,box1MaxZ);
        glVertex3f(box1MinX,box1MaxY,box1MaxZ);
        glVertex3f(box1MinX,box1MaxY,box1MinZ);
    glEnd();

    //right
    glBegin(GL_LINE_LOOP);
        glColor3f(0,0,0);
        glVertex3f(box1MaxX,box1MinY,box1MinZ);
        glVertex3f(box1MaxX,box1MinY,box1MaxZ);
        glVertex3f(box1MaxX,box1MaxY,box1MaxZ);
        glVertex3f(box1MaxX,box1MaxY,box1MinZ);
    glEnd();
}

With these two now created I want to see if the box is able to be lifted up. This is if the surface below the box is at least half way across it in the X direction and is fully covered in the Z direction using this test:

bool truckBoxCollision()
{
    if(truckMaxX >= (box1MaxX/2) && truckMaxZ >= box1MaxZ)
        return true;
}

Then to try and move them up this is used:

if(keys['U'])
    {
        truckHeight += 0.05;
        if (truckBoxCollision() == true)
        {
            boxHeight += 0.05;
        }
        keys['U'] = false;
    }

This works to move the flat surface up but the box doesn't move up at all even if the surface is fully under the box. Is there something wrong with the box that I have created or do I need to add something else in there as well?

EDIT:

The truckHeight and boxHeight are 2 variables used to translate the truck and the box when they are drawn so they would effect and change the max and min y values. Here is the code for that:

glPushMatrix();
    glTranslatef(Xtri,truckHeight,Ztri);
    glRotatef(heading,0,1,0);
    drawTruck();
    glPopMatrix();

glPushMatrix();
    glTranslatef(0,boxHeight,0);
    box1();
    glPopMatrix();

All the Xtri and Ztri do is move the truck in the direction that it is facing and heading is so the truck rotates on the spot to face in a new direction.

share|improve this question
    
truckBoxCollision() has no references to either truckHeight or boxHeight. –  congusbongus Mar 11 '13 at 1:56
    
Would it make a difference since truckBoxCollision() is only returning a boolean value? All truckHeight and boxHeight are variables for when they are translated once they are drawn to the scene –  user1324894 Mar 11 '13 at 18:55
    
I thought that truckHeight and boxHeight were used to move the truck/box respectively, and hence their changing should affect whether truck/box collide or not, so it's strange to see that they're not used in truckBoxCollision. Unless truckHeight/boxHeight change truck/box/MaxX/Y/Z? Then you should show that code and make this clearer. –  congusbongus Mar 11 '13 at 23:11
    
ok I have added that in at the end as an edit so hopefully that should seem a little clearer now. I'm still stuck on it so any help that you can give me is greatly appreciated –  user1324894 Mar 12 '13 at 0:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.