int CPhysEnv::CheckForCollisions( tParticle *system ) { // be optimistic! int collisionState = NOT_COLLIDING; float const depthEpsilon = 0.001f; int loop; tParticle *curParticle; m_ContactCnt = 0; // THERE ARE CURRENTLY NO CONTACTS curParticle = system; for (loop = 0; (loop < m_ParticleCnt) && (collisionState != PENETRATING); loop++,curParticle++) { // CHECK THE MAIN BOUNDARY PLANES FIRST for(int planeIndex = 0;(planeIndex < m_CollisionPlaneCnt) && (collisionState != PENETRATING);planeIndex++) { tCollisionPlane *plane = &m_CollisionPlane[planeIndex]; float axbyczd = DotProduct(&curParticle->pos,&plane->normal) + plane->d; if(axbyczd < -depthEpsilon) { // ONCE ANY PARTICLE PENETRATES, QUIT THE LOOP collisionState = PENETRATING; } else if(axbyczd < depthEpsilon) { float relativeVelocity = DotProduct(&plane->normal,&curParticle->v); if(relativeVelocity < 0.0f) { collisionState = COLLIDING; m_Contact[m_ContactCnt].particle = loop; memcpy(&m_Contact[m_ContactCnt].normal,&plane->normal,sizeof(tVector)); m_ContactCnt++; } } } if (m_CollisionActive) { // THIS IS NEW FROM MARCH - ADDED SPHERE BOUNDARIES // CHECK ANY SPHERE BOUNDARIES for(int sphereIndex = 0;(sphereIndex < m_SphereCnt) && (collisionState != PENETRATING);sphereIndex++) { tCollisionSphere *sphere = &m_Sphere[sphereIndex]; tVector distVect; VectorDifference(&curParticle->pos, &sphere->pos, &distVect); float radius = VectorSquaredLength(&distVect); // SINCE IT IS TESTING THE SQUARED DISTANCE, SQUARE THE RADIUS ALSO float dist = radius - (sphere->radius * sphere->radius); if(dist < -depthEpsilon) { // ONCE ANY PARTICLE PENETRATES, QUIT THE LOOP collisionState = PENETRATING; } else if(dist < depthEpsilon) { // NORMALIZE THE VECTOR NormalizeVector(&distVect); float relativeVelocity = DotProduct(&distVect,&curParticle->v); if(relativeVelocity < 0.0f) { collisionState = COLLIDING; m_Contact[m_ContactCnt].particle = loop; memcpy(&m_Contact[m_ContactCnt].normal,&distVect,sizeof(tVector)); m_ContactCnt++; } } } } } return collisionState; }
/* returns length of input vector */ double VectorLength(vertex *v) { return(sqrt(VectorSquaredLength(v))); }