2010-09-09 22:06:57 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
2013-03-02 15:26:18 +00:00
|
|
|
* Copyright (c) 2010-2013 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
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// ReactPhysics3D namespace
|
2010-09-09 22:06:57 +00:00
|
|
|
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
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
// Class CollisionDetection
|
|
|
|
/**
|
|
|
|
* This class computes the collision detection algorithms. We first
|
|
|
|
* perform a broad-phase algorithm to know which pairs of bodies can
|
|
|
|
* collide and then we run a narrow-phase algorithm to compute the
|
|
|
|
* collision contacts between bodies.
|
|
|
|
*/
|
2010-09-09 22:06:57 +00:00
|
|
|
class CollisionDetection {
|
2012-09-18 20:09:49 +00:00
|
|
|
|
2010-09-09 22:06:57 +00:00
|
|
|
private :
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// -------------------- Attributes -------------------- //
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Pointer to the physics world
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionWorld* mWorld;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Broad-phase overlapping pairs
|
2012-10-09 20:21:02 +00:00
|
|
|
std::map<bodyindexpair, BroadPhasePair*> mOverlappingPairs;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Broad-phase algorithm
|
2012-10-09 20:21:02 +00:00
|
|
|
BroadPhaseAlgorithm* mBroadPhaseAlgorithm;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Narrow-phase GJK algorithm
|
2012-10-09 20:21:02 +00:00
|
|
|
GJKAlgorithm mNarrowPhaseGJKAlgorithm;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Narrow-phase Sphere vs Sphere algorithm
|
2012-10-09 20:21:02 +00:00
|
|
|
SphereVsSphereAlgorithm mNarrowPhaseSphereVsSphereAlgorithm;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Memory pool for contactinfo
|
2012-10-09 20:21:02 +00:00
|
|
|
MemoryPool<ContactInfo> mMemoryPoolContactInfos;
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Memory pool for broad-phase pairs
|
2012-10-09 20:21:02 +00:00
|
|
|
MemoryPool<BroadPhasePair> mMemoryPoolBroadPhasePairs;
|
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Private copy-constructor
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionDetection(const CollisionDetection& collisionDetection);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Private assignment operator
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionDetection& operator=(const CollisionDetection& collisionDetection);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Compute the broad-phase collision detection
|
2012-10-09 20:21:02 +00:00
|
|
|
void computeBroadPhase();
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Compute the narrow-phase collision detection
|
2012-10-09 20:21:02 +00:00
|
|
|
bool computeNarrowPhase();
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Select the narrow phase algorithm to use given two collision shapes
|
2012-08-03 22:34:30 +00:00
|
|
|
NarrowPhaseAlgorithm& SelectNarrowPhaseAlgorithm(CollisionShape* collisionShape1,
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionShape* collisionShape2);
|
2012-01-25 22:57:27 +00:00
|
|
|
|
2010-09-09 22:06:57 +00:00
|
|
|
public :
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Constructor
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionDetection(CollisionWorld* world);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Destructor
|
2012-10-09 20:21:02 +00:00
|
|
|
~CollisionDetection();
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Add a body to the collision detection
|
2012-10-09 20:21:02 +00:00
|
|
|
void addBody(CollisionBody* body);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Remove a body from the collision detection
|
2012-10-09 20:21:02 +00:00
|
|
|
void removeBody(CollisionBody* body);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Compute the collision detection
|
2012-10-09 20:21:02 +00:00
|
|
|
bool computeCollisionDetection();
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Allow the broadphase to notify the collision detection about a new overlapping pair.
|
2012-10-09 20:21:02 +00:00
|
|
|
void broadPhaseNotifyAddedOverlappingPair(BodyPair* pair);
|
|
|
|
|
2013-03-05 22:09:50 +00:00
|
|
|
/// Allow the broadphase to notify the collision detection about a removed overlapping pair
|
2012-10-09 20:21:02 +00:00
|
|
|
void broadPhaseNotifyRemovedOverlappingPair(BodyPair* 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
|
2012-10-09 20:21:02 +00:00
|
|
|
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-10-09 20:21:02 +00:00
|
|
|
return mNarrowPhaseSphereVsSphereAlgorithm;
|
2012-01-25 22:57:27 +00:00
|
|
|
}
|
|
|
|
else { // GJK algorithm
|
2012-10-09 20:21:02 +00:00
|
|
|
return mNarrowPhaseGJKAlgorithm;
|
2012-01-25 22:57:27 +00:00
|
|
|
}
|
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
|
2012-10-09 20:21:02 +00:00
|
|
|
mBroadPhaseAlgorithm->addObject(body, *(body->getAABB()));
|
2012-07-25 21:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2012-10-09 20:21:02 +00:00
|
|
|
mBroadPhaseAlgorithm->removeObject(body);
|
2012-07-25 21:31:57 +00:00
|
|
|
}
|
2012-01-25 22:57:27 +00:00
|
|
|
|
2013-03-02 15:26:18 +00:00
|
|
|
}
|
2010-09-09 22:06:57 +00:00
|
|
|
|
|
|
|
#endif
|