I am making a simple game demo to get me started on openGL but I do not know where to start when implementing collisions. I have looked on the internet to see if there is a function to do it for me but I cant seem to find it. Do I have to make my own collision sistem? If so where can I start. I am trying to make my player collide with the walls arround him.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#define or ||
#define and &&
int width=1000; //Screen width
int height=500; //Screen height
int px=100; //Player pos x
int py=100; //Player pos y
//-----------------------------MAP----------------------------------------------
#define mapX 8 //map width
#define mapY 8 //map height
#define mapS 64 //map cube size
int map[]={ //the map array. Edit to change level but keep the outer walls
1,1,1,1,1,1,1,1,
1,0,1,0,0,0,0,1,
1,0,1,0,0,0,0,1,
1,0,1,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,1,0,1,
1,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,
};
void drawPlayer(int px,int py){
glColor3f(1,1,0);
glPointSize(8);
glBegin(GL_POINTS);
glVertex2i(px,py);
glEnd();
}
void drawMap2D(){
int x,y,xo,yo;
for(y=0;y<mapY;y++){
for(x=0;x<mapX;x++){
if(map[y*mapX+x]==1){glColor3f(1,1,1);} else{glColor3f(0,0,0);}
xo=x*mapS; yo=y*mapS;
glBegin(GL_QUADS);
glVertex2i( 0 +xo, 0 +yo);
glVertex2i( 0 +xo, mapS+yo);
glVertex2i( mapS+xo, mapS+yo);
glVertex2i( mapS+xo, 0 +yo);
glEnd();
}
}
}
void Buttons(unsigned char key,int x,int y){
if(key=='a'){px-=5;}
if(key=='d'){px+=5;}
if(key=='w'){py-=5;}
if(key=='s'){py+=5;}
glutPostRedisplay();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//-----------------------Draw----------------------
drawMap2D();
drawPlayer(px,py);
//-------------------------------------------------
glutSwapBuffers();
}
void init(){
glClearColor(0.3,0.3,0.3,0);
gluOrtho2D(0,width,height,0);
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(width,height);
glutCreateWindow("OpenGL");
init();
glutDisplayFunc(display);
glutKeyboardFunc(Buttons);
glutMainLoop();
return 0;
}