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

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    27

    Basic 2D car game problem

    I'm programming the typical 2d car game in c++ with glut and opengl and i'm trying to acommplish this:

    1. Two quads (4 tris), one for the circuit, the other for the car
    2. The quad of the circuit has a texture of a circuit in his top view and is static, while the quad of the car has a texture of a car in a top view too and is not static

    Circuit (example) http://caterhamf1.com/previews/2013/...g/TrackMap.png
    Car (example) http://img2.netcarshow.com/Ferrari-F...llpaper_27.jpg

    That more or less i know how to do it but, here's my questions:

    3. The car is like a "vector", if I press UP it goes in the positive direction of his vector, if I press DOWN goes in reverse direction and if I press RIGHT or LEFT the vector rotates a few grades and growing (something like the ++ in C++)

    4. Could I have a chase cam with it? So only a ceratin area will apear in the screen (where the car is, not the full map

    How I could do that? I'm not asking for a complete source code, just the way of how to do it

    Also OpenAL website does not work, there's other option apart of OpenAL


    Thank you very much!
    Last edited by Marc Colomé; 08-11-2013 at 02:52 PM.

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    474
    Quote Originally Posted by Marc Colomé View Post
    4. Could I have a chase cam with it? So only a ceratin area will apear in the screen (where the car is, not the full map
    glTranslatef(-car_x, -car_y, 0) before rendering the map.

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    27
    Thank you!

    Also how I could make to have 2 quads at the same time? I'm only able to have one

  4. #4
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    27
    anyone knows?

  5. #5
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Posts
    167
    Post your code. I'll try it out. If it works, I'll help you.

  6. #6
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    27
    //Is a modified code from videotutorialsrock.com, the best way i found for learning OGL!

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

    using namespace std;

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

    //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*/, 500.0/*Max distance*/);//Meters
    }

    float _angle = 30.0f;
    float _cameraAngle = 0.0f;

    //Draws the 3D scene
    void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f);
    glTranslatef(0.0f, 0.0f, -20.0f);//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



    glBegin(GL_QUADS); //Ferrari
    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();




    glBegin(GL_QUADS); //Circuit (A big parking
    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();


    glutSwapBuffers();
    }

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

    glutPostRedisplay();
    glutTimerFunc(25, 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(25, update, 0); //Add a timer

    glutMainLoop();
    return 0;
    }





    What I need to do is rotate the car and then make it go foward in his direction.

    I can't do it
    Last edited by Marc Colomé; 08-16-2013 at 11:12 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
  •