2 Replies - 2387 Views - Last Post: 08 November 2007 - 02:21 PM Rate Topic: -----

#1 Chii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-October 07

Need help with opengl (landing on platforms)

Post icon  Posted 28 October 2007 - 11:15 AM

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)

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



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with opengl (landing on platforms)

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Need help with opengl (landing on platforms)

Posted 30 October 2007 - 07:43 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 821
  • View blog
  • Posts: 7,692
  • Joined: 05-May 05

Re: Need help with opengl (landing on platforms)

Posted 08 November 2007 - 02:21 PM

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: 08 November 2007 - 02:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1