   GameTutorial's Collision Det.
  Hallo everybody, i have a problem with GameTut.Colli.Det.Im working at shcool with c+++opengl (pc:600Mz and 16mb grafik ).problem: this Collision slow down my app to one fps, without this colli. i have 60 fps,,, well i need to know how to make this colli. better and faster.. plz help me guysHERE IS THIS COLLISIONvoid CPlayer::CheckCollision(CVector3 *pVertices, int numOfVerts, int radius){for(int i = 0; i < numOfVerts; i += 3){CVector3 vTriangle[3] = { pVertices, pVertices[i+1], pVertices[i+2] };CVector3 vNormal = Normal(vTriangle);float distance = 0.0f;int classification = ClassifySphere(m_vPosition, vNormal, vTriangle[0], radius, distance);if(classification == INTERSECTS) {CVector3 vOffset = vNormal * distance;CVector3 vIntersection = m_vPosition - vOffset;if(InsidePolygon(vIntersection, vTriangle, 3) ||   EdgeSphereCollision(m_vPosition, vTriangle, 3, radius / 2)){vOffset = GetCollisionOffset(vNormal, radius, distance);m_vPosition = m_vPosition + vOffset;m_vView = m_vView + vOffset;}}}}may be it because im doind this colli 36 times every frame, but i have to do that because if my player moving his self with speed greater as colli radius .. then the colli. will dont workt.[edited by - syih on February 18, 2004 2:29:25 AM] 
  36 collision tests, brings your machine to 1 fps?!? even on a 600mhz, I would think this would be a peice of cake for the processor. I've got another algorithm that you can try,  //-------------------------------------------------------------------// Calculate the squared distance of a point to the triangle//// Nicked code from David Eberly's most excelent physics site// Don;t ask me how it works. It just does :)// (http://www.magic-software.com)// pfSParam and pfTParams are the barycentric coordinate of the // closest point on the triangle to the point.// that closest point will then be// Ptri = V[0] + s * (V[1] - V[0]) + t * (V[2] - V[0])//-------------------------------------------------------------------float CTriangle::DistanceSquared(const Vector& P, const Vector* V, float* pfSParam, float* pfTParam) const{//-------------------------------------------------------------------// 2 edges of the triangle//-------------------------------------------------------------------Vector E0 = (V[1] - V[0]);Vector E1 = (V[2] - V[0]);    Vector kDiff = V[0] - P;    float fA00 = E0 * E0;    float fA01 = E0 * E1;    float fA11 = E1 * E1;    float fB0  = kDiff * E0;float fB1  = kDiff * E1;float fC   = kDiff * kDiff;    float fDet = (float) fabs(fA00*fA11-fA01*fA01);    float fS   = fA01*fB1-fA11*fB0;    float fT   = fA01*fB0-fA00*fB1;    float fSqrDist;if (fabs(fDet) < 0.00000001f)return 100000000.0f;    if ( fS + fT <= fDet )    {   if ( fS < (float)0.0 )   {  if ( fT < (float)0.0 )  // region 4  { if ( fB0 < (float)0.0 ) {fT = (float)0.0;if ( -fB0 >= fA00 ){    fS = (float)1.0;    fSqrDist = fA00+((float)2.0)*fB0+fC;}else{    fS = -fB0/fA00;    fSqrDist = fB0*fS+fC;} } else {fS = (float)0.0;if ( fB1 >= (float)0.0 ){    fT = (float)0.0;    fSqrDist = fC;}else if ( -fB1 >= fA11 ){    fT = (float)1.0;    fSqrDist = fA11+((float)2.0)*fB1+fC;}else{    fT = -fB1/fA11;    fSqrDist = fB1*fT+fC;} }  }  else  // region 3  { fS = (float)0.0; if ( fB1 >= (float)0.0 ) {fT = (float)0.0;fSqrDist = fC; } else if ( -fB1 >= fA11 ) {fT = (float)1.0;fSqrDist = fA11+((float)2.0)*fB1+fC; } else {fT = -fB1/fA11;fSqrDist = fB1*fT+fC; }  }   }   else if ( fT < (float)0.0 )  // region 5   {  fT = (float)0.0;  if ( fB0 >= (float)0.0 )  { fS = (float)0.0; fSqrDist = fC;  }  else if ( -fB0 >= fA00 )  { fS = (float)1.0; fSqrDist = fA00+((float)2.0)*fB0+fC;  }  else  { fS = -fB0/fA00; fSqrDist = fB0*fS+fC;  }   }   else  // region 0   {  // minimum at interior point  float fInvDet = ((float)1.0)/fDet;  fS *= fInvDet;  fT *= fInvDet;  fSqrDist = fS*(fA00*fS+fA01*fT+((float)2.0)*fB0) + fT*(fA01*fS+fA11*fT+((float)2.0)*fB1)+fC;   }    }    else    {   float fTmp0, fTmp1, fNumer, fDenom;   if ( fS < (float)0.0 )  // region 2   {  fTmp0 = fA01 + fB0;  fTmp1 = fA11 + fB1;  if ( fTmp1 > fTmp0 )  { fNumer = fTmp1 - fTmp0; fDenom = fA00-2.0f*fA01+fA11; if ( fNumer >= fDenom ) {fS = (float)1.0;fT = (float)0.0;fSqrDist = fA00+((float)2.0)*fB0+fC; } else {fS = fNumer/fDenom;fT = (float)1.0 - fS;fSqrDist = fS*(fA00*fS+fA01*fT+2.0f*fB0) +    fT*(fA01*fS+fA11*fT+((float)2.0)*fB1)+fC; }  }  else  { fS = (float)0.0; if ( fTmp1 <= (float)0.0 ) {fT = (float)1.0;fSqrDist = fA11+((float)2.0)*fB1+fC; } else if ( fB1 >= (float)0.0 ) {fT = (float)0.0;fSqrDist = fC; } else {fT = -fB1/fA11;fSqrDist = fB1*fT+fC; }  }   }   else if ( fT < (float)0.0 )  // region 6   {  fTmp0 = fA01 + fB1;  fTmp1 = fA00 + fB0;  if ( fTmp1 > fTmp0 )  { fNumer = fTmp1 - fTmp0; fDenom = fA00-((float)2.0)*fA01+fA11; if ( fNumer >= fDenom ) {fT = (float)1.0;fS = (float)0.0;fSqrDist = fA11+((float)2.0)*fB1+fC; } else {fT = fNumer/fDenom;fS = (float)1.0 - fT;fSqrDist = fS*(fA00*fS+fA01*fT+((float)2.0)*fB0) +    fT*(fA01*fS+fA11*fT+((float)2.0)*fB1)+fC; }  }  else  { fT = (float)0.0; if ( fTmp1 <= (float)0.0 ) {fS = (float)1.0;fSqrDist = fA00+((float)2.0)*fB0+fC; } else if ( fB0 >= (float)0.0 ) {fS = (float)0.0;fSqrDist = fC; } else {fS = -fB0/fA00;fSqrDist = fB0*fS+fC; }  }   }   else  // region 1   {  fNumer = fA11 + fB1 - fA01 - fB0;  if ( fNumer <= (float)0.0 )  { fS = (float)0.0; fT = (float)1.0; fSqrDist = fA11+((float)2.0)*fB1+fC;  }  else  { fDenom = fA00-2.0f*fA01+fA11; if ( fNumer >= fDenom ) {fS = (float)1.0;fT = (float)0.0;fSqrDist = fA00+((float)2.0)*fB0+fC; } else {fS = fNumer/fDenom;fT = (float)1.0 - fS;fSqrDist = fS*(fA00*fS+fA01*fT+((float)2.0)*fB0) +    fT*(fA01*fS+fA11*fT+((float)2.0)*fB1)+fC; }  }   }    }    if ( pfSParam )   *pfSParam = fS;    if ( pfTParam )   *pfTParam = fT;    return (float) fabs(fSqrDist);}I can do 7000+ tests like this one on an athlon 1700 @1.4ghz at 20 fps, and that's with shitty OpenGL rendering, no space partioning, and with unoptimised code. So I should think yours would be fast enough. Especially since that distance calculation is called, without any quick exit from the algo, so it's slow. but, ... Am I reading this right? you test 36 times per frame, means 36 times the whole collision mesh? or just 36 triangles once per frame?if you are worried that your player will miss collisions while moving too fast, either split the movement to segments of 1/2 of the player radius, or do a swept test.   [edited by - oliii on February 18, 2004 10:02:50 AM]