Dec 22, 2011 at 2:53pm UTC
I've been on this problem of junction detection for a few days now. I am making a simple game with a square that moves around and when move it passed a junction with two keys held down it keeps moving in that direction and doesn't take the turn. my problem is quite hard to explain so I have uploaded the whole project to media fire encase the code isn't enough. Also if you see any major mistakes that I've made it would be appreciated if you could flag them up.
link: http://www.mediafire.com/?kuo7o1zzki467o7
The Code :
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <GL/glut.h>
using namespace std;
float Xpos;
float Ypos;
float NextXpos;
float NextYpos;
bool Xinside = 1;
bool Yinside = 1;
bool Xcollision;
bool Ycollision;
bool Xjunction = 0;
bool Yjunction = 0;
float Speed;
float Size = 10;
float BackColour[3];
float AvatarColour[3];
float WallColour[3];
int mi, mj, pi, pj, ix, iy, ci, cj;
int PropertiesArray[30];
bool MapArray[10][10];
bool* keyStates = new bool[256];
bool* keySpecialStates = new bool[246];
void LoadData(void)
{
ifstream Map;
Map.open("map_data.txt");
for (int x=0; x < 10; x++)
{
for (int y=0; y < 10; y++)
{
Map >> MapArray[y][x];
}
}
cout << "Map Sucessfully Loaded!\n";
Map.close();
ifstream Properties;
Properties.open("Properties.txt");
for (int i=0; i < 30; i++)
{
Properties >> PropertiesArray[i];
}
Speed = (PropertiesArray[0]*10)+PropertiesArray[1];
Xpos = (PropertiesArray[2]*10)-50;
Ypos = (PropertiesArray[3]*-10)+50;
BackColour[0] = PropertiesArray[4];
BackColour[1] = PropertiesArray[5];
BackColour[2] = PropertiesArray[6];
WallColour[0] = PropertiesArray[7];
WallColour[1] = PropertiesArray[8];
WallColour[2] = PropertiesArray[9];
AvatarColour[0] = PropertiesArray[10];
AvatarColour[1] = PropertiesArray[11];
AvatarColour[2] = PropertiesArray[12];
cout << "Properties Sucessfully Loaded!\n";
Properties.close();
}
void Collision_Detec(void)
{
Xcollision = 0;
Ycollision = 0;
for(int ci=0; ci < 10; ci++)
{
for(int cj=0; cj < 10; cj++)
{
if (MapArray[cj][ci] == 1)
{
if (Ypos < (((cj*10)-50)*-1)+10 && Ypos > (((cj*10)-50)*-1)-10)
{
if (Xpos <= (ci*10)-60 && NextXpos >= (ci*10)-60)
{
Xcollision = 1;
}
if (Xpos >= (ci*10)-40 && NextXpos <= (ci*10)-40)
{
Xcollision = 1;
}
}
if (Xpos > (ci*10)-60 && Xpos < (ci*10)-40)
{
if (Ypos <= (((cj*10)-50)*-1)-10 && NextYpos >= (((cj*10)-50)*-1)-10)
{
Ycollision = 1;
}
if (Ypos >= (((cj*10)-50)*-1)+10 && NextYpos <= (((cj*10)-50)*-1)+10)
{
Ycollision = 1;
}
}
}
}
}
}
void OutsideMap(void)
{
Xinside = 1;
Yinside = 1;
if (NextXpos > 40) Xinside = 0;
if (NextXpos < -50) Xinside = 0;
if (NextYpos > 50) Yinside = 0;
if (NextYpos < -40) Yinside = 0;
}
void Move(bool ma, int mb)
{
if (ma)
{
NextXpos = Xpos + Speed/50*mb;
int iNextXpos = floor (NextXpos);
OutsideMap();
if (Xinside)
{
Collision_Detec();
if (Xcollision == 0) Xpos = NextXpos;
else Xpos = floor((Xpos+0.5)/10)*10;
}
else
{
Xpos = floor((Xpos+0.5)/10)*10;
}
}
else
{
NextYpos = Ypos + Speed/50*mb;
int iNextYpos = floor (NextYpos);
OutsideMap();
if (Yinside)
{
Collision_Detec();
if (Ycollision == 0 && Yjunction == 0) Ypos = NextYpos;
else Ypos = floor((Ypos+0.5)/10)*10;
}
else
{
Ypos = floor((Ypos+0.5)/10)*10;
}
}
glutPostRedisplay();
}
void Background(void)
{
glColor3f(BackColour[0], BackColour[1], BackColour[2]);
glRectf(-50, -50, 50, 50);
for (ix = 0; ix < 10; ix ++)
{
for (iy = 0; iy < 10; iy ++)
{
if (MapArray[ix][iy] == true)
{
glColor3f(WallColour[0], WallColour[1], WallColour[2]);
glRectf(iy*10-40, -ix*10+40, iy*10-50, -ix*10+50);
glutPostRedisplay();
}
}
}
}
void Avatar(void)
{
glColor3f(AvatarColour[0], AvatarColour[1], AvatarColour[2]);
glRectf(Xpos, Ypos, Xpos + Size, Ypos - Size);
}
void keyOperations (void)
{
if (keyStates['a'] == false) Move(1, -1);
if (keyStates['d'] == false) Move(1, 1);
if (keyStates['w'] == false) Move(0, 1);
if (keyStates['s'] == false) Move(0, -1);
glutPostRedisplay();
}
void keySpecialOperations(void)
{
if (keySpecialStates[GLUT_KEY_LEFT] == false) Move(1, -1);
if (keySpecialStates[GLUT_KEY_RIGHT] == false) Move(1, 1);
if (keySpecialStates[GLUT_KEY_UP] == false) Move(0, 1);
if (keySpecialStates[GLUT_KEY_DOWN] == false) Move(0, -1);
glutPostRedisplay();
}
void display(void)
{
keyOperations();
keySpecialOperations();
glClear(GL_COLOR_BUFFER_BIT);
Background();
Avatar();
glutSwapBuffers();
}
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 keyboard_keys_pressed(unsigned char key, int x, int y)
{
keyStates[key] = false;
}
void keyboard_keys_released(unsigned char key, int x, int y)
{
keyStates[key] = true;
}
void keyboard_arrowkeys_pressed(int arrowkey, int x, int y)
{
keySpecialStates[arrowkey] = false;
}
void keyboard_arrowkeys_released(int arrowkey, int x, int y)
{
keySpecialStates[arrowkey] = true;
}
int main(int argc, char** argv)
{
HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_HIDE );
LoadData();
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Rowan's Square");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard_keys_pressed);
glutKeyboardUpFunc(keyboard_keys_released);
glutSpecialFunc(keyboard_arrowkeys_pressed);
glutSpecialUpFunc(keyboard_arrowkeys_released);
glutSetKeyRepeat(GLUT_KEY_REPEAT_ON);
glutMainLoop();
return 0;
}