Welcome to Dream.In.Code
Become an Expert!

Join 150,185 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,073 people online right now. Registration is fast and FREE... Join Now!




Need help with opengl (landing on platforms)

 
Reply to this topicStart new topic

Need help with opengl (landing on platforms)

Chii
28 Oct, 2007 - 10:15 AM
Post #1

New D.I.C Head
*

Joined: 28 Oct, 2007
Posts: 1


My Contributions
Hi, i have started to make a basic 2d game in opengl and glut on visual studio. I am struggling to work out how to have my character land on platforms, currently he just jumps up and goes through all the platforms, this is the code i have at the moment. (ive taken out the bits which dont really have anything to do with the question)

CODE

/* orbits.cpp                     */

#include <iostream>
using namespace std;

#include <gl/glut.h>

const int width=500;
const int height=500;
const int xpos=100;
const int ypos=100;
GLfloat spin = 0.0;
GLfloat spin_incr = 2.0;
bool fall = true;
bool plat = false;
//variables for moving object
float Xtri = 0;        
float Ytri = 0;            //x and y coords of the user controlled object
float play = 0;

void init(void)
{
    glClearColor(0.5, 0.5, 1.0, 0.0);
}


void display(void)
{
    
    //draw floor
        glColor3f(0.5,1.0,0.5);
        glBegin(GL_POLYGON);
            glVertex2f(-50,-50);
            glVertex2f(50,-50);
            glVertex2f(50,-45);
            glVertex2f(-50,-45);
        glEnd();
        
        glPushMatrix();
        glTranslatef(Xtri, 0.0, 0.0);
        glColor3f(0.0,0.0,1.0);    
        glBegin(GL_POLYGON);
            glVertex2f(50,-50);
            glVertex2f(20,-50);
            glVertex2f(20,-45);
            glVertex2f(50,-45);
        glEnd();
        glPopMatrix();

    //draw platforms
        glPushMatrix();
        glTranslatef(Xtri, 0.0, 0.0);
        glColor3f(1.0,0.0,0.0);
        glBegin(GL_POLYGON);
            glVertex2f(-20,-35);
            glVertex2f(-40,-35);
            glVertex2f(-40,-40);
            glVertex2f(-20,-40);
        glEnd();

        glBegin(GL_POLYGON);
            glVertex2f(0,-25);
            glVertex2f(-20,-25);
            glVertex2f(-20,-30);
            glVertex2f(0,-30);
        glEnd();

        glBegin(GL_POLYGON);
            glVertex2f(20,-15);
            glVertex2f(0,-15);
            glVertex2f(0,-20);
            glVertex2f(20,-20);
        glEnd();        
        

        glBegin(GL_POLYGON);
            glVertex2f(70,-15);
            glVertex2f(50,-15);
            glVertex2f(50,-20);
            glVertex2f(70,-20);
        glEnd();
        glPopMatrix();

    //draw player
    glPushMatrix();
    glTranslatef(0.0, Ytri, 0.0);
    glTranslatef(-40, -41, 0.0);
        glBegin(GL_TRIANGLES);
            glColor3f(1.0,0.0,0.0);
            glVertex2f(-4,-4);
            glColor3f(0.0,1.0,0.0);
            glVertex2f(4,-4);
            glColor3f(0.0,0.0,1.0);
            glVertex2f(0,4);
        glEnd();
    glPopMatrix();

    glutSwapBuffers();
    


    if(Ytri > 0)
    {
        if(plat = false)
        {
        fall = true;
        }
    }

    if(plat = true)
    {
        Ytri++;
    }

    if(play >= 0)
    {
        if(play <= 20)
        {
        if(Ytri = 10)
        {
        plat = true;
        }
        }
    }

    if(plat = true)
    {
        fall = false;
    if(fall = true)
    {
    
        Ytri--;
        if(Ytri = 0)
    {
        fall = false;
    }
    }    
    }

}


void spinDisplay(void)
{
    spin += spin_incr;
    if(spin>360.0)spin-=360.0;

    glutPostRedisplay();
}

void reshape(int w, int h)
{
    glViewport(0,0,(GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


void keys(unsigned char key, int x, int y)
{
}

void special(int key, int x, int y)
{
    switch(key)
    {
    case GLUT_KEY_LEFT:
        Xtri++;
        Xtri++;
        play--;
        play--;
        break;
    case GLUT_KEY_UP:
        Ytri += 10;
        break;
    case GLUT_KEY_RIGHT:
        Xtri --;
        Xtri --;
        play++;
        play++;
        break;
    case GLUT_KEY_DOWN:
        //Ytri--;
        break;
    }
}


void main(int argc, char **argv)
{
    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(width, height);
    glutInitWindowPosition(xpos, ypos);

    glutCreateWindow("Spinning Rectangle");

    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutIdleFunc(spinDisplay);

    //add keyboard callback.
    glutKeyboardFunc(keys);
    //add callback for the function keys.
    glutSpecialFunc(special);

    glutMainLoop();
}

User is offlineProfile CardPM
+Quote Post

girasquid
RE: Need Help With Opengl (landing On Platforms)
30 Oct, 2007 - 06:43 PM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,295



Thanked: 18 times
Dream Kudos: 725
My Contributions
Your problem is collisions - are you detecting them?

You need to track the positions of both the character and the platforms - all the space the platforms should occupy, and all the space the character should occupy. Then, when the two spaces are overlapping, you treat it as a collision.

If the bottom of the character is touching the platform's top, for example, you'd mark it as a collision - and call whatever code you'd call to stop the player from falling any further.
User is offlineProfile CardPM
+Quote Post

WolfCoder
RE: Need Help With Opengl (landing On Platforms)
8 Nov, 2007 - 01:21 PM
Post #3

ギュウ~
Group Icon

Joined: 5 May, 2005
Posts: 3,723



Thanked: 8 times
Dream Kudos: 1450
My Contributions
Don't forget to sample all the way through for the character speed. If you're simply adding the speed to the current position of the character every frame, and if the character moves fast enough, you can fall through platforms that are too thin. You'll need to trace lines to see if the platform is preventing the character from falling any further no matter how fast your character is falling. Set the sample size to half the size of your smallest possible collision hull unit.

I would use 5 lines, 1 for the middle and 4 for the corners of the bounding box. I might also split the movements into three directions so that I always have straight lines (makes it much easier and faster). So I would check movement in the X, and then the Y, and finally in the Z direction with 5 straight lines for each. If it is slower than simply checking a cube for every unit until you reach the target destination for the next frame, then use the cube method.

So if you're character is going to travel a distance of 8 units and your smallest platform is 4 by 4 by 4 units, then every 2 units to the destination, check for a platform. This is simple bounding box collision but it can work if you don't need precise collision checking.

In my game, I don't need collision for the map movement since it's all old school grid style, but the battles are all active and play out with smooth XYZ movement. I'll use two rectangles to check for intersection with level geometry, one in the XY direction and another in the YZ direction. If both bounding boxes intersect in the XY direction, then I check the YZ direction. If those intersect too, then it's a collision and I cancel the movement. At high speeds the characters will bounce off the object and fall down on the floor, and it will happen too fast for the player to notice the frame before, the character was standing 100 units away, so don't worry about that. For every cubic unit in my game, it can hold 8 texel cubes inside (1 unit = 2 texels). Texels are pixels in three dimensions used for textures.

But! The smallest collision geometry I will use is 8 units by 8 units so I'll only sample 4 units in any direction. That's a 32x32 texture mapped perfectly to a cube, twice the normal size since I want to use tileset images of a little higher resolution. Each character is only 16x16 pixels for an old-school effect, but they will be twice the size of the textures.

This post has been edited by WolfCoder: 8 Nov, 2007 - 01:39 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:06AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month