Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 14

Thread: Basic 2D car game problem

Threaded View

  1. #10
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    28
    Quote Originally Posted by Carmine View Post
    Yes. That's what you said. But your code doesn't show that you know how to do even basic rotations and translations. To me the code is the truth. It is where you are at. If you can do basic rotations and translations, show it to us in the code. Explain why you are not satisfied with them. I think you'll get more responses from there. You will from me.
    Thank you Carmine! Here you have the code:

    #include <iostream>
    #include <stdlib.h>
    #include <glut.h>

    using namespace std;

    float _angle = 0.0f;//For rotating the car
    float xpos = 0.0f;//For moving the car in the X axis
    float ypos = 0.0f;//For moving the car in the Y axis
    float cameraheight = -20.0f;//For zoom or unzoom the camera

    //Called when a key is pressed
    void handleKeypress(unsigned char key, int x, int y) {
    switch (key) {
    case 27: //ESC
    exit(0);
    break;

    case 49: //Number 1
    _angle += 5.0f;
    break;

    case 50: //Number 2
    _angle -= 5.0f;
    break;

    case 51: //Number 3
    cameraheight -= 5.0f;
    break;

    case 52: //Number 4
    cameraheight += 5.0f;
    break;

    case 53: //Number 5
    xpos += 1.0f;
    break;

    case 54: //Number 6
    xpos -= 1.0f;
    break;

    case 55: //Number 7
    ypos += 1.0f;
    break;

    case 56: //Number 8
    ypos -= 1.0f;
    break;

    /*case 57: //Number 9

    break;

    case 58: //Number 0

    break;*/

    }
    }


    //Initializes 3D rendering
    void initRendering() {
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL); //Enable color
    glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //Background color is sky blue
    }

    //Called when the window is resized
    void handleResize(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (double)w / (double)h, 0.01/*Min render distance*/, 1000.0/*Max distance*/);//Meters
    }

    //Draws the 3D scene
    void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, cameraheight);//I have moved the circuit and the car 20 meters under, so now
    //the camera is "set" at 20 meters high than the car and the circuit


    //CAR
    glPushMatrix(); //Save the transformations performed thus far
    glTranslatef(xpos, ypos, 0.0f);
    glRotatef(_angle, 0.0f, 0.0f, 1.0f); //Rotate about the z-axis


    glBegin(GL_QUADS);
    glColor3f(1.0f, 0.0f, 0.0f); //Red Ferrari
    glVertex3f(-2.25f, 1.0f, 0.0f); //Meters (4,5m long per 2,25m wide)
    glVertex3f(2.25f, 1.0f, 0.0f);
    glVertex3f(2.25f, -1.0f, 0.0f);
    glVertex3f(-2.25f, -1.0f, 0.0f);
    glEnd();

    glPopMatrix(); //Undo the move of the car


    //CIRCUIT
    glPushMatrix();
    glScalef(0.25f, 0.25f, 0.25f);//25% original size

    glBegin(GL_QUADS);
    glColor3f(0.2f, 0.2f, 0.2f); //Asphalt color
    glVertex3f(-200.0f, 200.0f, 0.0f); //Meters
    glVertex3f(200.0f, 200.0f, 0.0f);
    glVertex3f(200.0f, -200.0f, 0.0f);
    glVertex3f(-200.0f, -200.0f, 0.0f);
    glEnd();
    glPopMatrix();


    glutSwapBuffers();
    }

    void update(int value) {
    if (_angle > 360) {
    _angle -= 360;
    }

    glutPostRedisplay();
    glutTimerFunc(16, update, 0);
    }

    int main(int argc, char** argv) {
    //Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(320, 240);

    //Create the window
    glutCreateWindow("Test");
    initRendering();

    //Set handler functions
    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);
    glutTimerFunc(16, update, 0); //Add a timer

    glutMainLoop();
    return 0;
    }











    I'm not satisfied because when I rotate the car it not goes in the direction is facing, it only goes up/down/left/right. I don't know how to make it go correctly





    So much thank you,

    Marc
    Last edited by Marc Colomé; 08-17-2013 at 04:45 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •