Original Post
hi, I'm trying to create a very simple 3D pong game...this is the first time im programming so im having quite a few probs with the collision dectection...for now,ive got 5 walls set up,and a ball and a bat..and the user kinda has to play against the wall like squash..i have a level.cpp,bat.cpp,ball.cpp,and a main program...coming to the problem,the ball actually collides far away from the wall or bat,and keeps moving in mid air!..so theres something going wrong with the math somewhere...someone plz help me out with this..ive been stick for a week...pasting my level.cpp,bat.cpp and collision detection function in the main below...thnx a lot! LEVEL.CPP(the room) #include "Level.h" using namespace std; using namespace GraphicsLib; Level::Level(GraphicsLib::Point3 center, GLfloat W, GLfloat D) { Center=center; Width=W; Depth=D; verts[0].x=Center.x-(Width/2.0f); verts[0].y=Center.y-(Width/2.0f); verts[0].z=Center.z+(Depth/2.0f); verts[1].x=Center.x+(Width/2.0f); verts[1].y=Center.y+(Width/2.0f); verts[1].z=Center.z+(Depth/2.0f); verts[2].x=Center.x+(Width/2.0f); verts[2].y=Center.y+(Width/2.0f); verts[2].z=Center.z-(Depth/2.0f); verts[3].x=Center.x-(Width/2.0f); verts[3].y=Center.y-(Width/2.0f); verts[3].z=Center.z-(Depth/2.0f); } Level::Level() { //Center.set(0,0,0); Width=10; Depth=10; verts[0].x=Center.x-(Width/2.0f); verts[0].y=Center.y-(Width/2.0f); verts[0].z=Center.z+(Depth/2.0f); verts[1].x=Center.x+(Width/2.0f); verts[1].y=Center.y+(Width/2.0f); verts[1].z=Center.z+(Depth/2.0f); verts[2].x=Center.x+(Width/2.0f); verts[2].y=Center.y+(Width/2.0f); verts[2].z=Center.z-(Depth/2.0f); verts[3].x=Center.x-(Width/2.0f); verts[3].y=Center.y-(Width/2.0f); verts[3].z=Center.z-(Depth/2.0f); } Level::~Level() { // do nothing for now } void Level::Draw(void) { //glDisable(GL_LIGHTING); //floor glPushMatrix(); Normal.set(0.0f,1.0f,0.0f); //glRotatef(90,1,0,0); glColor3f(1,1,1); Center.Translate(); glRotatef(-45,0,0,1); glScalef(4,4,9); glBegin(GL_QUADS); Normal.Normal(); for(int i=0; i<4; i++) { verts.Vertex(); } glEnd(); glBegin(GL_LINES); glColor3f(1,1,1); Center.Vertex(); //Normal.Vertex(); glVertex3f(Normal.x*4.0,Normal.y*4.0,Normal.z*4.0); glEnd(); //glEnable(GL_LIGHT1); Normal=CalcNormal(verts[3],verts[2],verts[1]); glPopMatrix(); //celing glPushMatrix(); Normal.set(0.0f,-1.0f,0.0f); //Normal=-CalcNormal(verts[3],verts[2],verts[1]); glColor3f(1,1,1); Center.Translate(); glTranslatef(5,40,0); glRotatef(-45,0,0,1); glScalef(4,4,9); glBegin(GL_QUADS); Normal.Normal(); for(int i=0; i<4; i++) { verts.Vertex(); } glEnd(); glBegin(GL_LINES); glColor3f(1,1,1); Center.Vertex(); //Normal.Vertex(); glVertex3f(Normal.x*4.0,Normal.y*4.0,Normal.z*4.0); glEnd(); //glEnable(GL_LIGHT2); glPopMatrix(); //left wall glPushMatrix(); Normal.set(0.0f,-1.0f,0.0f); glColor3f(1,1,1); Center.Translate(); glTranslatef(-25,20,0); glRotatef(45,0,0,1); glScalef(4,3,9); glBegin(GL_QUADS); Normal.Normal(); for(int i=0; i<4; i++) { verts.Vertex(); } glEnd(); glBegin(GL_LINES); glColor3f(1,1,1); Center.Vertex(); //Normal.Vertex(); glVertex3f(Normal.x*4.0,Normal.y*4.0,Normal.z*4.0); glEnd(); //glEnable(GL_LIGHT3); glPopMatrix(); //right wall glPushMatrix(); Normal.set(0.0f,1.0f,0.0f); glColor3f(1,1,1); Center.Translate(); glTranslatef(25,20,0); glRotatef(60,0,0,1); glScalef(4,3,9); glBegin(GL_QUADS); Normal.Normal(); glEnable(GL_LIGHTING); for(int i=0; i<4; i++) { verts.Vertex(); } glEnd(); glBegin(GL_LINES); glColor3f(1,1,1); Center.Vertex(); //Normal.Vertex(); glVertex3f(Normal.x*4.0,Normal.y*4.0,Normal.z*4.0); glEnd(); //glEnable(GL_LIGHT4); glPopMatrix(); //back wall glPushMatrix(); Normal.set(0.0f,-1.0f,0.0f); glColor3f(1,1,1); Center.Translate(); glTranslatef(10,25,-30); glRotatef(90,0,1,0); glRotatef(45,0,0,1); glScalef(4,4,7); glBegin(GL_QUADS); Normal.Normal(); for(int i=0; i<4; i++) { verts.Vertex(); } glEnd(); glBegin(GL_LINES); glColor3f(1,1,1); Center.Vertex(); //Normal.Vertex(); glVertex3f(Normal.x*4.0,Normal.y*4.0,Normal.z*4.0); glEnd(); //glEnable(GL_LIGHT5); //Normal=-CalcNormal(verts[3],verts[2],verts[1]); glPopMatrix(); //glEnable(GL_LIGHTING); } ****************************************************************************** #include "Level.h" using namespace std; using namespace GraphicsLib; Level::Level(GraphicsLib::Point3 center, GLfloat W, GLfloat D) { Center=center; Width=W; Depth=D; verts[0].x=Center.x-(Width/2.0f); verts[0].y=Center.y-(Width/2.0f); verts[0].z=Center.z+(Depth/2.0f); verts[1].x=Center.x+(Width/2.0f); verts[1].y=Center.y+(Width/2.0f); verts[1].z=Center.z+(Depth/2.0f); verts[2].x=Center.x+(Width/2.0f); verts[2].y=Center.y+(Width/2.0f); verts[2].z=Center.z-(Depth/2.0f); verts[3].x=Center.x-(Width/2.0f); verts[3].y=Center.y-(Width/2.0f); verts[3].z=Center.z-(Depth/2.0f); } Level::Level() { //Center.set(0,0,0); Width=10; Depth=10; verts[0].x=Center.x-(Width/2.0f); verts[0].y=Center.y-(Width/2.0f); verts[0].z=Center.z+(Depth/2.0f); verts[1].x=Center.x+(Width/2.0f); verts[1].y=Center.y+(Width/2.0f); verts[1].z=Center.z+(Depth/2.0f); verts[2].x=Center.x+(Width/2.0f); verts[2].y=Center.y+(Width/2.0f); verts[2].z=Center.z-(Depth/2.0f); verts[3].x=Center.x-(Width/2.0f); verts[3].y=Center.y-(Width/2.0f); verts[3].z=Center.z-(Depth/2.0f); } Level::~Level() { // do nothing for now } void Level::Draw(void) { //glDisable(GL_LIGHTING); //floor glPushMatrix(); Normal.set(0.0f,1.0f,0.0f); //glRotatef(90,1,0,0); glColor3f(1,1,1); Center.Translate(); glRotatef(-45,0,0,1); glScalef(4,4,9); glBegin(GL_QUADS); Normal.Normal(); for(int i=0; i<4; i++) { verts.Vertex(); } glEnd(); glBegin(GL_LINES); glColor3f(1,1,1); Center.Vertex(); //Normal.Vertex(); glVertex3f(Normal.x*4.0,Normal.y*4.0,Normal.z*4.0); glEnd(); //glEnable(GL_LIGHT1); Normal=CalcNormal(verts[3],verts[2],verts[1]); glPopMatrix(); //celing glPushMatrix(); Normal.set(0.0f,-1.0f,0.0f); //Normal=-CalcNormal(verts[3],verts[2],verts[1]); glColor3f(1,1,1); Center.Translate(); glTranslatef(5,40,0); glRotatef(-45,0,0,1); glScalef(4,4,9); glBegin(GL_QUADS); Normal.Normal(); for(int i=0; i<4; i++) { verts.Vertex(); } glEnd(); glBegin(GL_LINES); glColor3f(1,1,1); Center.Vertex(); //Normal.Vertex(); glVertex3f(Normal.x*4.0,Normal.y*4.0,Normal.z*4.0); glEnd(); //glEnable(GL_LIGHT2); glPopMatrix(); //left wall glPushMatrix(); Normal.set(0.0f,-1.0f,0.0f); glColor3f(1,1,1); Center.Translate(); glTranslatef(-25,20,0); glRotatef(45,0,0,1); glScalef(4,3,9); glBegin(GL_QUADS); Normal.Normal(); for(int i=0; i<4; i++) { verts.Vertex(); } glEnd(); glBegin(GL_LINES); glColor3f(1,1,1); Center.Vertex(); //Normal.Vertex(); glVertex3f(Normal.x*4.0,Normal.y*4.0,Normal.z*4.0); glEnd(); //glEnable(GL_LIGHT3); glPopMatrix(); //right wall glPushMatrix(); Normal.set(0.0f,1.0f,0.0f); glColor3f(1,1,1); Center.Translate(); glTranslatef(25,20,0); glRotatef(60,0,0,1); glScalef(4,3,9); glBegin(GL_QUADS); Normal.Normal(); glEnable(GL_LIGHTING); for(int i=0; i<4; i++) { verts.Vertex(); } glEnd(); glBegin(GL_LINES); glColor3f(1,1,1); Center.Vertex(); //Normal.Vertex(); glVertex3f(Normal.x*4.0,Normal.y*4.0,Normal.z*4.0); glEnd(); //glEnable(GL_LIGHT4); glPopMatrix(); //back wall glPushMatrix(); Normal.set(0.0f,-1.0f,0.0f); glColor3f(1,1,1); Center.Translate(); glTranslatef(10,25,-30); glRotatef(90,0,1,0); glRotatef(45,0,0,1); glScalef(4,4,7); glBegin(GL_QUADS); Normal.Normal(); for(int i=0; i<4; i++) { verts.Vertex(); } glEnd(); glBegin(GL_LINES); glColor3f(1,1,1); Center.Vertex(); //Normal.Vertex(); glVertex3f(Normal.x*4.0,Normal.y*4.0,Normal.z*4.0); glEnd(); //glEnable(GL_LIGHT5); //Normal=-CalcNormal(verts[3],verts[2],verts[1]); glPopMatrix(); //glEnable(GL_LIGHTING); } ******************************************************************************** COLLISION FUNCTIONS IN THE MAIN void SphereLevelCollide(void) { Vector p=sphere.Pos-sphere.NextPos; //p.normalize; GLfloat D; //p.set(sphere.NextPos.x,sphere.NextPos.y,sphere.NextPos.z); D=P1.Normal.dot(p); D+=sphere.Radius; if(D >=0.0) { if(sphere.Pos.x>P1.Center.x-(P1.Width/2.0) && sphere.Pos.x < P1.Center.x+(P1.Width/2.0) && sphere.Pos.y >P1.Center.y-(P1.Depth/2.0) && sphere.Pos.y < P1.Center.y+(P1.Depth/2.0) ) { cout <
=0) { cout<