Archived

This topic is now archived and is closed to further replies.

OleKaiwalker

Collision Detection

Recommended Posts

My upcoming collision detection isn''t working quite the way I want it... It recieves the distance between my point (isn''t a sphere yet) and the plane, on which the polygon is, well. But the detection dosn''t use my point... instead it uses the origin (0, 0, 0). A fast example -> My polygon has z-coord -> -10. My point has the z-coord -> -5. When I move the polygon to the point, it dosn''t say that it''s inside, but first when I move it polygon to the origin it says, that it''s inside... Hope you understand me... My Code-> CVector3 CrossFromVectors(CVector3 cVector, CVector3 cVector2){ CVector3 vNormal; vNormal.x = ((cVector.y * cVector2.z) - (cVector.z * cVector2.y)); vNormal.y = ((cVector.z * cVector2.x) - (cVector.x * cVector2.z)); vNormal.z = ((cVector.x * cVector2.y) - (cVector.y * cVector2.x)); return vNormal; } GLfloat DotProduct(CVector3 cVector, CVector3 cVector2){ return ((cVector.x * cVector2.x) + (cVector.y * cVector2.y) + (cVector.z * cVector2.z)); } CVector3 Normal(CVector3 pTriangle[]){ CVector3 vNormal; CVector3 cVector, cVector2; cVector.x = pTriangle[1].x - pTriangle[0].x; cVector.y = pTriangle[1].y - pTriangle[0].y; cVector.z = pTriangle[1].z - pTriangle[0].z; cVector2.x = pTriangle[2].x - pTriangle[0].x; cVector2.y = pTriangle[2].y - pTriangle[0].y; cVector2.z = pTriangle[2].z - pTriangle[0].z; vNormal = CrossFromVectors(cVector, cVector2); vNormal = NormalizeVector(vNormal); return vNormal; } GLfloat DistanceFromPlane(CVector3 pNormal, CVector3 pPoint, CVector3 vPoint){ GLfloat D = - (DotProduct(pNormal, pPoint)); GLfloat pDistance = ( (pNormal.x * vPoint.x) + (pNormal.y * vPoint.y) + (pNormal.z * vPoint.z) + D); return pDistance; } GLdouble AngleBetweenVectors(CVector3 cVector, CVector3 cVector2){ GLfloat dotProduct = DotProduct(cVector, cVector2); GLfloat vMagnitude = VectorMagnitude(cVector) * VectorMagnitude(cVector2); GLdouble vAngle = acos(dotProduct / vMagnitude); if (_isnan(vAngle)) return 0; return vAngle; } bool InsidePolygon(CVector3 pIntersection, CVector3 pVertices[], GLuint vertexCount){ const GLdouble floatMatch = 0.99f; GLdouble pAngle = 0.0f; CVector3 tVector, tVector2; for (GLuint i = 0; i < vertexCount; i++){ tVector = pVertices - pIntersection; tVector2 = pVertices[(i + 1) % vertexCount] - pIntersection; pAngle += AngleBetweenVectors(tVector, tVector2); } if (pAngle >= (floatMatch * (2.0f * 3.1415f))) return true; return false; } GLfloat Absolute(GLfloat tNum){ if (tNum < 0) return 0 - tNum; return tNum; } CVector3 pTriangle[3] = { CVector3(-1.0f, -1.0f, -15.0f), CVector3(1.0f, -1.0f, -15.0f), CVector3(0.0f, 1.0f, -15.0f) }; CVector3 vPoint = CVector3(0.0f, 0.0f, -6.0f); ////////// *** NEW *** ////////// GLvoid DrawGLScreen(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glDisable(GL_TEXTURE_2D); CVector3 pNormal = Normal(pTriangle); GLfloat pDistance = DistanceFromPlane(pNormal, pTriangle[0], vPoint); CVector3 pOffset = pNormal * pDistance; CVector3 pPosition = vPoint - pOffset; bool pInside = InsidePolygon(pPosition, pTriangle, 3); if (pInside){ glEnable(GL_TEXTURE_2D); gFont.glDrawText(0, 0, wOpenGL.winWidth, wOpenGL.winHeight, 0, 1, "Is Inside"); glDisable(GL_TEXTURE_2D); } glBegin(GL_TRIANGLE_STRIP); glNormal3fv(&pNormal.z); for (GLuint i = 0; i < sizeof(pTriangle); i++){ glColor3d(100, 100, 100); glVertex3i((GLint)pTriangle[i].x, (GLint)pTriangle[i].y, (GLint)pTriangle[i].z); } glEnd(); glEnable(GL_TEXTURE_2D); gFont.glDrawText( 0, (wOpenGL.winHeight - 22), wOpenGL.winWidth, wOpenGL.winHeight, 0, 1, "The Normal For The Polygon: %d, %d, %d", (GLint)pNormal.x, (GLint)pNormal.y, (GLint)pNormal.z); // Polygon Positions gFont.glDrawText( 0, (wOpenGL.winHeight - 44), wOpenGL.winWidth, wOpenGL.winHeight, 0, 1, "Polygon X-Pos: %d, %d, %d", (GLint)pTriangle[0].x, (GLint)pTriangle[1].x, (GLint)pTriangle[2].x); gFont.glDrawText( 0, (wOpenGL.winHeight - 66), wOpenGL.winWidth, wOpenGL.winHeight, 0, 1, "Polygon Y-Pos: %d, %d, %d", (GLint)pTriangle[0].y, (GLint)pTriangle[1].y, (GLint)pTriangle[2].y); gFont.glDrawText( 0, (wOpenGL.winHeight - 88), wOpenGL.winWidth, wOpenGL.winHeight, 0, 1, "Polygon Z-Pos: %d, %d, %d", (GLint)pTriangle[0].z, (GLint)pTriangle[1].z, (GLint)pTriangle[2].z); gFont.glDrawText( 0, (wOpenGL.winHeight - 110), wOpenGL.winWidth, wOpenGL.winHeight, 0, 1, "Distance From Plane - Written In Code: %2.1f", pDistance); gFont.glDrawText( 0, (wOpenGL.winHeight - 132), wOpenGL.winWidth, wOpenGL.winHeight, 0, 1, "Position: %d, %d, %d", (GLint)pPosition.x, (GLint)pPosition.y, (GLint)pPosition.z); SwapBuffers(wOpenGL.hDC); }

Share this post


Link to post
Share on other sites
What kind of intersection do you want?

Point-Poly, Sphere-Poly, Poly-Poly,...???

quote:

It recieves the distance between my point (isn''t a sphere yet) and the plane, on which the polygon is, well. But the detection dosn''t use my point... instead it uses the origin (0, 0, 0).



Aarghh...

Share this post


Link to post
Share on other sites
Read the Fluid Studios Article ("Generic Collision Detection for Games Using Ellipsoids") and Code("Software rendered demo with full source").
www.fluidStudios.com

Share this post


Link to post
Share on other sites