diff --git a/sources/reactphysics3d/collision/CollisionDetection.cpp b/sources/reactphysics3d/collision/CollisionDetection.cpp index 8b4855e0..afbe4ec0 100644 --- a/sources/reactphysics3d/collision/CollisionDetection.cpp +++ b/sources/reactphysics3d/collision/CollisionDetection.cpp @@ -55,11 +55,9 @@ void CollisionDetection::computeCollisionContacts() { // Compute the collision detection for the time interval [0, timeMax] // The method returns true if a collision occurs in the time interval [0, timeMax] -bool CollisionDetection::computeCollisionDetection(CollisionWorld* collisionWorld, const Time& timeMax, Time& timeFirst, Time& timeLast) { +bool CollisionDetection::computeCollisionDetection(CollisionWorld* collisionWorld, const Time& timeMax, Time& timeFirstCollision) { bool existsCollision = false; // True if a collision is found in the time interval [0, timeMax] - Time timeFirstTemp; // Temporary timeFirst value - Time timeLastTemp; // Temporary timeLast value // For each pair of bodies in the collisionWorld for(std::vector::const_iterator it1 = collisionWorld->getBodyListStartIterator(); it1 != collisionWorld->getBodyListEndIterator(); ++it1) { @@ -81,15 +79,14 @@ bool CollisionDetection::computeCollisionDetection(CollisionWorld* collisionWorl Vector3D velocity2 = rigidBody2->getInterpolatedState().getLinearVelocity(); // Use the narrow-phase algorithm to check if the two bodies really collide - if(narrowPhaseAlgorithm->testCollision(&obb1, &obb2, &contact, velocity1, velocity2, timeMax, timeFirstTemp, timeLastTemp)) { + if(narrowPhaseAlgorithm->testCollision(&obb1, &obb2, &contact, velocity1, velocity2, timeMax)) { assert(contact != 0); existsCollision = true; // Check if the collision time is the first collision between all bodies - if (timeFirstTemp < timeFirst) { + if (contact->getTime() < timeFirstCollision) { // Update the first collision time between all bodies - timeFirst.setValue(timeFirstTemp.getValue()); - timeLast.setValue(timeLastTemp.getValue()); + timeFirstCollision.setValue(contact->getTime().getValue()); // Add the new collision contact into the collision world collisionWorld->addConstraint(contact); diff --git a/sources/reactphysics3d/collision/CollisionDetection.h b/sources/reactphysics3d/collision/CollisionDetection.h index cf8ebdd7..be357054 100644 --- a/sources/reactphysics3d/collision/CollisionDetection.h +++ b/sources/reactphysics3d/collision/CollisionDetection.h @@ -53,7 +53,7 @@ class CollisionDetection { CollisionDetection(); // Constructor ~CollisionDetection(); // Destructor - bool computeCollisionDetection(CollisionWorld* collisionWorld, const Time& timeMax, Time& timeFirst, Time& timeLast); // Compute the collision detection + bool computeCollisionDetection(CollisionWorld* collisionWorld, const Time& timeMax, Time& timeFirstCollision); // Compute the collision detection }; // Add a possible collision pair of bodies in the possibleCollisionPairList diff --git a/sources/reactphysics3d/collision/NarrowPhaseAlgorithm.h b/sources/reactphysics3d/collision/NarrowPhaseAlgorithm.h index 727cd300..8225f0e5 100644 --- a/sources/reactphysics3d/collision/NarrowPhaseAlgorithm.h +++ b/sources/reactphysics3d/collision/NarrowPhaseAlgorithm.h @@ -44,7 +44,7 @@ class NarrowPhaseAlgorithm { virtual bool testCollision(const BoundingVolume* const boundingVolume1, const BoundingVolume* const boundingVolume2, Contact** contact, const Vector3D& velocity1, const Vector3D& velocity2, - const Time& timeMax, Time& timeFirst, Time& timeLast)=0; // Return true and compute a collision contact and collision time if the two bounding volume collide + const Time& timeMax)=0; // Return true and compute a collision contact and collision time if the two bounding volume collide }; } // End of reactphysics3d namespace diff --git a/sources/reactphysics3d/collision/SeparatingAxisOBB.cpp b/sources/reactphysics3d/collision/SeparatingAxisOBB.cpp index d923bf88..1fb94998 100644 --- a/sources/reactphysics3d/collision/SeparatingAxisOBB.cpp +++ b/sources/reactphysics3d/collision/SeparatingAxisOBB.cpp @@ -41,7 +41,7 @@ SeparatingAxisOBB::~SeparatingAxisOBB() { // Return true and compute a collision contact if the two bounding volume collide. // The method returns false if there is no collision between the two bounding volumes. bool SeparatingAxisOBB::testCollision(const BoundingVolume* const boundingVolume1, const BoundingVolume* const boundingVolume2, Contact** contact, - const Vector3D& velocity1, const Vector3D& velocity2, const Time& timeMax, Time& timeFirst, Time& timeLast) { + const Vector3D& velocity1, const Vector3D& velocity2, const Time& timeMax) { assert(boundingVolume1 != boundingVolume2); // If the two bounding volumes are OBB @@ -51,7 +51,7 @@ bool SeparatingAxisOBB::testCollision(const BoundingVolume* const boundingVolume // If the two bounding volumes are OBB if (obb1 && obb2) { // Compute the collision test between two OBB - return computeCollisionTest(obb1, obb2, contact, velocity1, velocity2, timeMax, timeFirst, timeLast); + return computeCollisionTest(obb1, obb2, contact, velocity1, velocity2, timeMax); } else { return false; @@ -68,8 +68,7 @@ bool SeparatingAxisOBB::testCollision(const BoundingVolume* const boundingVolume // vector of OBB 1 and Bj is the jth face normal vector of OBB 2. We will use the notation Ai for the ith face // normal of OBB 1 and Bj for the jth face normal of OBB 2. bool SeparatingAxisOBB::computeCollisionTest(const OBB* const obb1, const OBB* const obb2, Contact** contact, - const Vector3D& velocity1, const Vector3D& velocity2, const Time& timeMax, - Time& timeFirst, Time& timeLast) { + const Vector3D& velocity1, const Vector3D& velocity2, const Time& timeMax) { double center; // Center double speed; // Relavtive speed of the projection intervals (dotProduct(SeparatingAxis, deltaVelocity)) @@ -96,8 +95,8 @@ bool SeparatingAxisOBB::computeCollisionTest(const OBB* const obb1, const OBB* c Vector3D deltaVelocity = velocity2-velocity1; // Difference of box center velocities Vector3D boxDistance = obb2->getCenter() - obb1->getCenter(); // Distance between the centers of the OBBs - timeFirst.setValue(0.0); // timeFirst = 0 - timeLast.setValue(DBL_MAX); // timeLast = infinity + Time timeFirst(0.0); // timeFirst = 0 + Time timeLast(DBL_MAX); // timeLast = infinity (time when two colliding bodies separates) // Axis A0 for (int i=0; i<3; ++i) { @@ -247,8 +246,8 @@ bool SeparatingAxisOBB::computeCollisionTest(const OBB* const obb1, const OBB* c // There exists a parallel pair of face normals and we have already checked all the face // normals for separation. Therefore the OBBs must intersect - // TODO : Delete this - (*contact) = new Contact(obb1->getBodyPointer(), obb2->getBodyPointer(), Vector3D(1,0,0)); + // TODO : Construct a face-face contact here + (*contact) = new Contact(obb1->getBodyPointer(), obb2->getBodyPointer(), Vector3D(1,0,0), timeFirst); std::cout << "Contact : " << contact << std::endl; return true; @@ -381,7 +380,7 @@ bool SeparatingAxisOBB::computeCollisionTest(const OBB* const obb1, const OBB* c } // TODO : Delete this - (*contact) = new Contact(obb1->getBodyPointer(), obb2->getBodyPointer(), Vector3D(1,0,0)); + (*contact) = new Contact(obb1->getBodyPointer(), obb2->getBodyPointer(), Vector3D(1,0,0), timeFirst); std::cout << "Contact2 : " << contact << std::endl; // We have found no separation axis, therefore the two OBBs must collide @@ -425,7 +424,6 @@ bool SeparatingAxisOBB::computeIntervalsIntersectionTime(const Time& timeMax, do // If we found a earlier separated collision time, we update the last collision time if (t < timeLast.getValue()) { - timeLast.setValue(t); } diff --git a/sources/reactphysics3d/collision/SeparatingAxisOBB.h b/sources/reactphysics3d/collision/SeparatingAxisOBB.h index b4b653e3..feb0c8f7 100644 --- a/sources/reactphysics3d/collision/SeparatingAxisOBB.h +++ b/sources/reactphysics3d/collision/SeparatingAxisOBB.h @@ -45,18 +45,16 @@ namespace reactphysics3d { class SeparatingAxisOBB : public NarrowPhaseAlgorithm { private : bool computeCollisionTest(const OBB* const obb1, const OBB* const obb2, Contact** contact, - const Vector3D& velocity1, const Vector3D& velocity2, - const Time& timeMax, Time& timeFirst, Time& timeLast); // Return true and compute a collision contact if the two OBB collide + const Vector3D& velocity1, const Vector3D& velocity2, const Time& timeMax); // Return true and compute a collision contact if the two OBB collide bool computeIntervalsIntersectionTime(const Time& timeMax, double speed, double min0, double max0, - double min1, double max1, Time& timeFirst, Time& timeLast); // Compute the intersection time of two projection intervals + double min1, double max1, Time& timeFirst, Time& timeLast); // Compute the intersection time of two projection intervals public : SeparatingAxisOBB(); // Constructor ~SeparatingAxisOBB(); // Destructor virtual bool testCollision(const BoundingVolume* const boundingVolume1, const BoundingVolume* const boundingVolume2, - Contact** contact, const Vector3D& velocity1, const Vector3D& velocity2, const Time& timeMax, - Time& timeFirst, Time& timeLast); // Return true and compute a collision contact if the two bounding volume collide + Contact** contact, const Vector3D& velocity1, const Vector3D& velocity2, const Time& timeMax); // Return true and compute a collision contact if the two bounding volume collide };