bool CollisionDetection::collideEntityWithObj(Entity *e) { if( objCollision != NULL) { NewtonCollision *eCollision; std::map<Entity *,NewtonCollision *>::const_iterator iter = collisionsMap.find(e); if(iter != collisionsMap.end()) { eCollision=iter->second; } else { cout << "in collideEntityWithObj(e), collision mesh for enitiy e could not be found in (cpp)map" << endl; return false; } dFloat eMatrix[16]; getMatrix(e,eMatrix, true); dFloat contacts[16]; dFloat normals[16]; dFloat penetration[16]; int numCollisionPoints = NewtonCollisionCollide (newtonWorld, 1, eCollision, &eMatrix[0], objCollision, &idmatrix[0], &contacts[0], &normals[0], &penetration[0], 0); if (numCollisionPoints > 0) return true; else return false; } else { cout << "Objective collision mesh is NULL (most probably not initialised)" << endl; return false; } }
int NewtonBodyCollide(NewtonWorld* world, int maxsize, NewtonBody* body0, NewtonBody* body1, float* contacts, float* normals, float* penetration) { NewtonCollision* collision[2]; float mat0[16]; float mat1[16]; collision[0]=NewtonBodyGetCollision(body0); collision[1]=NewtonBodyGetCollision(body1); NewtonBodyGetMatrix(body0,mat0); NewtonBodyGetMatrix(body1,mat1); return NewtonCollisionCollide(world,maxsize,collision[0],mat0,collision[1],mat1,contacts,normals,penetration,0); }
//only to be used with map pieces where the second element is not transformed Collision CollisionDetection::staicAndDynamicCollision(Entity *e1, Entity *e2, bool dynamic) { dFloat e1Matrix[16]; dFloat e2Matrix[16]; NewtonCollision *e1Collision; NewtonCollision *e2Collision; std::map<Entity *,NewtonCollision *>::const_iterator iter = collisionsMap.find(e1); if(iter != collisionsMap.end()) { e1Collision=iter->second; } else { cout<<"in mapCollision(e1,e2) the collision mesh for enitiy e1 could not be found in (C++)map"<< endl; dFloat contacts[16] = {0.0f}; dFloat normals[16] = {0.0f}; dFloat penetration[16] = {0.0f}; return Collision(false,normals,contacts,penetration); } iter = collisionsMap.find(e2); if(iter != collisionsMap.end()) { e2Collision=iter->second; } else { cout<<"in mapCollision(e1,e2) collision mesh for mapEnitiy e2 could not be found in (C++)map"<<endl; dFloat contacts[16] = {0.0f}; dFloat normals[16] = {0.0f}; dFloat penetration[16] = {0.0f}; return Collision(false,normals,contacts,penetration);; } getMatrix(e1,e1Matrix, true); //allwarys transform getMatrix(e2,e2Matrix, dynamic); //transfrom second entity only if not static dFloat contacts[16]; dFloat normals[16]; dFloat penetration[16]; int numCollisionPoints = NewtonCollisionCollide (newtonWorld, 1, e1Collision, &e1Matrix[0], e2Collision, &e2Matrix[0], &contacts[0], &normals[0], &penetration[0], 0); if (numCollisionPoints > 0) { return Collision(true,normals,contacts,penetration); } else { return Collision(false,normals,contacts,penetration); } }
static int CalculateContacts (const NewtonBody* const otherBody, void* const userData) { ShowCollisionCollide* const me = (ShowCollisionCollide*)userData; if (me->m_body != otherBody) { const int cMaxContacts = 15; dFloat contacts[cMaxContacts][3]; dFloat normals[cMaxContacts][3]; dFloat penetrations[cMaxContacts]; dLong attributeA[cMaxContacts]; dLong attributeB[cMaxContacts]; NewtonWorld* const world = NewtonBodyGetWorld(otherBody); //NewtonBodyGetMatrix(me->m_body, &matrixA[0][0]); //NewtonBodyGetMatrix(otherBody, &matrixB[0][0]); DemoEntity* const entityA = (DemoEntity*)NewtonBodyGetUserData(me->m_body); DemoEntity* const entityB = (DemoEntity*)NewtonBodyGetUserData(otherBody); const dMatrix& matrixA = entityA->GetRenderMatrix (); const dMatrix& matrixB = entityB->GetRenderMatrix (); NewtonCollision* const collisionA = NewtonBodyGetCollision(me->m_body); NewtonCollision* const collisionB = NewtonBodyGetCollision(otherBody); int count = NewtonCollisionCollide (world, cMaxContacts, collisionA, &matrixA[0][0], collisionB, &matrixB[0][0], &contacts[0][0], &normals[0][0], penetrations, attributeA, attributeB, 0); dVector originColor (1.0f, 0.0f, 0.0f, 0.0f); dVector lineColor (0.0f, 0.0f, 1.0f, 0.0f); for (int i = 0; i < count; i ++) { dVector n (normals[i][0], normals[i][1], normals[i][2], 0.0f); dVector p0 (contacts[i][0], contacts[i][1], contacts[i][2], 0.0f); dVector p1 (p0 + n.Scale (0.5f)); p0 = matrixA.UntransformVector(p0); p1 = matrixA.UntransformVector(p1); ShowMousePicking (p0, p1, originColor, lineColor); } } return 1; }