2010-09-09 22:06:57 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
2011-11-13 17:49:03 +00:00
|
|
|
* Copyright (c) 2010-2012 Daniel Chappuis *
|
2010-09-09 22:06:57 +00:00
|
|
|
*********************************************************************************
|
|
|
|
* *
|
2011-11-13 17:49:03 +00:00
|
|
|
* This software is provided 'as-is', without any express or implied warranty. *
|
|
|
|
* In no event will the authors be held liable for any damages arising from the *
|
|
|
|
* use of this software. *
|
2010-09-09 22:06:57 +00:00
|
|
|
* *
|
2011-11-13 17:49:03 +00:00
|
|
|
* Permission is granted to anyone to use this software for any purpose, *
|
|
|
|
* including commercial applications, and to alter it and redistribute it *
|
|
|
|
* freely, subject to the following restrictions: *
|
|
|
|
* *
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim *
|
|
|
|
* that you wrote the original software. If you use this software in a *
|
|
|
|
* product, an acknowledgment in the product documentation would be *
|
|
|
|
* appreciated but is not required. *
|
|
|
|
* *
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be *
|
|
|
|
* misrepresented as being the original software. *
|
|
|
|
* *
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution. *
|
2010-09-09 22:06:57 +00:00
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
2011-02-19 19:38:08 +00:00
|
|
|
#ifndef COLLISION_DETECTION_H
|
|
|
|
#define COLLISION_DETECTION_H
|
2010-09-09 22:06:57 +00:00
|
|
|
|
|
|
|
// Libraries
|
2012-09-18 20:09:49 +00:00
|
|
|
#include "../body/CollisionBody.h"
|
2012-07-25 21:31:57 +00:00
|
|
|
#include "broadphase/BroadPhaseAlgorithm.h"
|
2012-09-26 21:07:40 +00:00
|
|
|
#include "BroadPhasePair.h"
|
2011-10-18 22:03:05 +00:00
|
|
|
#include "../memory/MemoryPool.h"
|
2012-01-25 22:57:27 +00:00
|
|
|
#include "narrowphase/GJK/GJKAlgorithm.h"
|
|
|
|
#include "narrowphase/SphereVsSphereAlgorithm.h"
|
2010-09-09 22:06:57 +00:00
|
|
|
#include "ContactInfo.h"
|
|
|
|
#include <vector>
|
2011-10-18 22:03:05 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <utility>
|
2012-07-25 21:31:57 +00:00
|
|
|
#include <iostream> // TODO : Delete this
|
2011-10-18 22:03:05 +00:00
|
|
|
|
2010-09-09 22:06:57 +00:00
|
|
|
|
|
|
|
// ReactPhysics3D namespace
|
|
|
|
namespace reactphysics3d {
|
|
|
|
|
2011-10-18 22:03:05 +00:00
|
|
|
// Declarations
|
|
|
|
class BroadPhaseAlgorithm;
|
2012-10-03 19:00:17 +00:00
|
|
|
class CollisionWorld;
|
2012-07-25 21:31:57 +00:00
|
|
|
|
2010-09-09 22:06:57 +00:00
|
|
|
/* -------------------------------------------------------------------
|
|
|
|
Class CollisionDetection :
|
|
|
|
This class computes the collision detection algorithms. We first
|
2012-01-25 22:57:27 +00:00
|
|
|
perform a broad-phase algorithm to know which pairs of bodies can
|
2010-09-09 22:06:57 +00:00
|
|
|
collide and then we run a narrow-phase algorithm to compute the
|
|
|
|
collision contacts between bodies.
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
class CollisionDetection {
|
2012-09-18 20:09:49 +00:00
|
|
|
|
2010-09-09 22:06:57 +00:00
|
|
|
private :
|
2012-10-03 19:00:17 +00:00
|
|
|
CollisionWorld* world; // Pointer to the physics world
|
2012-09-26 21:07:40 +00:00
|
|
|
std::map<bodyindexpair, BroadPhasePair*> overlappingPairs; // Broad-phase overlapping pairs of bodies
|
2012-01-25 22:57:27 +00:00
|
|
|
BroadPhaseAlgorithm* broadPhaseAlgorithm; // Broad-phase algorithm
|
|
|
|
GJKAlgorithm narrowPhaseGJKAlgorithm; // Narrow-phase GJK algorithm
|
|
|
|
SphereVsSphereAlgorithm narrowPhaseSphereVsSphereAlgorithm; // Narrow-phase Sphere vs Sphere algorithm
|
2012-01-28 21:03:30 +00:00
|
|
|
MemoryPool<ContactInfo> memoryPoolContactInfos; // Memory pool for the contact info
|
2012-09-26 21:07:40 +00:00
|
|
|
MemoryPool<BroadPhasePair> memoryPoolOverlappingPairs; // Memory pool for the overlapping pairs
|
|
|
|
|
2012-01-25 22:57:27 +00:00
|
|
|
void computeBroadPhase(); // Compute the broad-phase collision detection
|
|
|
|
bool computeNarrowPhase(); // Compute the narrow-phase collision detection
|
2012-08-03 22:34:30 +00:00
|
|
|
NarrowPhaseAlgorithm& SelectNarrowPhaseAlgorithm(CollisionShape* collisionShape1,
|
|
|
|
CollisionShape* collisionShape2); // Select the narrow phase algorithm to use given two collision shapes
|
2012-01-25 22:57:27 +00:00
|
|
|
|
2010-09-09 22:06:57 +00:00
|
|
|
public :
|
2012-10-03 19:00:17 +00:00
|
|
|
CollisionDetection(CollisionWorld* world); // Constructor
|
2012-01-25 22:57:27 +00:00
|
|
|
~CollisionDetection(); // Destructor
|
2012-07-25 21:31:57 +00:00
|
|
|
|
2012-09-18 20:09:49 +00:00
|
|
|
void addBody(CollisionBody* body); // Add a body to the collision detection
|
|
|
|
void removeBody(CollisionBody* body); // Remove a body from the collision detection
|
2012-01-25 22:57:27 +00:00
|
|
|
bool computeCollisionDetection(); // Compute the collision detection
|
2012-09-26 21:07:40 +00:00
|
|
|
void broadPhaseNotifyAddedOverlappingPair(BodyPair* pair); // Allow the broadphase to notify the collision detection about a new overlapping pair
|
|
|
|
void broadPhaseNotifyRemovedOverlappingPair(BodyPair* pair); // Allow the broadphase to notify the collision detection about a removed overlapping pair
|
2010-09-09 22:06:57 +00:00
|
|
|
};
|
|
|
|
|
2012-08-03 22:34:30 +00:00
|
|
|
// Select the narrow-phase collision algorithm to use given two collision shapes
|
|
|
|
inline NarrowPhaseAlgorithm& CollisionDetection::SelectNarrowPhaseAlgorithm(CollisionShape* collisionShape1, CollisionShape* collisionShape2) {
|
2012-01-25 22:57:27 +00:00
|
|
|
|
|
|
|
// Sphere vs Sphere algorithm
|
2012-08-03 22:34:30 +00:00
|
|
|
if (collisionShape1->getType() == SPHERE && collisionShape2->getType() == SPHERE) {
|
2012-01-25 22:57:27 +00:00
|
|
|
return narrowPhaseSphereVsSphereAlgorithm;
|
|
|
|
}
|
|
|
|
else { // GJK algorithm
|
|
|
|
return narrowPhaseGJKAlgorithm;
|
|
|
|
}
|
2012-07-25 21:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a body to the collision detection
|
2012-09-18 20:09:49 +00:00
|
|
|
inline void CollisionDetection::addBody(CollisionBody* body) {
|
2012-07-25 21:31:57 +00:00
|
|
|
|
|
|
|
// Add the body to the broad-phase
|
|
|
|
broadPhaseAlgorithm->addObject(body, *(body->getAABB()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a body from the collision detection
|
2012-09-18 20:09:49 +00:00
|
|
|
inline void CollisionDetection::removeBody(CollisionBody* body) {
|
2012-07-25 21:31:57 +00:00
|
|
|
|
|
|
|
// Remove the body from the broad-phase
|
|
|
|
broadPhaseAlgorithm->removeObject(body);
|
|
|
|
}
|
2012-01-25 22:57:27 +00:00
|
|
|
|
2010-09-09 22:06:57 +00:00
|
|
|
} // End of the ReactPhysics3D namespace
|
|
|
|
|
|
|
|
#endif
|