2009-07-08 14:53:58 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2009 Daniel Chappuis *
|
|
|
|
****************************************************************************
|
|
|
|
* This file is part of ReactPhysics3D. *
|
|
|
|
* *
|
|
|
|
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "CollisionDetection.h"
|
2009-11-20 13:51:47 +00:00
|
|
|
#include "NoBroadPhaseAlgorithm.h"
|
|
|
|
#include "NarrowPhaseSATAlgorithm.h"
|
2009-07-21 20:29:37 +00:00
|
|
|
#include "../body/OBB.h"
|
|
|
|
#include "../body/RigidBody.h"
|
|
|
|
#include <cassert>
|
2009-07-08 14:53:58 +00:00
|
|
|
|
|
|
|
// We want to use the ReactPhysics3D namespace
|
|
|
|
using namespace reactphysics3d;
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
CollisionDetection::CollisionDetection() {
|
|
|
|
|
2009-07-08 16:35:57 +00:00
|
|
|
// Construct the broad-phase algorithm that will be used (Separating axis with AABB)
|
2009-11-20 13:51:47 +00:00
|
|
|
broadPhaseAlgorithm = new NoBroadPhaseAlgorithm();
|
2009-07-08 14:53:58 +00:00
|
|
|
|
2009-07-08 16:35:57 +00:00
|
|
|
// Construct the narrow-phase algorithm that will be used (Separating axis with OBB)
|
2009-11-20 13:51:47 +00:00
|
|
|
narrowPhaseAlgorithm = new NarrowPhaseSATAlgorithm();
|
2009-07-08 14:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor
|
2009-07-08 16:35:57 +00:00
|
|
|
CollisionDetection::~CollisionDetection() {
|
2009-07-08 14:53:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-07-21 20:29:37 +00:00
|
|
|
// Compute the collision detection for the time interval [0, timeMax]
|
|
|
|
// The method returns true if a collision occurs in the time interval [0, timeMax]
|
2009-10-18 19:24:18 +00:00
|
|
|
bool CollisionDetection::computeCollisionDetection(CollisionWorld* collisionWorld) {
|
2009-07-21 20:29:37 +00:00
|
|
|
|
2009-07-31 16:13:57 +00:00
|
|
|
bool existsCollision = false; // True if a collision is found in the time interval [0, timeMax]
|
2009-07-21 20:29:37 +00:00
|
|
|
|
|
|
|
// For each pair of bodies in the collisionWorld
|
|
|
|
for(std::vector<Body*>::const_iterator it1 = collisionWorld->getBodyListStartIterator(); it1 != collisionWorld->getBodyListEndIterator(); ++it1) {
|
|
|
|
for(std::vector<Body*>::const_iterator it2 = it1; it2 != collisionWorld->getBodyListEndIterator(); ++it2) {
|
|
|
|
// If both bodies are RigidBody and are different
|
|
|
|
RigidBody* rigidBody1 = dynamic_cast<RigidBody*>(*it1);
|
|
|
|
RigidBody* rigidBody2 = dynamic_cast<RigidBody*>(*it2);
|
|
|
|
if(rigidBody1 && rigidBody2 && rigidBody1 != rigidBody2) {
|
|
|
|
// Get the oriented bounding boxes of the two bodies
|
|
|
|
OBB obb1 = rigidBody1->getOBB();
|
|
|
|
OBB obb2 = rigidBody2->getOBB();
|
|
|
|
|
|
|
|
// Use the broad-phase algorithm to decide if the two bodies can collide
|
|
|
|
if(broadPhaseAlgorithm->testCollisionPair(&obb1, &obb2)) {
|
|
|
|
Contact* contact = 0;
|
|
|
|
|
|
|
|
// Use the narrow-phase algorithm to check if the two bodies really collide
|
2009-11-20 13:51:47 +00:00
|
|
|
if(narrowPhaseAlgorithm->testCollision(&obb1, &obb2, &contact)) {
|
2009-07-21 20:29:37 +00:00
|
|
|
assert(contact != 0);
|
2010-02-05 14:38:02 +00:00
|
|
|
existsCollision = true;
|
2009-07-31 16:13:57 +00:00
|
|
|
|
2010-02-06 18:35:50 +00:00
|
|
|
Vector3D test = contact->getNormal(); // TODO : Delete this
|
|
|
|
|
2009-10-18 19:24:18 +00:00
|
|
|
// Add the new collision contact into the collision world
|
|
|
|
collisionWorld->addConstraint(contact);
|
2009-07-21 20:29:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return existsCollision;
|
2009-07-08 14:53:58 +00:00
|
|
|
}
|