PDA

View Full Version : Need a little help with this collision code



Glacier
03-21-2002, 01:44 PM
I have a sphere set at camera cords and have collision against a box and the sphere but for some reason I am always stuck in the middle of the box http://www.opengl.org/discussion_boards/ubb/smile.gif

I am using the COLDET collision library and a tga texture loader thats one reason this is so long. thanx
BKC

#include <windows.h>
#include "gl\glut.h"
#include <math.h>
#include "gl\gl.h"
#include <stdlib.h>
#include <stdio.h>
#include "tgaload.h"
#include "coldet.h"

#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

#define MAX_NO_TEXTURES 1

#define CUBE_TEXTURE 0

#define GROUND_TEXTURE 2

GLuint texture_id[MAX_NO_TEXTURES];

static GLint fogMode;

GLubyte rasters[24] = {
0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00,
0xff, 0x00, 0xff, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00,
0xff, 0xc0, 0xff, 0xc0};

static float turn = 0.0;
static float spin = 0.0;
static float angle=0.0,ratio;
static float x=10.0f,y=1.0f,z=10.0f;
static float lx=0.0f,ly=0.0f,lz=-1.0f;
static GLint box_display_list;
static GLint chr_display_list;

CollisionModel3D* model = newCollisionModel3D();
CollisionModel3D* model2 = newCollisionModel3D();

void render_string(void* font, const char* string)
{
char* p = (char*) string;
while (*p != '\0') glutBitmapCharacter(font, *p++);
}

void DrawNet(GLfloat size, GLint LinesX, GLint LinesZ)
{
glBegin(GL_LINES);
for (int xc = 0; xc < LinesX; xc++)
{
glVertex3f( -size / 2.0 + xc / (GLfloat)(LinesX-1)*size,
0.0,
size / 2.0);
glVertex3f( -size / 2.0 + xc / (GLfloat)(LinesX-1)*size,
0.0,
size / -2.0);
}
for (int zc = 0; zc < LinesX; zc++)
{
glVertex3f( size / 2.0,
0.0,
-size / 2.0 + zc / (GLfloat)(LinesZ-1)*size);
glVertex3f( size / -2.0,
0.0,
-size / 2.0 + zc / (GLfloat)(LinesZ-1)*size);
}
glEnd();
}


void changeSize(int w, int h)
{
if(h == 0)
h = 1;

ratio = 1.0f * w / h;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glViewport(0, 0, w, h);

gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}

void drawbox() {

model2->addTriangle(0.0f, 1.0f, 0.0f,-1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f);
model2->addTriangle(0.0f, 1.0f, 0.0f, 1.0f,-1.0f, 1.0f, 1.0f,-1.0f,-1.0f);
model2->addTriangle(0.0f, 1.0f, 0.0f, 1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f);
model2->addTriangle(0.0f, 1.0f, 0.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f, 1.0f);

model2->finalize ();

//glBindTexture ( GL_TEXTURE_2D, texture_id[2] );

//glBegin(GL_QUADS);
//glTexCoord2f(0.0f, 0.0f); glVertex3f(-10.0f, 0.0f, -10.0f);
//glTexCoord2f(1.0f, 0.0f); glVertex3f(-10.0f, 0.0f, 10.0f);
//glTexCoord2f(1.0f, 1.0f); glVertex3f( 10.0f, 0.0f, 10.0f);
//glTexCoord2f(0.0f, 1.0f); glVertex3f( 10.0f, 0.0f, -10.0f);
//glEnd();

glBindTexture ( GL_TEXTURE_2D, texture_id[0] );
glTranslatef(0.0,1.5,0.0);
glBegin ( GL_QUADS );
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();

char str[128];

glLoadIdentity();
glTranslatef(0.0f,0.0f,-1.0f);
sprintf(str, "Project1: Created by BANDED SOFTWARE", 0.0f);
glRasterPos2f(-0.25f, 0.20f);
render_string(GLUT_BITMAP_HELVETICA_18, str);
}

void drawsphere() {

model->addTriangle(0.0f, 1.0f, 0.0f,-1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f);
model->addTriangle(0.0f, 1.0f, 0.0f, 1.0f,-1.0f, 1.0f, 1.0f,-1.0f,-1.0f);
model->addTriangle(0.0f, 1.0f, 0.0f, 1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f);
model->addTriangle(0.0f, 1.0f, 0.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f, 1.0f);

model->finalize ();

glPushMatrix();
glTranslatef(x,y,z);
glutSolidSphere(1.0,20,20);
glPopMatrix();

float mat[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
model->setTransform (mat);

}

GLuint createDL() {
GLuint boxDL;

boxDL = glGenLists(1);

glNewList(boxDL,GL_COMPILE);

drawbox();

glEndList();

return(boxDL);
}

GLuint sphereDL() {
GLuint sphereDL;

sphereDL = glGenLists(1);

glNewList(sphereDL,GL_COMPILE);

drawsphere();

glEndList();

return(sphereDL);
}


void orientMe(float ang) {


lx = sin(ang);
lz = -cos(ang);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}


void moveMeFlat(int i) {
x = x + i*(lx)*0.1;
z = z + i*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}

void processNormalKeys(unsigned char key, int x, int y) {

if (key == 27)
exit(0);
}


void inputKey(int key, int x, int y) {

switch (key) {
case GLUT_KEY_LEFT : angle -= 0.04f;orientMe(angle);break;
case GLUT_KEY_RIGHT : angle +=0.04f;orientMe(angle);break;
case GLUT_KEY_UP : moveMeFlat(2);break;
case GLUT_KEY_DOWN : moveMeFlat(-2);break;
}
}

void initScene() {

GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 80.0 };
GLfloat light_position[] = { 0.0, 1.0, 0.0, 0.0 };

glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.5f, 0.5f, 0.0f);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glEnable(GL_FOG);
{
GLfloat fogColor[4] = {0.0, 0.4, 0.2, 1.0};

fogMode = GL_EXP;
glFogi (GL_FOG_MODE, fogMode);
glFogfv (GL_FOG_COLOR, fogColor);
glFogf (GL_FOG_DENSITY, 0.2);
glHint (GL_FOG_HINT, GL_DONT_CARE);
glFogf (GL_FOG_START, 2.0);
glFogf (GL_FOG_END, 5.0);
}


glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable ( GL_COLOR_MATERIAL );
glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );

glEnable ( GL_TEXTURE_2D );
glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
glGenTextures (1, texture_id);

image_t temp_image;

glBindTexture ( GL_TEXTURE_2D, texture_id[CUBE_TEXTURE] );
tgaLoad ( "metal.tga", &temp_image, TGA_FREE | TGA_LOW_QUALITY );
//glEnable ( GL_CULL_FACE );

glBindTexture ( GL_TEXTURE_2D, texture_id[GROUND_TEXTURE] );
tgaLoad ( "SWIRL.tga", &temp_image, TGA_FREE | TGA_LOW_QUALITY );

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
box_display_list = createDL();
chr_display_list = sphereDL();

}

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

GLfloat size = 12.0;
GLint LinesX = 15;
GLint LinesZ = 15;

GLfloat halfsize = size / 2.0;
glPushMatrix();
glTranslatef(0.0,-halfsize ,0.0);
DrawNet(size,LinesX,LinesZ);
glTranslatef(0.0,size,0.0);
DrawNet(size,LinesX,LinesZ);
glPopMatrix();
glPushMatrix();
glTranslatef(-halfsize,0.0,0.0);
glRotatef(90.0,0.0,0.0,halfsize);
DrawNet(size,LinesX,LinesZ);
glTranslatef(0.0,-size,0.0);
DrawNet(size,LinesX,LinesZ);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,0.0,-halfsize);
glRotatef(90.0,halfsize,0.0,0.0);
DrawNet(size,LinesX,LinesZ);
glTranslatef(0.0,size,0.0);
DrawNet(size,LinesX,LinesZ);
glPopMatrix();

GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glPushMatrix();
glRotated(spin, 1.0, 0.0, 0.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glPopMatrix();

glPushMatrix();
glCallList(box_display_list);;
glPopMatrix();
float mat2[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mat2);
model2->setTransform (mat2);

glPushMatrix();
glTranslatef(x,y,z);
glCallList(chr_display_list);;
glPopMatrix();

glutSwapBuffers();

spin+=0.5;

BOOL collision=model->collision (model2);
if (collision==TRUE)
{

x=0.0;
z=0.0;

}


}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(400,400);
glutCreateWindow (argv[0]);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(inputKey);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);

initScene();

glutMainLoop();

return(0);
}

Glacier
03-21-2002, 03:31 PM
if anyone is willing to help I can send you the code since on this forum you cannot upload files.

chxfryer
03-21-2002, 03:51 PM
On http://www.gametutorials.com there is collision detection tutorials. Even a camera/plane tutorial. Check it out, should help you

Glacier
03-21-2002, 04:43 PM
Yeah thoughs tutorials on gametutorials.com are great but there only for openGl not Glut.
But I fixed my problem. Just need to figure out how to stop movement. http://www.opengl.org/discussion_boards/ubb/smile.gif thanx
cya

richardve
03-21-2002, 09:46 PM
Originally posted by Glacier:
but there only for openGl not Glut.

OpenGL == (3D) Graphics Library (duh).
GLU == Higher level utilities for OpenGL.
GLUT == Utility Toolkit (duh) for use with OpenGL that wraps WGL/GLX/OS specific functions into a simple 'API'.
WGL/GLX == 'glue' between the windowing system and OpenGL.
GLAUX == Obsolete.

So what's make you think those tutorials are 'not for GLUT' ?

Glacier
03-22-2002, 03:58 AM
silly me, your right, I have took most of tutorials from nehe and gametutorials and worked them into glut applications. forgive me for sounding like such a noob. http://www.opengl.org/discussion_boards/ubb/smile.gif
I have only been using opengl for little over a week so I have a lot to learn . thanx
BKC