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 3 of 3

Thread: OpenGL 2.0 C++ 2D game

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2017
    Posts
    1

    Question OpenGL 2.0 C++ 2D game

    I am using an old version of openGL with the opengl32 and GLu32 library. I am creating a 2D game that is very very simple. I am using win32 to manage the window and key inputs etc. Everything at the moment is inside the main as I do not want to complicate things at this moment, plus my c++ skills are very rusty. So far I have an actor(player) square that I can move, and a platform for that character to interact with. Am I right in thinking that in order to add gravity I should impliment collision resolution/detection first? My actor is denoted by:

    Code cpp:
    //actor
    glPushMatrix();
    glTranslatef(XActor, YActor, 0.0);
    glColor3f(0.678, 1.000, 0.184);
    glBegin(GL_POLYGON);
    glVertex2f(-19, -20);
    glVertex2f(-19, -17.75);
    glVertex2f(-16.75, -17.75);
    glVertex2f(-16.75, -20);
    glEnd();
     
    glPopMatrix();
     
    float xMovingMin = -19 + XActor;
    float xMovingMax = -16.75 + XActor;
    float yMovingMin = -20 + YActor;
    float yMovingMax = -17.75 + YActor;
     
     
    //test for overlap
    if (xMovingMin < xStaticMax &&
    xMovingMax > xStaticMin &&
    yMovingMin < yStaticMax &&
    yMovingMax > yStaticMin)
    {
    glColor3f(1.0, 1.0, 0.0); //AABBs are colliding
    }
    else
    glColor3f(1.0, 0.0, 1.0); //AABBs are not colliding
     
    glLineWidth(3.0);
    glBegin(GL_LINE_LOOP);
    glVertex2f(xMovingMin, yMovingMin);
    glVertex2f(xMovingMax, yMovingMin);
    glVertex2f(xMovingMax, yMovingMax);
    glVertex2f(xMovingMin, yMovingMax);
    glEnd();
    glLineWidth(1.0);

    The code above creates a polygon and then checks if the coordinates overlap with the platform. The Line loop code simply draws a box around my object which changes colour when I detect the platform. The platform is denoted by:

    Code cpp:
    //Platform 1
    glPushMatrix();
    glBegin(GL_POLYGON);
    glColor3f(0.686, 0.933, 0.933);
    glVertex2f(-14.0, -17.5);
    glVertex2f(-14.0, -17.0);
    glVertex2f(-4.0, -17.0);
    glVertex2f(-4.0, -17.5);
    glEnd();
    glPopMatrix();
     
    //extents of the static square - it isn't transformed
    float xStaticMin = -14.0;
    float xStaticMax = -4.0;
    float yStaticMin = -17.5;
    float yStaticMax = -17.0;

    The first problem I have is that when I add a second platform, say:

    Code cpp:
    //platform 2
    glPushMatrix();
    glBegin(GL_POLYGON);
    glColor3f(0.686, 0.933, 0.933);
    glVertex2f(-20, -12.5);
    glVertex2f(-20, -12.0);
    glVertex2f(-10, -12.0);
    glVertex2f(-10, -12.5);
    glEnd();
    glPopMatrix();

    I do not know how to factor in this platform with the first collision detection conditions. I thought of maybe adding in a 'XstaticMin2' and so on to the original '&' conditions but that did not work. I think I tried creating a new set of '&' conditions, copying and pasting the first collision detection but swapping out the coordinates for the new plaform but I don't think that worked either. I am also stuck on how to impliment collision resolution too, which is what i'm mainly asking for help with. Once I can do collision resolution for one platform I am sure I can figure out the rest! I am trying to keep it as basic and primative as possible. The addition of gravity would also be helpful if someone could tell me how to impliment such a thing(I know it is just a constant downward velocity but again my programming skills are inept.

    My second and final problem is getting the scene/camera to move with my character and making sure my character cannot go off screen(i.e detecting the screen paramaters) For the movement of the scene I read somewhere that the translation used to translate my player (glTranslatef(XActor, YActor, 0.0); can be place on top of the model view matrix. I am quite sure how to impliment that but if it is correct I would appreciate being shown. I have also placed my transformation above all my objects so that everything moves with my player, giving the illusion of a camera but instead it moves everything with my player so platforms follow my character around instead of staying where they are. So far my display is coded such as:

    Code cpp:
    void reshape(int width, int height)    // Resize the OpenGL window
    {
    screenWidth = width; screenHeight = height; // to ensure the mouse coordinates match 
    // we will use these values to set the coordinate system
     
    glViewport(0, 0, width, height);    // Reset The Current Viewport
     
    glMatrixMode(GL_PROJECTION);    // Select The Projection Matrix
    glLoadIdentity();    // Reset The Projection Matrix
     
    gluOrtho2D(-20, 20, -20, 20); // set the coordinate system for the window
     
     
    glMatrixMode(GL_MODELVIEW);    // Select The Modelview Matrix
    glLoadIdentity();    // Reset The Modelview Matrix
    }
    void init()
    {
    glClearColor(0.970, 0.970, 1.000, 0.0);    //sets the clear colour to yellow
    //glClear(GL_COLOR_BUFFER_BIT) in the display function
    //will clear the buffer to this colour.
    }

    The input keys that move the player are denoted by:

    Code cpp:
    void processKeys()
    {
    if (keys[VK_LEFT])
    {
    XActor -= 0.005f;
    }
    if (keys[VK_RIGHT])
    {
    XActor += 0.005f;
    }
    if (keys[VK_UP])
    {
    YActor += 0.005f;
    }
    if (keys[VK_DOWN])
    {
    YActor -= 0.005f;
    }
     
    }

    A final note is that I am also unsure on how to rescale the window without the aspect ration distorting. I think this is a simple fix but I am unsure.

    I completing this game without the use of external physics libraries, so any external libraries are not helpful, however I am open to changing to the freeGlut toolkit if nescessary.

    Thankyou in advance to anyone that offers help.
    Last edited by Dark Photon; 07-28-2017 at 06:55 PM.

  2. #2
    Intern Contributor
    Join Date
    Dec 2016
    Posts
    58
    If you are doing this in 2D and you want gravity, then all the platforms will be underneath the player, so you just need to test if the player's bottom hits the platform's top, then stop the movement downward at that point. (velocity will equal zero).

    An easy way to start would be to make all platforms the same height.

    Gravity is a constant acceleration down. So speed will increase constantly. So the player's velocity will be constantly decreasing while going up, then constantly increasing while falling down.

    I'm going to be working on an almost identical project today and the rest of this week, you can follow my channel here, although I am working in openframeworks, it's a wrapper for opengl, the collision detection and stuff should be the same, although I haven't looked at openframeworks to see if it has collision detection routines.... hopefully it does. lol

    https://www.youtube.com/channel/UCzx...ELz5h1dfCdkdfg

    My latest videos have dealt with 2D platforms, next I'm going to implement the character and background stuff, as well as, making more platforms, instead of just one screen full's worth.

    Regards,
    Jeff

  3. #3
    Newbie Newbie
    Join Date
    May 2015
    Posts
    2
    That isn't OpenGL 2.0, it's 1.x

Tags for this Thread

Posting Permissions

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