Sign in to follow this  
SeCrEt-CoDe

Please I need your help in my project

Recommended Posts

Hi everybody

I was working on my very begining project and I have a problem with collision

the project is simple and similar to bomber man game

I used let's say my own algorithm to construct the 3d world

please take a look at the code and help me

I user VC++ 2010

thanks a lot in advance

//begining of Code:::

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <gl\GLAUX.h>
#include <math.h>

static int shoulder = 0, elbow = 0;

GLfloat xrot; // X Rotation ( NEW )
GLfloat yrot; // Y Rotation ( NEW )
GLfloat zrot; // Z Rotation ( NEW )

HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application


GLuint texture[1];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc



AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r");

if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}

int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("Data/wall.bmp"))
{
Status=TRUE; // Set The Status To TRUE
glGenTextures(1, &texture[0]); // Create The Texture

// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);
// Generate The Texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
}

if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}

free(TextureImage[0]); // Free The Image Structure
}

return Status; // Return The Status
}


int init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel (GL_FLAT);

if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
{
return FALSE; // If Texture Didn't Load Return FALSE ( NEW )
}

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_TEXTURE_2D);
return TRUE;
}

void drawunitcube(float posx,float posy,float posz,float sizex,float sizey,float sizez,float rotangle,float rotx,float roty,float rotz)
{
glColor3f(1.0,1.0,1.0);

glTranslatef (posx,posy,posz);

glRotatef (rotangle,rotx,roty,rotz);

glBindTexture(GL_TEXTURE_2D, texture[0]);

glBegin(GL_QUADS);

//bottom face
//glColor3f(1.0,0.0,0.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f (-0.5,-0.5,-0.5);
glTexCoord2f(1.0f, 0.0f); glVertex3f (-0.5 + sizex,-0.5,-0.5);
glTexCoord2f(1.0f, 1.0f); glVertex3f (-0.5 + sizex,-0.5,-0.5 + sizez);
glTexCoord2f(0.0f, 1.0f); glVertex3f (-0.5,-0.5,-0.5 + sizez);

//Top face
//glColor3f(1.0,0.0,0.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f (-0.5,-0.5+sizey,-0.5);
glTexCoord2f(1.0f, 0.0f); glVertex3f (-0.5 + sizex,-0.5+sizey,-0.5);
glTexCoord2f(1.0f, 1.0f); glVertex3f (-0.5+sizex,-0.5+sizey,-0.5+sizez);
glTexCoord2f(0.0f, 1.0f); glVertex3f (-0.5,-0.5+sizey,-0.5+sizez);

//Right face
//glColor3f(0.0,1.0,0.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f (-0.5 + sizex,-0.5,-0.5);
glTexCoord2f(1.0f, 0.0f); glVertex3f (-0.5 + sizex,-0.5,-0.5 + sizez);
glTexCoord2f(1.0f, 1.0f); glVertex3f (-0.5+sizex,-0.5+sizey,-0.5+sizez);
glTexCoord2f(0.0f, 1.0f); glVertex3f (-0.5 + sizex,-0.5+sizey,-0.5);

//Left face
//glColor3f(0.0,0.0,1.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f (-0.5,-0.5,-0.5 + sizez);
glTexCoord2f(1.0f, 0.0f); glVertex3f (-0.5,-0.5,-0.5);
glTexCoord2f(1.0f, 1.0f); glVertex3f (-0.5,-0.5+sizey,-0.5);
glTexCoord2f(0.0f, 1.0f); glVertex3f (-0.5,-0.5+sizey,-0.5+sizez);

//Back face
//glColor3f(0.5,0.5,0.5);
glTexCoord2f(0.0f, 0.0f); glVertex3f (-0.5 + sizex,-0.5,-0.5 + sizez);
glTexCoord2f(1.0f, 0.0f); glVertex3f (-0.5,-0.5,-0.5 + sizez);
glTexCoord2f(1.0f, 1.0f); glVertex3f (-0.5,-0.5+sizey,-0.5+sizez);
glTexCoord2f(0.0f, 1.0f); glVertex3f (-0.5+sizex,-0.5+sizey,-0.5+sizez);

//Front face
//glColor3f(0.5,0.5,0.5);
glTexCoord2f(0.0f, 0.0f); glVertex3f (-0.5,-0.5,-0.5);
glTexCoord2f(1.0f, 0.0f); glVertex3f (-0.5 + sizex,-0.5,-0.5);
glTexCoord2f(1.0f, 1.0f); glVertex3f (-0.5 + sizex,-0.5+sizey,-0.5);
glTexCoord2f(0.0f, 1.0f); glVertex3f (-0.5,-0.5+sizey,-0.5);

glEnd();


}

int maze [11][11] = {1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,1,0,0,0,0,1,
1,0,0,0,0,1,0,0,0,0,1,
1,0,0,1,1,1,0,0,0,0,1,
1,0,0,1,0,0,0,1,0,0,1,
1,0,0,1,1,0,0,1,0,0,1,
1,0,0,0,1,0,1,1,0,0,1,
1,0,0,0,1,0,1,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1};

float xp= 0.0,yp = 0.0, zp = 0.0;
float roty = 0 ;
float angle = 0;

struct Points
{
float x;
float y;
float z;
};


Points positions[441];

void DrawMap (int Matrix[][11] , int Size)
{
glPushMatrix ();
//glTranslatef( xp-1.0, yp -2.5 , zp);

//glTranslatef( -xp+1.0, -yp +2.5 , -zp);

glTranslatef (4.0,0.0,-1.0);


float z = -2.5;
float x = -2.5;

glTranslatef (x,0.0,z);
//glRotatef (angle,1.0,0.0,0.0);


for (int i = 0 ; i < Size ; i++)
{
x = -2.5;
for (int j = 0 ; j < Size ; j++)
{

if (j < Size-1 && Matrix[i][j]==1 && Matrix[i][j+1] == 1)
{
glPushMatrix ();
positions[j].x = x;
positions[j].z = z;
drawunitcube (x,0.0,z,0.50,1,0.25,0.0,0.0,0.0,0.0);
glPopMatrix ();
}

if (i < Size-1 && Matrix[i][j] == 1 && Matrix[i+1][j] == 1)
{
glPushMatrix ();
positions[j].x = x;
positions[j].z = z;
drawunitcube (x,0.0,z,0.25,1,0.50,0.0,0.0,0.0,0.0);
glPopMatrix ();
}


if (Matrix[i][j] == 1)
{
glPushMatrix ();
positions[j].x = x;
positions[j].z = z;
drawunitcube (x,0.0,z,0.25,1,0.25,0.0,0.0,0.0,0.0);
glPopMatrix ();
}
x += 0.5;

}

z += 0.5;

}

glPopMatrix ();

//glPopMatrix();
}



void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
//float roty = 360.0f - angle;

//gluLookAt (xp-1.0, yp -2.5 ,zp-5.0,xp -1.0, yp -2.5 ,zp -6.0,0.0,1.0,0.0);



glRotatef (35,1.0,0.0,0.0);

glTranslatef (0.0,0.0,1.5);
// glRotatef (roty,0.0,1.0,0.0);
DrawMap(maze , 11);



glPopMatrix ();

glPushMatrix ();
glRotatef (35,1.0,0.0,0.0);
glTranslatef (xp-1.0,0.0,zp-0.5);
glColor3f (1.0,0.0,0.0);

glutSolidSphere(0.25,100,100);
glPopMatrix ();

glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(35.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -3.0);
glOrtho (-1.3,3.5,0.0,5.0,0.0,-2.0);
}


void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 'd': /* s key rotates at shoulder */
xp = xp + 0.1;
glutPostRedisplay();
break;
case 'S':
angle = angle - 0.1;
glutPostRedisplay();
break;
case 's': /* e key rotates at elbow */

zp = zp + 0.1;
glutPostRedisplay();
break;

case 'w':

zp = zp - 0.1;
glutPostRedisplay();
break;

case 'a': /* s key rotates at shoulder */
xp = xp - 0.1;
glutPostRedisplay();
break;
case 'q': /* s key rotates at shoulder */
roty += 0.5;
glutPostRedisplay();
break;
case 'e': /* s key rotates at shoulder */
zp = 360.0f - zp;
glutPostRedisplay();
break;
default:
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

//End of Code:::

[Edited by - SeCrEt-CoDe on December 12, 2010 1:39:03 PM]

Share this post


Link to post
Share on other sites
Use the code tags PLEASE. Also, if you put all of this in one cpp file, try to split up things, have a separate gameloop which gets called every tick for example. Just a way of cleaning stuff up. Split up the backend that sets up your window from your actual game loop with all it's content.


Now please make it readable so we can help you ;) [code] tags would be a start.

Share this post


Link to post
Share on other sites
Thanks alot dude ...
I did the tag thing

and I thing that code is seperated by functions ... it's readable now

and I want to know particularly how can I track the positions of the walls (set of cubes) while moving an object in the maze

I can't link the array which has information about walls (0's and 1's) and real scene


thanks again

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this