0

I have a carrier object and a plane that is controllable by keyboard input, i want to be able to detect a collision between the 2 so i can land on the carrier. This is my code:

//Camera
        gluLookAt(45,30,-50,eyeX,eyeY-5,eyeZ,0,1,0);

        //Skybox
        glPushMatrix(); 
            glEnable(GL_TEXTURE_2D);
                drawSea();
            glDisable(GL_TEXTURE_2D);
            drawCube();
            //Carrier
            glPushMatrix(); 
                //glTranslatef(-22,12,0);
                glTranslatef(-carrierX,12,-carrierZ-190);
                glEnable(GL_TEXTURE_2D);
                    model2.speedDisplayFaceNormals();
                glDisable(GL_TEXTURE_2D);
            glPopMatrix();
        glPopMatrix();

        //Aircraft
        glPushMatrix(); 
            glTranslatef(eyeX,eyeY-5,eyeZ);
            glRotatef(-heading, 0,1,0);
            glRotatef(-elevation, 1,0,0);
            glEnable(GL_TEXTURE_2D);
                model.speedDisplayFaceNormals();
            glDisable(GL_TEXTURE_2D);
            //Bounding box
            glBegin(GL_QUADS);
                glVertex3f(-6, -2, -6);
                glVertex3f(6, -2, -6);
                glVertex3f(6, -2, 6);
                glVertex3f(-6, -2, 6);
            glEnd();
        glPopMatrix();

My initial ideas are to have a plane at the bottom of the aircraft, and one on top of the carrier, and then detecting when they collide should be easy. You can see i have added a plane to the bottom of the aircraft, however, as the position of the aircraft moves, the plane will move too, how do i get the current co-ordinates of the plane as it is being moved? Or is there a different way i should go about this? I have a good understanding of how to execute the detection once i can get the co ordinates as the objects are moving

1 Answer 1

4

OpenGl and collision detection are two completely separate things. OpenGl does the drawing part for you, but it is up to you to keep track of the object positions so you can do collision detection.

Usually what happens is you have an object class which holds a translation vector, a scale vector, and a rotation vector (specifying the rotation about all three axes). You update those on your own, then pass the results to the opengl drawing code.

It seems like you are already doing that with (eyeX,eyeY-5,eyeZ) since the plane seems to be drawn at the eye position. So I don't see what the actual problem is...

Edit: If you want to transform the vertices of the plane you will need to create a transformation matrix out of the translation and rotations and multiply the vertices by this matrix. There are plenty of resources online about how to do this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.