PDA

View Full Version : Collision Detection



DeclanMcgee11
04-09-2015, 07:29 AM
Hello I am trying to get a lunar lander type game made but i am having trouble getting the shape to hit off the sides of the window when it collides with them.

Any suggestions would be very helpful

LEVEL.CPP

#include "Level.h"
#include "Random.h"
#include <gl/glut.h>
#include <cmath>
#include <iostream>
using namespace std;


Apollo::Apollo(float X, float Y, float width, float height, float radius)
{
x = X;
y = Y;
w = width;
h = height;
r = radius;
}

void Apollo::show()
{
glBegin(GL_POLYGON);
glVertex2f(x - (w/4), y + (h/2));
glVertex2f(x + (w / 4), y + (h / 2));
glVertex2f(x + (w / 2), y);
glVertex2f(x + (w / 4), y - (h / 2));
glVertex2f(x - (w / 4), y - (h / 2));
glVertex2f(x - (w / 2), y);
glEnd();
}

//y is position, iy is movement, iiy is velocity. iiy keeps adding on to iy and then sum is added to y

void Apollo::gravity() //adds gravity
{
x += ix; // move horizontal
y += -iy; // gravity
iy += m; // to vertical movement

}

void Apollo::control(float iix, float iiy) //effect of gravity shown as velocity keeps increasing
{
ix += iix; // increment horizontal velocity
iy -= iiy; // increment vertical velocity
}


Ground::Ground(float X, float Y, float width, float height)
{
x = X;
y = Y;
w = width;
h = height;
}

void Ground::show() //draws ground
{
glBegin(GL_LINE_LOOP);
glVertex2f(x, y);
glVertex2f(x + w, y);
glEnd();
}

Apollo a(0, 0.75, 0.1, 0.1, 0);
Ground g(-1.0, -0.5, 2, 0);
int fuel = 500;

void WASD(unsigned char key, int x, int y)
{
if(fuel>0){
switch(key)
{
case 's': //Bottom thruster (move up)
a.control(0, 0.00002);
fuel = fuel - 2;
break;

case 'd': //Right side thruster (move left)
a.control(-0.00002,0);
fuel = fuel - 1;
break;

case 'a': //Left side thruster (move right)
a.control(0.00002,0);
fuel = fuel - 1;
break;
}
}
else
a.control(0,0); //if no fuel remaining
}

void ARROWS(int key, int x, int y)
{
if(fuel>0){
switch(key)
{
case GLUT_KEY_DOWN: //Bottom thruster (move up)
a.control(0, 0.00002);
fuel = fuel - 2;
break;

case GLUT_KEY_RIGHT: //Right side thruster (move left)
a.control(-0.00002, 0);
fuel = fuel - 1;
break;

case GLUT_KEY_LEFT: //Left side thruster (move right)
a.control(0.00002, 0);
fuel = fuel - 1;
break;
}
}
else
a.control(0,0); //if no fuel remaining
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 0);
a.show();
a.gravity();
a.wallCollisions();
g.show();
glFlush();
}


int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutCreateWindow("Lunar Lander");
glClearColor(1, 1, 1, 1);
glutKeyboardFunc(WASD);
glutDisplayFunc(display);
glutSpecialFunc(ARROWS);
glutIdleFunc(display);
glutMainLoop();
return 0;
}

LEVEL.H

#ifndef LEVEL_H
#define LEVEL_H


const float m = 0.000000016;
class Apollo
{
public:
Apollo(float X, float Y, float width, float height, float radius);
//virtual void create();
virtual void show();
//void move();
void control(float iix, float iiy);
void gravity();
void velocity(float X, float Y);
//void stop();
int fuel;
private:
//x,y,w,h,r are used to create the lander
//ix and iy are the landers movement
//iix and iiy are the velocity
float x, y, w, h, r, ix, iy, iix, iiy;
};
#endif

class Ground
{
public:
Ground(float X, float Y, float width, float height);
void show();
private:
float x, y, w, h;
};

RANDOM.CPP

#include<ctime>
#include<cstdlib>

// This function is used to seed the random umver so they are unpredictable
void start_random()
{
static bool seeded = false;
if (!seeded){
srand((unsigned)time(NULL));
seeded = true;
}
}

// Generate a random float in the range -1 to +1
float rnd(){
start_random();
return (-1) + (float)rand() / 16384;
}

// Generate a random float in the specified range
float rnd(float rangemin, float rangemax){
start_random();
return rangemin + (float)rand()*(rangemax - rangemin) / 32768.0;
}

// Generate a random integer in the specified range(0..range-1)
int rnd(int range){
start_random();
return rand() % range;
}

RANDOM.H

#ifndef RANDOM_H
#define RANDOM_H

float rnd();
float rnd(float rangemin, float rangemax);
int rnd(int range);

#endif