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();
}