3 Replies - 13886 Views - Last Post: 05 May 2011 - 04:04 PM Rate Topic: -----

#1 Confuzedplusplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-May 11

C++/OpenGL/GLUT Game - How to implement Collision Detection and HUD

Posted 05 May 2011 - 10:15 AM

Hey guys. I'm "attempting" to make a game with C++, OpenGL and GLUT. It's a lander game and so far I've got it moving about on the screen the way I want it to. I'm moving onto collision detection(then the HUD) but I am a little confused on how to get it to work. Every time I think I have it done I compile and it doesn't work.

The way my code is laid out is I have my main.cpp file with all the main() function, display() function and things like keyboard input functions etc. Then I have my lander.cpp and lander.h files with the definition and implementation of my lander class. I've created two functions of type bool called IsLanded() and IsColliding()but to be honest I'm unsure what to put in these and what parameters whey should be taking to get my game to work. I'm not a very good programmer( even for a begginer) but I'm trying to understand(hence being here asking for help) so please keep the critisism constructive and focused on the code. As long as its about the code then critisize away, pick at anything you consider bad practice etc and suggest better ways of doing things. Its the only way i'll learn.

If anyone prefers seeing it in action rather than just reading the code from here then I can send you a link to my dropbox where you can download the full project(VC++ project file and all the files including bitmaps)

And thank you for taking the time to read this. I appreciate it


Main.cpp

#include"Bitmap.h"
#include<gl/glut.h>
#include "lander.h"
#include <iostream>
#include <cmath>
//#include "random.h"


const float gravity = -0.005;
const float engine_thrust = 0.010;
const float x_pad = 258;
const float y_pad = 459;
const float pad_length = 87;
#define windowWidth	800
#define windowHeight 600

bool left_key;
bool right_key;
bool up_key; 
float dx;
float dy;
bool IsLanded();

Bitmap *bitmap;
Bitmap *bitmap2 = NULL;
Lander *lander;

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    // Draw the background...
	bitmap->draw();	
	if(bitmap2)
		bitmap2->drawAt(0, 0);
	if(lander)
		lander->show();
	glFlush();
}

void init(){
	// Just a bitmap file, with transparency enabled (true)...
    bitmap = new Bitmap("background.bmp", false);
	bitmap2 = new Bitmap("ground.bmp", true);
	lander = new Lander("lander.bmp",400, 550, 0, 0);
}


void update()
{
	float x_thrust=0;
	float y_thrust=gravity;
	if(left_key){
		x_thrust = -0.1;
	}
	if (right_key){
		x_thrust += 0.1;
	}
	if(up_key){
		y_thrust += engine_thrust;
	}
	lander->update(x_thrust, y_thrust);

	
		



	glutPostRedisplay();
}

void timer(int value)
{
	update();
	glutTimerFunc(40, timer, 0);
}

void special_keys(int value, int x, int y)
{
	switch (value) {
		case GLUT_KEY_LEFT:
			left_key = true;
			dx += 0.8;			
			break;

		case GLUT_KEY_RIGHT:
			right_key = true;
			dx += -0.8;
			break;

		case GLUT_KEY_UP:
			 up_key = true;
			 dy += 0.8;
			 break;
			
			break;

		//case GLUT_KEY_ESC: 
			//end program
			//break;
	} 
}

void special_up_keys(int value, int x, int y)
{
	switch (value) {
		case GLUT_KEY_LEFT:
			left_key = false;
			break;

		case GLUT_KEY_RIGHT:
			right_key = false;
			break;

		case GLUT_KEY_UP:
			 up_key = false;
			 break;
		//case GLUT_KEY_ESC: 
			//end program
			//break;
	} 
}


bool IsLanded()
{

  
	float x;
	float y;
	
   if ( x+1 < x_pad || x > x_pad + 87 && y >  y_pad+1)
	   return false;
   else
	   // 
		
	   return true;
}


bool IsColliding(Bitmap, float Ground1)
{
   //I don't have a clue what goes here
}
	



int main(int argc,char** argv){
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
	glutInitWindowSize(windowWidth, windowHeight);
	glutCreateWindow("Mars Lander by Michael McGuire and Patrick Stevenson");
	glClearColor(1.0, 1.0, 0.0, 0.0);
	gluOrtho2D(0, windowWidth, 0, windowHeight);
	init();
	glutDisplayFunc(display);
	glutSpecialFunc(special_keys);
	glutSpecialUpFunc(special_up_keys);
	glutTimerFunc(0, timer, 0);
	glutMainLoop();
	delete bitmap;
	return 0;
}






Lander.h


#include "Bitmap.h"

class Lander
{
public:

	Lander::Lander (char* filename, float ix, float iy,float idx, float idy);

		
	void move();
	void show();
	void update(float dx, float dy);
	float get_Altitude();
	float set_Altitude();
	float get_Fuel();
	float set_Fuel();	
	
private:
	Bitmap *bitmap;

	float gravity, terminal_velocity, x,  y, fuel, altitude;
	float dx, dy;	

	
};

	
//float Lander::get_Altitude{ return altitude}
 



Lander.cpp

#include "lander.h"
#include <GL/glut.h>
#include "Bitmap.h"




Lander::Lander (char* filename, float ix, float iy, float idx, float idy)
{
	bitmap = new Bitmap(filename, true);
	x = ix; y = iy;
	dx=idx; dy=idy;
	show();
}






void Lander::move()
{ 
	x += dx;
	y += dy;
	show();
}

void Lander::show()
{
	bitmap->drawAt(x,y);
}

void Lander::update(float dx, float dy)
{
	this->dy += dy;
	this->dx += dx;
	move();
}




Is This A Good Question/Topic? 0
  • +

Replies To: C++/OpenGL/GLUT Game - How to implement Collision Detection and HUD

#2 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 251
  • Joined: 22-April 11

Re: C++/OpenGL/GLUT Game - How to implement Collision Detection and HUD

Posted 05 May 2011 - 01:30 PM

First critique I'm going to give is use more descriptive variable names. This will help you out a LOT in the long run. I'd rename bitmap to background, and bitmap2 to ground.

Collision is tricky, and it tends to differ depending on the situation. The issue you have here, is that your ground is irregular. A bounding box around your lander would probably suffice, but then you have to check if the box has any ground pixels in it.

What you might be able to do, is go through all the pixels in the bounding box on your ground bitmap. If any of those pixels aren't transparent (whatever you're using for the transparent color, perhaps 255,0,255), then there was a collision. The rest is up to you.
Was This Post Helpful? 0
  • +
  • -

#3 Confuzedplusplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-May 11

Re: C++/OpenGL/GLUT Game - How to implement Collision Detection and HUD

Posted 05 May 2011 - 03:58 PM

Thanks for the reply . I totally agree about the variable names, I'll change them. As for doing collision based on the pixels, I agree that would be better but I think it would also be harder to do. I'm finding it difficult trying to get bounding boxes to work, so I think that method would be beyond my capabilities at the moment.

T
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: C++/OpenGL/GLUT Game - How to implement Collision Detection and HUD

Posted 05 May 2011 - 04:04 PM

For your implementation it would suffice to have 'collision reference points' at the extremities of your model, such that if any of these points come into contact with your terrain a collision has taken place. A second option, depending on the dimensions of the model, it could simply be a function of the distance from the center of your model to the point of contact with the terrain. The key here is that you do not need a perfect solution.

Regular shapes like collision boxes/spheres are not suitable for determining collisions with terrain.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1