Mar 21, 2014 at 5:53pm UTC
hey guys. So for my C++ module I chose to make a game using Object oriented design philosophy.
Everything has been fine so far however I'm stuck at collisions. I'm trying to use separation of axis to detect collisions. I understand the math behind it but I don't know how to implement it in C.
This is the code I'm using. It's just a colored box you can move with the directional keys.
Any and all help will greatly be appreciated.
#include <iostream>
#include <math.h>
#include <GL/glut.h>
#define PI 3.1416
float x_offset=0, y_offset=0;
//=======================================================
//============keyboardMain===============================
void keyboardmain(unsigned char key, int x, int y)
{
switch (key) {
case 27: //Escape key
exit(0);
}
}
//=======================================================
//============keyboardMain===============================
void keyboardcont(int key, int x, int y)
{
if (key== GLUT_KEY_UP)
{
y_offset += 0.05f;
}
if (key==GLUT_KEY_DOWN)
{
y_offset += -0.05f;
}
if (key==GLUT_KEY_RIGHT)
{
x_offset += 0.05f;
}
if (key==GLUT_KEY_LEFT)
{
x_offset += -0.05f;
}
}
//=======================================================
//============Window Resize==============================
void Resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)w/(float)h, 1.0, 200.0);
}
//=======================================================
//============Window Initialise==========================
void initialrendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
}
//=======================================================
//============Draw square================================
void drawsquare(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
//glTranslatef(-x_offset, -y_offset, 0.0f);
glPushMatrix();
glTranslatef(x_offset, y_offset, 0.0f) ;
glBegin(GL_QUADS);
glColor3f (0.0f, 0.0f, 1.0f); //Color
glVertex3f(1.0f, 1.0f, -10.0f); //
//
glColor3f (0.0f, 1.0f, 1.0f); //Color
glVertex3f(1.0f, 0.0f, -10.0f); //
//
glColor3f (1.0f, 0.0f, 1.0f); //Color
glVertex3f(0.0f, 0.0f, -10.0f); //
//
glColor3f (0.0f, 1.0f, 0.0f); //Color
glVertex3f(0.0f, 1.0f, -10.0f); //
//
glEnd();
glPopMatrix();
glEnd();
glutPostRedisplay();
glutSwapBuffers();
}
//===========================================================
//============Main function==================================
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(1080, 720);
glutCreateWindow("The Box that moved");
initialrendering();
glutDisplayFunc(drawsquare);
glutReshapeFunc(Resize);
glutKeyboardFunc(keyboardmain);
glutSpecialFunc (keyboardcont);
glutMainLoop();
return 0;
}
Last edited on Mar 21, 2014 at 5:54pm UTC