Add the bodyindex type to represent the index number of a body
This commit is contained in:
parent
62823f3d43
commit
c7faae9b20
|
@ -31,7 +31,7 @@
|
||||||
using namespace reactphysics3d;
|
using namespace reactphysics3d;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Body::Body(const Transform& transform, Collider* collider, decimal mass, long unsigned int id)
|
Body::Body(const Transform& transform, Collider* collider, decimal mass, bodyindex id)
|
||||||
: collider(collider), mass(mass), transform(transform), isActive(true), id(id), hasMoved(false) {
|
: collider(collider), mass(mass), transform(transform), isActive(true), id(id), hasMoved(false) {
|
||||||
assert(mass > 0.0);
|
assert(mass > 0.0);
|
||||||
assert(collider);
|
assert(collider);
|
||||||
|
|
|
@ -55,14 +55,14 @@ class Body {
|
||||||
bool isMotionEnabled; // True if the body is able to move
|
bool isMotionEnabled; // True if the body is able to move
|
||||||
bool isCollisionEnabled; // True if the body can collide with others bodies
|
bool isCollisionEnabled; // True if the body can collide with others bodies
|
||||||
AABB* aabb; // Axis-Aligned Bounding Box for Broad-Phase collision detection
|
AABB* aabb; // Axis-Aligned Bounding Box for Broad-Phase collision detection
|
||||||
luint id; // ID of the body
|
bodyindex id; // ID of the body
|
||||||
bool hasMoved; // True if the body has moved during the last frame
|
bool hasMoved; // True if the body has moved during the last frame
|
||||||
|
|
||||||
public :
|
public :
|
||||||
Body(const Transform& transform, Collider* collider, decimal mass, long unsigned int id); // Constructor
|
Body(const Transform& transform, Collider* collider, decimal mass, bodyindex id); // Constructor
|
||||||
virtual ~Body(); // Destructor
|
virtual ~Body(); // Destructor
|
||||||
|
|
||||||
luint getID() const; // Return the id of the body
|
bodyindex getID() const; // Return the id of the body
|
||||||
bool getHasMoved() const; // Return true if the body has moved during the last frame
|
bool getHasMoved() const; // Return true if the body has moved during the last frame
|
||||||
void setHasMoved(bool hasMoved); // Set the hasMoved variable (true if the body has moved during the last frame)
|
void setHasMoved(bool hasMoved); // Set the hasMoved variable (true if the body has moved during the last frame)
|
||||||
Collider* getCollider() const; // Return the collision collider
|
Collider* getCollider() const; // Return the collision collider
|
||||||
|
@ -91,7 +91,7 @@ class Body {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return the id of the body
|
// Return the id of the body
|
||||||
inline luint Body::getID() const {
|
inline bodyindex Body::getID() const {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@
|
||||||
using namespace reactphysics3d;
|
using namespace reactphysics3d;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
RigidBody::RigidBody(const Transform& transform, decimal mass, const Matrix3x3& inertiaTensorLocal, Collider* collider, long unsigned id)
|
RigidBody::RigidBody(const Transform& transform, decimal mass, const Matrix3x3& inertiaTensorLocal, Collider* collider, bodyindex id)
|
||||||
: Body(transform, collider, mass, id), inertiaTensorLocal(inertiaTensorLocal),
|
: Body(transform, collider, mass, id), inertiaTensorLocal(inertiaTensorLocal),
|
||||||
inertiaTensorLocalInverse(inertiaTensorLocal.getInverse()), massInverse(1.0/mass) {
|
inertiaTensorLocalInverse(inertiaTensorLocal.getInverse()), massInverse(1.0/mass) {
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ class RigidBody : public Body {
|
||||||
Vector3 linearVelocity; // Linear velocity of the body
|
Vector3 linearVelocity; // Linear velocity of the body
|
||||||
Vector3 angularVelocity; // Angular velocity of the body
|
Vector3 angularVelocity; // Angular velocity of the body
|
||||||
Vector3 externalForce; // Current external force on the body
|
Vector3 externalForce; // Current external force on the body
|
||||||
Vector3 externalTorque; // Current external torque on the body
|
Vector3 externalTorque; // Current external torque on the body
|
||||||
Matrix3x3 inertiaTensorLocal; // Local inertia tensor of the body (in body coordinates)
|
Matrix3x3 inertiaTensorLocal; // Local inertia tensor of the body (in body coordinates)
|
||||||
Matrix3x3 inertiaTensorLocalInverse; // Inverse of the inertia tensor of the body (in body coordinates)
|
Matrix3x3 inertiaTensorLocalInverse; // Inverse of the inertia tensor of the body (in body coordinates)
|
||||||
decimal massInverse; // Inverse of the mass of the body
|
decimal massInverse; // Inverse of the mass of the body
|
||||||
|
@ -55,7 +55,7 @@ class RigidBody : public Body {
|
||||||
|
|
||||||
public :
|
public :
|
||||||
RigidBody(const Transform& transform, decimal mass, const Matrix3x3& inertiaTensorLocal,
|
RigidBody(const Transform& transform, decimal mass, const Matrix3x3& inertiaTensorLocal,
|
||||||
Collider* collider, long unsigned int id); // Constructor // Copy-constructor
|
Collider* collider, bodyindex id); // Constructor // Copy-constructor
|
||||||
virtual ~RigidBody(); // Destructor
|
virtual ~RigidBody(); // Destructor
|
||||||
|
|
||||||
Vector3 getLinearVelocity() const; // Return the linear velocity
|
Vector3 getLinearVelocity() const; // Return the linear velocity
|
||||||
|
@ -65,8 +65,8 @@ class RigidBody : public Body {
|
||||||
void setMassInverse(decimal massInverse); // Set the inverse of the mass
|
void setMassInverse(decimal massInverse); // Set the inverse of the mass
|
||||||
Vector3 getExternalForce() const; // Return the current external force of the body
|
Vector3 getExternalForce() const; // Return the current external force of the body
|
||||||
void setExternalForce(const Vector3& force); // Set the current external force on the body
|
void setExternalForce(const Vector3& force); // Set the current external force on the body
|
||||||
Vector3 getExternalTorque() const; // Return the current external torque of the body
|
Vector3 getExternalTorque() const; // Return the current external torque of the body
|
||||||
void setExternalTorque(const Vector3& torque); // Set the current external torque of the body
|
void setExternalTorque(const Vector3& torque); // Set the current external torque of the body
|
||||||
decimal getMassInverse() const; // Return the inverse of the mass of the body
|
decimal getMassInverse() const; // Return the inverse of the mass of the body
|
||||||
Matrix3x3 getInertiaTensorLocal() const; // Return the local inertia tensor of the body (in body coordinates)
|
Matrix3x3 getInertiaTensorLocal() const; // Return the local inertia tensor of the body (in body coordinates)
|
||||||
void setInertiaTensorLocal(const Matrix3x3& inertiaTensorLocal); // Set the local inertia tensor of the body (in body coordinates)
|
void setInertiaTensorLocal(const Matrix3x3& inertiaTensorLocal); // Set the local inertia tensor of the body (in body coordinates)
|
||||||
|
@ -112,12 +112,12 @@ inline void RigidBody::setExternalForce(const Vector3& force) {
|
||||||
this->externalForce = force;
|
this->externalForce = force;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the current external torque on the body
|
// Return the current external torque on the body
|
||||||
inline Vector3 RigidBody::getExternalTorque() const {
|
inline Vector3 RigidBody::getExternalTorque() const {
|
||||||
return externalTorque;
|
return externalTorque;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the current external torque on the body
|
// Set the current external torque on the body
|
||||||
inline void RigidBody::setExternalTorque(const Vector3& torque) {
|
inline void RigidBody::setExternalTorque(const Vector3& torque) {
|
||||||
this->externalTorque = torque;
|
this->externalTorque = torque;
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ CollisionDetection::CollisionDetection(PhysicsWorld* world)
|
||||||
// Destructor
|
// Destructor
|
||||||
CollisionDetection::~CollisionDetection() {
|
CollisionDetection::~CollisionDetection() {
|
||||||
// Delete the remaining overlapping pairs
|
// Delete the remaining overlapping pairs
|
||||||
for (map<std::pair<luint, luint>, OverlappingPair*>::iterator it=overlappingPairs.begin(); it != overlappingPairs.end(); it++) {
|
for (map<std::pair<bodyindex, bodyindex>, OverlappingPair*>::iterator it=overlappingPairs.begin(); it != overlappingPairs.end(); it++) {
|
||||||
// Delete the overlapping pair
|
// Delete the overlapping pair
|
||||||
(*it).second->OverlappingPair::~OverlappingPair();
|
(*it).second->OverlappingPair::~OverlappingPair();
|
||||||
memoryPoolOverlappingPairs.freeObject((*it).second);
|
memoryPoolOverlappingPairs.freeObject((*it).second);
|
||||||
|
@ -117,7 +117,7 @@ void CollisionDetection::computeBroadPhase() {
|
||||||
// Compute the narrow-phase collision detection
|
// Compute the narrow-phase collision detection
|
||||||
bool CollisionDetection::computeNarrowPhase() {
|
bool CollisionDetection::computeNarrowPhase() {
|
||||||
bool collisionExists = false;
|
bool collisionExists = false;
|
||||||
map<std::pair<luint, luint>, OverlappingPair*>::iterator it;
|
map<std::pair<bodyindex, bodyindex>, OverlappingPair*>::iterator it;
|
||||||
|
|
||||||
// For each possible collision pair of bodies
|
// For each possible collision pair of bodies
|
||||||
for (it = overlappingPairs.begin(); it != overlappingPairs.end(); it++) {
|
for (it = overlappingPairs.begin(); it != overlappingPairs.end(); it++) {
|
||||||
|
@ -168,13 +168,13 @@ void CollisionDetection::broadPhaseNotifyAddedOverlappingPair(const BroadPhasePa
|
||||||
std::cout << "New overlapping pair : id0=" << addedPair->body1->getID() << ", id=" << addedPair->body2->getID() << std::endl;
|
std::cout << "New overlapping pair : id0=" << addedPair->body1->getID() << ", id=" << addedPair->body2->getID() << std::endl;
|
||||||
|
|
||||||
// Construct the pair of body index
|
// Construct the pair of body index
|
||||||
pair<luint, luint> indexPair = addedPair->body1->getID() < addedPair->body2->getID() ? make_pair(addedPair->body1->getID(), addedPair->body2->getID()) :
|
pair<bodyindex, bodyindex> indexPair = addedPair->body1->getID() < addedPair->body2->getID() ? make_pair(addedPair->body1->getID(), addedPair->body2->getID()) :
|
||||||
make_pair(addedPair->body2->getID(), addedPair->body1->getID());
|
make_pair(addedPair->body2->getID(), addedPair->body1->getID());
|
||||||
assert(indexPair.first != indexPair.second);
|
assert(indexPair.first != indexPair.second);
|
||||||
|
|
||||||
// Add the pair into the set of overlapping pairs (if not there yet)
|
// Add the pair into the set of overlapping pairs (if not there yet)
|
||||||
OverlappingPair* newPair = new (memoryPoolOverlappingPairs.allocateObject()) OverlappingPair(addedPair->body1, addedPair->body2, memoryPoolContacts);
|
OverlappingPair* newPair = new (memoryPoolOverlappingPairs.allocateObject()) OverlappingPair(addedPair->body1, addedPair->body2, memoryPoolContacts);
|
||||||
pair<map<pair<luint, luint>, OverlappingPair*>::iterator, bool> check = overlappingPairs.insert(make_pair(indexPair, newPair));
|
pair<map<pair<bodyindex, bodyindex>, OverlappingPair*>::iterator, bool> check = overlappingPairs.insert(make_pair(indexPair, newPair));
|
||||||
|
|
||||||
// If the overlapping pair was already in the set of overlapping pair
|
// If the overlapping pair was already in the set of overlapping pair
|
||||||
if (!check.second) {
|
if (!check.second) {
|
||||||
|
@ -188,7 +188,7 @@ void CollisionDetection::broadPhaseNotifyAddedOverlappingPair(const BroadPhasePa
|
||||||
void CollisionDetection::broadPhaseNotifyRemovedOverlappingPair(const BroadPhasePair* removedPair) {
|
void CollisionDetection::broadPhaseNotifyRemovedOverlappingPair(const BroadPhasePair* removedPair) {
|
||||||
|
|
||||||
// Construct the pair of body index
|
// Construct the pair of body index
|
||||||
pair<luint, luint> indexPair = removedPair->body1->getID() < removedPair->body2->getID() ? make_pair(removedPair->body1->getID(), removedPair->body2->getID()) :
|
pair<bodyindex, bodyindex> indexPair = removedPair->body1->getID() < removedPair->body2->getID() ? make_pair(removedPair->body1->getID(), removedPair->body2->getID()) :
|
||||||
make_pair(removedPair->body2->getID(), removedPair->body1->getID());
|
make_pair(removedPair->body2->getID(), removedPair->body1->getID());
|
||||||
|
|
||||||
// Remove the overlapping pair from the memory pool
|
// Remove the overlapping pair from the memory pool
|
||||||
|
|
|
@ -59,9 +59,9 @@ class PhysicsWorld;
|
||||||
class CollisionDetection {
|
class CollisionDetection {
|
||||||
private :
|
private :
|
||||||
PhysicsWorld* world; // Pointer to the physics world
|
PhysicsWorld* world; // Pointer to the physics world
|
||||||
std::map<std::pair<luint, luint>, OverlappingPair*> overlappingPairs; // Broad-phase overlapping pairs of bodies
|
std::map<std::pair<bodyindex, bodyindex>, OverlappingPair*> overlappingPairs; // Broad-phase overlapping pairs of bodies
|
||||||
std::set<std::pair<luint, luint> > currentStepOverlappingPairs; // Overlapping pairs of bodies at the current collision detection step
|
std::set<std::pair<bodyindex, bodyindex> > currentStepOverlappingPairs; // Overlapping pairs of bodies at the current collision detection step
|
||||||
std::set<std::pair<luint, luint> > lastStepOverlappingPairs; // Overlapping pairs of bodies at the last collision detection step
|
std::set<std::pair<bodyindex, bodyindex> > lastStepOverlappingPairs; // Overlapping pairs of bodies at the last collision detection step
|
||||||
BroadPhaseAlgorithm* broadPhaseAlgorithm; // Broad-phase algorithm
|
BroadPhaseAlgorithm* broadPhaseAlgorithm; // Broad-phase algorithm
|
||||||
GJKAlgorithm narrowPhaseGJKAlgorithm; // Narrow-phase GJK algorithm
|
GJKAlgorithm narrowPhaseGJKAlgorithm; // Narrow-phase GJK algorithm
|
||||||
SphereVsSphereAlgorithm narrowPhaseSphereVsSphereAlgorithm; // Narrow-phase Sphere vs Sphere algorithm
|
SphereVsSphereAlgorithm narrowPhaseSphereVsSphereAlgorithm; // Narrow-phase Sphere vs Sphere algorithm
|
||||||
|
@ -80,7 +80,7 @@ class CollisionDetection {
|
||||||
|
|
||||||
void addBody(Body* body); // Add a body to the collision detection
|
void addBody(Body* body); // Add a body to the collision detection
|
||||||
void removeBody(Body* body); // Remove a body from the collision detection
|
void removeBody(Body* body); // Remove a body from the collision detection
|
||||||
OverlappingPair* getOverlappingPair(luint body1ID, luint body2ID); // Return an overlapping pair or null
|
OverlappingPair* getOverlappingPair(bodyindex body1ID, bodyindex body2ID); // Return an overlapping pair or null
|
||||||
bool computeCollisionDetection(); // Compute the collision detection
|
bool computeCollisionDetection(); // Compute the collision detection
|
||||||
void broadPhaseNotifyAddedOverlappingPair(const BroadPhasePair* pair); // Allow the broadphase to notify the collision detection about a new overlapping pair
|
void broadPhaseNotifyAddedOverlappingPair(const BroadPhasePair* pair); // Allow the broadphase to notify the collision detection about a new overlapping pair
|
||||||
void broadPhaseNotifyRemovedOverlappingPair(const BroadPhasePair* pair); // Allow the broadphase to notify the collision detection about a removed overlapping pair
|
void broadPhaseNotifyRemovedOverlappingPair(const BroadPhasePair* pair); // Allow the broadphase to notify the collision detection about a removed overlapping pair
|
||||||
|
@ -88,8 +88,8 @@ class CollisionDetection {
|
||||||
|
|
||||||
// Return an overlapping pair of bodies according to the given bodies ID
|
// Return an overlapping pair of bodies according to the given bodies ID
|
||||||
// The method returns null if the pair of bodies is not overlapping
|
// The method returns null if the pair of bodies is not overlapping
|
||||||
inline OverlappingPair* CollisionDetection::getOverlappingPair(luint body1ID, luint body2ID) {
|
inline OverlappingPair* CollisionDetection::getOverlappingPair(bodyindex body1ID, bodyindex body2ID) {
|
||||||
std::pair<luint, luint> pair = (body1ID < body2ID) ? std::make_pair(body1ID, body2ID) : std::make_pair(body2ID, body1ID);
|
std::pair<bodyindex, bodyindex> pair = (body1ID < body2ID) ? std::make_pair(body1ID, body2ID) : std::make_pair(body2ID, body1ID);
|
||||||
if (overlappingPairs.count(pair) == 1) {
|
if (overlappingPairs.count(pair) == 1) {
|
||||||
return overlappingPairs[pair];
|
return overlappingPairs[pair];
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,9 @@
|
||||||
|
|
||||||
using namespace reactphysics3d;
|
using namespace reactphysics3d;
|
||||||
|
|
||||||
|
// Initialization of static variables
|
||||||
|
bodyindex PairManager::INVALID_INDEX = std::numeric_limits<reactphysics3d::bodyindex>::max();
|
||||||
|
|
||||||
// Constructor of PairManager
|
// Constructor of PairManager
|
||||||
PairManager::PairManager(CollisionDetection& collisionDetection) : collisionDetection(collisionDetection) {
|
PairManager::PairManager(CollisionDetection& collisionDetection) : collisionDetection(collisionDetection) {
|
||||||
hashTable = 0;
|
hashTable = 0;
|
||||||
|
@ -59,8 +62,8 @@ BroadPhasePair* PairManager::addPair(Body* body1, Body* body2) {
|
||||||
sortBodiesUsingID(body1, body2);
|
sortBodiesUsingID(body1, body2);
|
||||||
|
|
||||||
// Get the bodies IDs
|
// Get the bodies IDs
|
||||||
luint id1 = body1->getID();
|
bodyindex id1 = body1->getID();
|
||||||
luint id2 = body2->getID();
|
bodyindex id2 = body2->getID();
|
||||||
|
|
||||||
// Compute the hash value of the two bodies
|
// Compute the hash value of the two bodies
|
||||||
uint hashValue = computeHashBodies(id1, id2) & hashMask;
|
uint hashValue = computeHashBodies(id1, id2) & hashMask;
|
||||||
|
@ -107,7 +110,7 @@ BroadPhasePair* PairManager::addPair(Body* body1, Body* body2) {
|
||||||
|
|
||||||
// Remove a pair of bodies from the pair manager. This method returns
|
// Remove a pair of bodies from the pair manager. This method returns
|
||||||
// true if the pair has been found and removed.
|
// true if the pair has been found and removed.
|
||||||
bool PairManager::removePair(luint id1, luint id2) {
|
bool PairManager::removePair(bodyindex id1, bodyindex id2) {
|
||||||
|
|
||||||
// Sort the bodies IDs
|
// Sort the bodies IDs
|
||||||
sortIDs(id1, id2);
|
sortIDs(id1, id2);
|
||||||
|
@ -139,15 +142,15 @@ bool PairManager::removePair(luint id1, luint id2) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal method to remove a pair from the set of overlapping pair
|
// Internal method to remove a pair from the set of overlapping pair
|
||||||
void PairManager::removePairWithHashValue(luint id1, luint id2, luint hashValue, luint indexPair) {
|
void PairManager::removePairWithHashValue(bodyindex id1, bodyindex id2, luint hashValue, bodyindex indexPair) {
|
||||||
|
|
||||||
// Get the initial offset of the pairs with
|
// Get the initial offset of the pairs with
|
||||||
// the corresponding hash value
|
// the corresponding hash value
|
||||||
luint offset = hashTable[hashValue];
|
bodyindex offset = hashTable[hashValue];
|
||||||
assert(offset != INVALID_OFFSET);
|
assert(offset != INVALID_INDEX);
|
||||||
|
|
||||||
// Look for the pair in the set of overlapping pairs
|
// Look for the pair in the set of overlapping pairs
|
||||||
luint previousPair = INVALID_OFFSET;
|
bodyindex previousPair = INVALID_INDEX;
|
||||||
while(offset != indexPair) {
|
while(offset != indexPair) {
|
||||||
previousPair = offset;
|
previousPair = offset;
|
||||||
offset = offsetNextPair[offset];
|
offset = offsetNextPair[offset];
|
||||||
|
@ -155,7 +158,7 @@ void PairManager::removePairWithHashValue(luint id1, luint id2, luint hashValue,
|
||||||
|
|
||||||
// If the pair was the first one with this hash
|
// If the pair was the first one with this hash
|
||||||
// value in the hash table
|
// value in the hash table
|
||||||
if (previousPair == INVALID_OFFSET) {
|
if (previousPair == INVALID_INDEX) {
|
||||||
// Replace the pair to remove in the
|
// Replace the pair to remove in the
|
||||||
// hash table by the next one
|
// hash table by the next one
|
||||||
hashTable[hashValue] = offsetNextPair[indexPair];
|
hashTable[hashValue] = offsetNextPair[indexPair];
|
||||||
|
@ -167,7 +170,7 @@ void PairManager::removePairWithHashValue(luint id1, luint id2, luint hashValue,
|
||||||
offsetNextPair[previousPair] = offsetNextPair[indexPair];
|
offsetNextPair[previousPair] = offsetNextPair[indexPair];
|
||||||
}
|
}
|
||||||
|
|
||||||
const luint indexLastPair = nbOverlappingPairs - 1;
|
const bodyindex indexLastPair = nbOverlappingPairs - 1;
|
||||||
|
|
||||||
// If the pair to remove is the last one in the list
|
// If the pair to remove is the last one in the list
|
||||||
if (indexPair == indexLastPair) {
|
if (indexPair == indexLastPair) {
|
||||||
|
@ -185,19 +188,19 @@ void PairManager::removePairWithHashValue(luint id1, luint id2, luint hashValue,
|
||||||
const uint lastPairHashValue = computeHashBodies(lastPair->body1->getID(), lastPair->body2->getID()) & hashMask;
|
const uint lastPairHashValue = computeHashBodies(lastPair->body1->getID(), lastPair->body2->getID()) & hashMask;
|
||||||
|
|
||||||
// Compute the initial offset of the last pair
|
// Compute the initial offset of the last pair
|
||||||
luint offset = hashTable[lastPairHashValue];
|
bodyindex offset = hashTable[lastPairHashValue];
|
||||||
assert(offset != INVALID_OFFSET);
|
assert(offset != INVALID_INDEX);
|
||||||
|
|
||||||
// Go through the pairs with the same hash value
|
// Go through the pairs with the same hash value
|
||||||
// and find the offset of the last pair
|
// and find the offset of the last pair
|
||||||
luint previous = INVALID_OFFSET;
|
bodyindex previous = INVALID_INDEX;
|
||||||
while(offset != indexLastPair) {
|
while(offset != indexLastPair) {
|
||||||
previous = offset;
|
previous = offset;
|
||||||
offset = offsetNextPair[offset];
|
offset = offsetNextPair[offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the last pair is not the first one with this hash value
|
// If the last pair is not the first one with this hash value
|
||||||
if (previous != INVALID_OFFSET) {
|
if (previous != INVALID_INDEX) {
|
||||||
|
|
||||||
// Remove the offset of the last pair in the "nextOffset" array
|
// Remove the offset of the last pair in the "nextOffset" array
|
||||||
assert(offsetNextPair[previous] == indexLastPair);
|
assert(offsetNextPair[previous] == indexLastPair);
|
||||||
|
@ -220,16 +223,16 @@ void PairManager::removePairWithHashValue(luint id1, luint id2, luint hashValue,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look for a pair in the set of overlapping pairs
|
// Look for a pair in the set of overlapping pairs
|
||||||
BroadPhasePair* PairManager::lookForAPair(luint id1, luint id2, luint hashValue) const {
|
BroadPhasePair* PairManager::lookForAPair(bodyindex id1, bodyindex id2, luint hashValue) const {
|
||||||
|
|
||||||
// Look for the pair in the set of overlapping pairs
|
// Look for the pair in the set of overlapping pairs
|
||||||
luint offset = hashTable[hashValue];
|
bodyindex offset = hashTable[hashValue];
|
||||||
while (offset != INVALID_OFFSET && isDifferentPair(overlappingPairs[offset], id1, id2)) {
|
while (offset != INVALID_INDEX && isDifferentPair(overlappingPairs[offset], id1, id2)) {
|
||||||
offset = offsetNextPair[offset];
|
offset = offsetNextPair[offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the pair has not been found in the overlapping pairs
|
// If the pair has not been found in the overlapping pairs
|
||||||
if (offset == INVALID_OFFSET) {
|
if (offset == INVALID_INDEX) {
|
||||||
// Return null
|
// Return null
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -246,18 +249,18 @@ void PairManager::reallocatePairs() {
|
||||||
|
|
||||||
// Reallocate the hash table and initialize it
|
// Reallocate the hash table and initialize it
|
||||||
free(hashTable);
|
free(hashTable);
|
||||||
hashTable = (luint*) malloc(nbElementsHashTable * sizeof(luint));
|
hashTable = (bodyindex*) malloc(nbElementsHashTable * sizeof(bodyindex));
|
||||||
assert(hashTable);
|
assert(hashTable);
|
||||||
for (luint i=0; i<nbElementsHashTable; i++) {
|
for (bodyindex i=0; i<nbElementsHashTable; i++) {
|
||||||
hashTable[i] = INVALID_OFFSET;
|
hashTable[i] = INVALID_INDEX;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reallocate the overlapping pairs
|
// Reallocate the overlapping pairs
|
||||||
BroadPhasePair* newOverlappingPairs = (BroadPhasePair*) malloc(nbElementsHashTable * sizeof(BroadPhasePair));
|
BroadPhasePair* newOverlappingPairs = (BroadPhasePair*) malloc(nbElementsHashTable * sizeof(BroadPhasePair));
|
||||||
luint* newNextOffset = (luint*) malloc(nbElementsHashTable * sizeof(luint));
|
bodyindex* newOffsetNextPair = (bodyindex*) malloc(nbElementsHashTable * sizeof(bodyindex));
|
||||||
|
|
||||||
assert(newOverlappingPairs);
|
assert(newOverlappingPairs);
|
||||||
assert(newNextOffset);
|
assert(newOffsetNextPair);
|
||||||
|
|
||||||
// If there is already some overlapping pairs
|
// If there is already some overlapping pairs
|
||||||
if (nbOverlappingPairs) {
|
if (nbOverlappingPairs) {
|
||||||
|
@ -266,9 +269,9 @@ void PairManager::reallocatePairs() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recompute the hash table with the new hash values
|
// Recompute the hash table with the new hash values
|
||||||
for (luint i=0; i<nbOverlappingPairs; i++) {
|
for (bodyindex i=0; i<nbOverlappingPairs; i++) {
|
||||||
const uint newHashValue = computeHashBodies(overlappingPairs[i].body1->getID(), overlappingPairs[i].body2->getID()) & hashMask;
|
const uint newHashValue = computeHashBodies(overlappingPairs[i].body1->getID(), overlappingPairs[i].body2->getID()) & hashMask;
|
||||||
newNextOffset[i] = hashTable[newHashValue];
|
newOffsetNextPair[i] = hashTable[newHashValue];
|
||||||
hashTable[newHashValue] = i;
|
hashTable[newHashValue] = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,6 +281,6 @@ void PairManager::reallocatePairs() {
|
||||||
|
|
||||||
// Replace by the new data
|
// Replace by the new data
|
||||||
overlappingPairs = newOverlappingPairs;
|
overlappingPairs = newOverlappingPairs;
|
||||||
offsetNextPair = newNextOffset;
|
offsetNextPair = newOffsetNextPair;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,6 @@ struct BroadPhasePair {
|
||||||
Body* body2; // Pointer to the second body
|
Body* body2; // Pointer to the second body
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO : Change and use uint instead of luint here
|
|
||||||
|
|
||||||
/* --------------------------------------------------------------------
|
/* --------------------------------------------------------------------
|
||||||
Class PairManager :
|
Class PairManager :
|
||||||
|
@ -56,42 +55,42 @@ struct BroadPhasePair {
|
||||||
*/
|
*/
|
||||||
class PairManager {
|
class PairManager {
|
||||||
private :
|
private :
|
||||||
luint nbElementsHashTable; // Number of elements in the hash table
|
bodyindex nbElementsHashTable; // Number of elements in the hash table
|
||||||
uint hashMask; // Hash mask for the hash function
|
uint hashMask; // Hash mask for the hash function
|
||||||
luint nbOverlappingPairs; // Number of overlapping pairs
|
bodyindex nbOverlappingPairs; // Number of overlapping pairs
|
||||||
luint* hashTable; // Hash table that contains the offset of the first pair of the list of
|
bodyindex* hashTable; // Hash table that contains the offset of the first pair of the list of
|
||||||
// pairs with the same hash value in the "overlappingPairs" array
|
// pairs with the same hash value in the "overlappingPairs" array
|
||||||
luint* offsetNextPair; // Array that contains for each offset, the offset of the next pair with
|
bodyindex* offsetNextPair; // Array that contains for each offset, the offset of the next pair with
|
||||||
// the same hash value
|
// the same hash value
|
||||||
// for a given same hash value
|
// for a given same hash value
|
||||||
BroadPhasePair* overlappingPairs; // Array that contains the currently active pairs
|
BroadPhasePair* overlappingPairs; // Array that contains the currently active pairs
|
||||||
static const luint INVALID_OFFSET = 0xffffffff; // Invalid ID
|
static bodyindex INVALID_INDEX; // Invalid ID
|
||||||
CollisionDetection& collisionDetection; // Reference to the collision detection
|
CollisionDetection& collisionDetection; // Reference to the collision detection
|
||||||
|
|
||||||
void sortBodiesUsingID(Body*& body1, Body*& body2) const; // Sort the bodies according to their IDs (smallest ID first)
|
void sortBodiesUsingID(Body*& body1, Body*& body2) const; // Sort the bodies according to their IDs (smallest ID first)
|
||||||
void sortIDs(luint& id1, luint& id2) const; // Sort the IDs (smallest ID first)
|
void sortIDs(bodyindex& id1, bodyindex& id2) const; // Sort the IDs (smallest ID first)
|
||||||
bool isDifferentPair(const BroadPhasePair& pair1, luint pair2ID1,
|
bool isDifferentPair(const BroadPhasePair& pair1, bodyindex pair2ID1,
|
||||||
luint pair2ID2) const; // Return true if pair1 and pair2 are the same
|
bodyindex pair2ID2) const; // Return true if pair1 and pair2 are the same
|
||||||
uint computeHashBodies(uint id1, uint id2) const; // Compute the hash value of two bodies using their IDs
|
uint computeHashBodies(uint id1, uint id2) const; // Compute the hash value of two bodies using their IDs
|
||||||
int computeHash32Bits(int key) const; // This method returns an hash value for a 32 bits key
|
int computeHash32Bits(int key) const; // This method returns an hash value for a 32 bits key
|
||||||
luint computeNextPowerOfTwo(luint number) const; // Return the next power of two
|
luint computeNextPowerOfTwo(luint number) const; // Return the next power of two
|
||||||
void reallocatePairs(); // Reallocate memory for more pairs
|
void reallocatePairs(); // Reallocate memory for more pairs
|
||||||
void shrinkMemory(); // Shrink the allocated memory
|
void shrinkMemory(); // Shrink the allocated memory
|
||||||
luint computePairOffset(const BroadPhasePair* pair) const; // Compute the offset of a given pair
|
bodyindex computePairOffset(const BroadPhasePair* pair) const; // Compute the offset of a given pair
|
||||||
BroadPhasePair* lookForAPair(luint id1, luint id2,
|
BroadPhasePair* lookForAPair(bodyindex id1, bodyindex id2,
|
||||||
luint hashValue) const; // Look for a pair in the set of overlapping pairs
|
luint hashValue) const; // Look for a pair in the set of overlapping pairs
|
||||||
BroadPhasePair* findPairWithHashValue(luint id1, luint id2,
|
BroadPhasePair* findPairWithHashValue(bodyindex id1, bodyindex id2,
|
||||||
luint hashValue) const; // Find a pair given two body IDs and an hash value
|
luint hashValue) const; // Find a pair given two body IDs and an hash value
|
||||||
void removePairWithHashValue(luint id1, luint id2, luint hashValue,
|
void removePairWithHashValue(bodyindex id1, bodyindex id2, luint hashValue,
|
||||||
luint indexPair); // Remove a pair from the set of active pair
|
bodyindex indexPair); // Remove a pair from the set of active pair
|
||||||
public :
|
public :
|
||||||
PairManager(CollisionDetection& collisionDetection); // Constructor
|
PairManager(CollisionDetection& collisionDetection); // Constructor
|
||||||
~PairManager(); // Destructor
|
~PairManager(); // Destructor
|
||||||
|
|
||||||
uint getNbOverlappingPairs() const; // Return the number of active pairs
|
bodyindex getNbOverlappingPairs() const; // Return the number of active pairs
|
||||||
BroadPhasePair* addPair(Body* body1, Body* body2); // Add a pair of bodies in the pair manager
|
BroadPhasePair* addPair(Body* body1, Body* body2); // Add a pair of bodies in the pair manager
|
||||||
bool removePair(luint id1, luint id2); // Remove a pair of bodies from the pair manager
|
bool removePair(bodyindex id1, bodyindex id2); // Remove a pair of bodies from the pair manager
|
||||||
BroadPhasePair* findPair(luint id1, luint id2) const; // Find a pair given two body IDs
|
BroadPhasePair* findPair(bodyindex id1, bodyindex id2) const; // Find a pair given two body IDs
|
||||||
BroadPhasePair* beginOverlappingPairsPointer() const; // Return a pointer to the first overlapping pair (used to iterate over the active pairs)
|
BroadPhasePair* beginOverlappingPairsPointer() const; // Return a pointer to the first overlapping pair (used to iterate over the active pairs)
|
||||||
BroadPhasePair* endOverlappingPairsPointer() const; // Return a pointer to the last overlapping pair (used to iterate over the active pairs)
|
BroadPhasePair* endOverlappingPairsPointer() const; // Return a pointer to the last overlapping pair (used to iterate over the active pairs)
|
||||||
void registerAddedOverlappingPairCallback(void (CollisionDetection::*callbackFunction) (const BroadPhasePair* addedActivePair)); // Register a callback function (using a function pointer) that will be called when a new overlapping pair is added in the pair manager
|
void registerAddedOverlappingPairCallback(void (CollisionDetection::*callbackFunction) (const BroadPhasePair* addedActivePair)); // Register a callback function (using a function pointer) that will be called when a new overlapping pair is added in the pair manager
|
||||||
|
@ -101,7 +100,7 @@ class PairManager {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return the number of overlapping pairs
|
// Return the number of overlapping pairs
|
||||||
inline uint PairManager::getNbOverlappingPairs() const {
|
inline bodyindex PairManager::getNbOverlappingPairs() const {
|
||||||
return nbOverlappingPairs;
|
return nbOverlappingPairs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +110,7 @@ inline uint PairManager::computeHashBodies(uint id1, uint id2) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return true if pair1 and pair2 are the same
|
// Return true if pair1 and pair2 are the same
|
||||||
inline bool PairManager::isDifferentPair(const BroadPhasePair& pair1, luint pair2ID1, luint pair2ID2) const {
|
inline bool PairManager::isDifferentPair(const BroadPhasePair& pair1, bodyindex pair2ID1, bodyindex pair2ID2) const {
|
||||||
return (pair2ID1 != pair1.body1->getID() || pair2ID2 != pair1.body2->getID());
|
return (pair2ID1 != pair1.body1->getID() || pair2ID2 != pair1.body2->getID());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,9 +139,9 @@ inline void PairManager::sortBodiesUsingID(Body*& body1, Body*& body2) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort the IDs (smallest ID first)
|
// Sort the IDs (smallest ID first)
|
||||||
inline void PairManager::sortIDs(luint& id1, luint& id2) const {
|
inline void PairManager::sortIDs(bodyindex &id1, bodyindex &id2) const {
|
||||||
if (id1 > id2) {
|
if (id1 > id2) {
|
||||||
luint temp = id2;
|
bodyindex temp = id2;
|
||||||
id2 = id1;
|
id2 = id1;
|
||||||
id1 = temp;
|
id1 = temp;
|
||||||
}
|
}
|
||||||
|
@ -162,7 +161,7 @@ inline int PairManager::computeHash32Bits(int key) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find a pair given two body IDs
|
// Find a pair given two body IDs
|
||||||
inline BroadPhasePair* PairManager::findPair(luint id1, luint id2) const {
|
inline BroadPhasePair* PairManager::findPair(bodyindex id1, bodyindex id2) const {
|
||||||
|
|
||||||
// Check if the hash table has been allocated yet
|
// Check if the hash table has been allocated yet
|
||||||
if (!hashTable) return 0;
|
if (!hashTable) return 0;
|
||||||
|
@ -180,7 +179,7 @@ inline BroadPhasePair* PairManager::findPair(luint id1, luint id2) const {
|
||||||
// Find a pair given two body IDs and an hash value
|
// Find a pair given two body IDs and an hash value
|
||||||
// This internal version is used to avoid computing multiple times in the
|
// This internal version is used to avoid computing multiple times in the
|
||||||
// caller method
|
// caller method
|
||||||
inline BroadPhasePair* PairManager::findPairWithHashValue(luint id1, luint id2, luint hashValue) const {
|
inline BroadPhasePair* PairManager::findPairWithHashValue(bodyindex id1, bodyindex id2, luint hashValue) const {
|
||||||
|
|
||||||
// Check if the hash table has been allocated yet
|
// Check if the hash table has been allocated yet
|
||||||
if (!hashTable) return 0;
|
if (!hashTable) return 0;
|
||||||
|
@ -193,7 +192,7 @@ inline BroadPhasePair* PairManager::findPairWithHashValue(luint id1, luint id2,
|
||||||
inline void PairManager::shrinkMemory() {
|
inline void PairManager::shrinkMemory() {
|
||||||
|
|
||||||
// Check if the allocated memory can be reduced
|
// Check if the allocated memory can be reduced
|
||||||
const luint correctNbElementsHashTable = computeNextPowerOfTwo(nbOverlappingPairs);
|
const bodyindex correctNbElementsHashTable = computeNextPowerOfTwo(nbOverlappingPairs);
|
||||||
if (nbElementsHashTable == correctNbElementsHashTable) return;
|
if (nbElementsHashTable == correctNbElementsHashTable) return;
|
||||||
|
|
||||||
// Reduce the allocated memory
|
// Reduce the allocated memory
|
||||||
|
@ -203,8 +202,8 @@ inline void PairManager::shrinkMemory() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute the offset of a given pair in the array of overlapping pairs
|
// Compute the offset of a given pair in the array of overlapping pairs
|
||||||
inline luint PairManager::computePairOffset(const BroadPhasePair* pair) const {
|
inline bodyindex PairManager::computePairOffset(const BroadPhasePair* pair) const {
|
||||||
return ((luint)((size_t(pair) - size_t(overlappingPairs))) / sizeof(BroadPhasePair));
|
return ((bodyindex)((size_t(pair) - size_t(overlappingPairs))) / sizeof(BroadPhasePair));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return a pointer to the first overlapping pair (used to iterate over the overlapping pairs) or
|
// Return a pointer to the first overlapping pair (used to iterate over the overlapping pairs) or
|
||||||
|
|
|
@ -33,6 +33,9 @@
|
||||||
using namespace reactphysics3d;
|
using namespace reactphysics3d;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
// Initialization of static variables
|
||||||
|
bodyindex SweepAndPruneAlgorithm::INVALID_INDEX = std::numeric_limits<reactphysics3d::bodyindex>::max();
|
||||||
|
|
||||||
// Constructor of AABBInt
|
// Constructor of AABBInt
|
||||||
AABBInt::AABBInt(const AABB& aabb) {
|
AABBInt::AABBInt(const AABB& aabb) {
|
||||||
for (int axis=0; axis<3; axis++) {
|
for (int axis=0; axis<3; axis++) {
|
||||||
|
@ -63,7 +66,7 @@ SweepAndPruneAlgorithm::~SweepAndPruneAlgorithm() {
|
||||||
// Notify the broad-phase about a new object in the world
|
// Notify the broad-phase about a new object in the world
|
||||||
// This method adds the AABB of the object ion to broad-phase
|
// This method adds the AABB of the object ion to broad-phase
|
||||||
void SweepAndPruneAlgorithm::addObject(Body* body, const AABB& aabb) {
|
void SweepAndPruneAlgorithm::addObject(Body* body, const AABB& aabb) {
|
||||||
luint boxIndex;
|
bodyindex boxIndex;
|
||||||
|
|
||||||
// If the index of the first free box is valid (means that
|
// If the index of the first free box is valid (means that
|
||||||
// there is a bucket in the middle of the array that doesn't
|
// there is a bucket in the middle of the array that doesn't
|
||||||
|
@ -86,7 +89,7 @@ void SweepAndPruneAlgorithm::addObject(Body* body, const AABB& aabb) {
|
||||||
// at the end-points array in all three axis
|
// at the end-points array in all three axis
|
||||||
const luint nbSentinels = 2;
|
const luint nbSentinels = 2;
|
||||||
const luint indexLimitEndPoint = 2 * nbBoxes + nbSentinels - 1;
|
const luint indexLimitEndPoint = 2 * nbBoxes + nbSentinels - 1;
|
||||||
for (int axis=0; axis<3; axis++) {
|
for (uint axis=0; axis<3; axis++) {
|
||||||
EndPoint* maxLimitEndPoint = &endPoints[axis][indexLimitEndPoint];
|
EndPoint* maxLimitEndPoint = &endPoints[axis][indexLimitEndPoint];
|
||||||
assert(endPoints[axis][0].boxID == INVALID_INDEX && endPoints[axis][0].isMin == true);
|
assert(endPoints[axis][0].boxID == INVALID_INDEX && endPoints[axis][0].isMin == true);
|
||||||
assert(maxLimitEndPoint->boxID == INVALID_INDEX && maxLimitEndPoint->isMin == false);
|
assert(maxLimitEndPoint->boxID == INVALID_INDEX && maxLimitEndPoint->isMin == false);
|
||||||
|
@ -109,7 +112,7 @@ void SweepAndPruneAlgorithm::addObject(Body* body, const AABB& aabb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the body pointer to box index mapping
|
// Add the body pointer to box index mapping
|
||||||
mapBodyToBoxIndex.insert(pair<Body*, luint>(body, boxIndex));
|
mapBodyToBoxIndex.insert(pair<Body*, bodyindex>(body, boxIndex));
|
||||||
|
|
||||||
nbBoxes++;
|
nbBoxes++;
|
||||||
|
|
||||||
|
@ -131,7 +134,7 @@ void SweepAndPruneAlgorithm::removeObject(Body* body) {
|
||||||
updateObject(body, aabb);
|
updateObject(body, aabb);
|
||||||
|
|
||||||
// Get the corresponding box
|
// Get the corresponding box
|
||||||
luint boxIndex = mapBodyToBoxIndex[body];
|
bodyindex boxIndex = mapBodyToBoxIndex[body];
|
||||||
BoxAABB* box = &boxes[boxIndex];
|
BoxAABB* box = &boxes[boxIndex];
|
||||||
|
|
||||||
// Add the box index into the list of free indices
|
// Add the box index into the list of free indices
|
||||||
|
@ -148,7 +151,7 @@ void SweepAndPruneAlgorithm::updateObject(Body* body, const AABB& aabb) {
|
||||||
AABBInt aabbInt(aabb);
|
AABBInt aabbInt(aabb);
|
||||||
|
|
||||||
// Get the corresponding box
|
// Get the corresponding box
|
||||||
luint boxIndex = mapBodyToBoxIndex[body];
|
bodyindex boxIndex = mapBodyToBoxIndex[body];
|
||||||
BoxAABB* box = &boxes[boxIndex];
|
BoxAABB* box = &boxes[boxIndex];
|
||||||
|
|
||||||
// Current axis
|
// Current axis
|
||||||
|
|
|
@ -42,15 +42,15 @@ struct EndPoint {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// TODO : Use uint here
|
// TODO : Use uint here
|
||||||
luint boxID; // ID of the AABB box corresponding to this end-point
|
bodyindex boxID; // ID of the AABB box corresponding to this end-point
|
||||||
bool isMin; // True if the end-point is a minimum end-point of a box
|
bool isMin; // True if the end-point is a minimum end-point of a box
|
||||||
uint value; // Value (one dimension coordinate) of the end-point
|
uint value; // Value (one dimension coordinate) of the end-point
|
||||||
|
|
||||||
void setValues(luint boxID, bool isMin, uint value); // Set the values of the endpoint
|
void setValues(bodyindex boxID, bool isMin, uint value); // Set the values of the endpoint
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set the values of the endpoint
|
// Set the values of the endpoint
|
||||||
inline void EndPoint::setValues(luint boxID, bool isMin, uint value) {
|
inline void EndPoint::setValues(bodyindex boxID, bool isMin, uint value) {
|
||||||
this->boxID = boxID;
|
this->boxID = boxID;
|
||||||
this->isMin = isMin;
|
this->isMin = isMin;
|
||||||
this->value = value;
|
this->value = value;
|
||||||
|
@ -60,8 +60,8 @@ inline void EndPoint::setValues(luint boxID, bool isMin, uint value) {
|
||||||
// Sweep-And-Prune algorithm
|
// Sweep-And-Prune algorithm
|
||||||
struct BoxAABB {
|
struct BoxAABB {
|
||||||
public:
|
public:
|
||||||
luint min[3]; // Index of the three minimum end-points of the AABB over the axis X, Y and Z
|
bodyindex min[3]; // Index of the three minimum end-points of the AABB over the axis X, Y and Z
|
||||||
luint max[3]; // Index of the three maximum end-points of the AABB over the axis X, Y and Z
|
bodyindex max[3]; // Index of the three maximum end-points of the AABB over the axis X, Y and Z
|
||||||
Body* body; // Body that corresponds to the owner of the AABB
|
Body* body; // Body that corresponds to the owner of the AABB
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -75,12 +75,6 @@ struct AABBInt {
|
||||||
AABBInt(const AABB& aabb); // Constructor
|
AABBInt(const AABB& aabb); // Constructor
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO : Use uint instead of luint here
|
|
||||||
|
|
||||||
// TODO : Rename the methods and variable to my way
|
|
||||||
|
|
||||||
// TODO : Try to replace the Body* pointer by the body ID everywhere in order that
|
|
||||||
// this class can be used in a more generic way
|
|
||||||
|
|
||||||
/* --------------------------------------------------------------------
|
/* --------------------------------------------------------------------
|
||||||
Class SweepAndPruneAlgorithm :
|
Class SweepAndPruneAlgorithm :
|
||||||
|
@ -93,21 +87,21 @@ struct AABBInt {
|
||||||
class SweepAndPruneAlgorithm : public BroadPhaseAlgorithm {
|
class SweepAndPruneAlgorithm : public BroadPhaseAlgorithm {
|
||||||
|
|
||||||
protected :
|
protected :
|
||||||
static const luint INVALID_INDEX = ULONG_MAX; // Invalid array index
|
static bodyindex INVALID_INDEX; // Invalid array index
|
||||||
|
|
||||||
BoxAABB* boxes; // Array that contains all the AABB boxes of the broad-phase
|
BoxAABB* boxes; // Array that contains all the AABB boxes of the broad-phase
|
||||||
EndPoint* endPoints[3]; // Array of end-points on the three axis
|
EndPoint* endPoints[3]; // Array of end-points on the three axis
|
||||||
luint nbBoxes; // Number of AABB boxes in the broad-phase
|
bodyindex nbBoxes; // Number of AABB boxes in the broad-phase
|
||||||
luint nbMaxBoxes; // Maximum number of boxes in the boxes array
|
bodyindex nbMaxBoxes; // Maximum number of boxes in the boxes array
|
||||||
std::vector<luint> freeBoxIndices; // Indices that are not used by any boxes
|
std::vector<bodyindex> freeBoxIndices; // Indices that are not used by any boxes
|
||||||
std::map<Body*, luint> mapBodyToBoxIndex; // Map a body pointer to its box index
|
std::map<Body*, bodyindex> mapBodyToBoxIndex; // Map a body pointer to its box index
|
||||||
|
|
||||||
void resizeArrays(); // Resize the boxes and end-points arrays when it's full
|
void resizeArrays(); // Resize the boxes and end-points arrays when it's full
|
||||||
void addPair(Body* body1, Body* body2); // Add an overlapping pair of AABBS
|
void addPair(Body* body1, Body* body2); // Add an overlapping pair of AABBS
|
||||||
bool testIntersect1DSortedAABBs(const BoxAABB& box1, const AABBInt& box2,
|
bool testIntersect1DSortedAABBs(const BoxAABB& box1, const AABBInt& box2,
|
||||||
const EndPoint* const baseEndPoint, luint axis) const; // Check for 1D box intersection
|
const EndPoint* const baseEndPoint, uint axis) const; // Check for 1D box intersection
|
||||||
bool testIntersect2D(const BoxAABB& box1, const BoxAABB& box2,
|
bool testIntersect2D(const BoxAABB& box1, const BoxAABB& box2,
|
||||||
luint axis1, luint axis2) const; // Check for 2D box intersection
|
luint axis1, uint axis2) const; // Check for 2D box intersection
|
||||||
|
|
||||||
public :
|
public :
|
||||||
SweepAndPruneAlgorithm(CollisionDetection& collisionDetection); // Constructor
|
SweepAndPruneAlgorithm(CollisionDetection& collisionDetection); // Constructor
|
||||||
|
@ -138,7 +132,7 @@ inline uint encodeFloatIntoInteger(float number) {
|
||||||
// given axis. Therefore, only one test is necessary here. We know that the
|
// given axis. Therefore, only one test is necessary here. We know that the
|
||||||
// minimum of box1 cannot be larger that the maximum of box2 on the axis.
|
// minimum of box1 cannot be larger that the maximum of box2 on the axis.
|
||||||
inline bool SweepAndPruneAlgorithm::testIntersect1DSortedAABBs(const BoxAABB& box1, const AABBInt& box2,
|
inline bool SweepAndPruneAlgorithm::testIntersect1DSortedAABBs(const BoxAABB& box1, const AABBInt& box2,
|
||||||
const EndPoint* const endPointsArray, luint axis) const {
|
const EndPoint* const endPointsArray, uint axis) const {
|
||||||
return !(endPointsArray[box1.max[axis]].value < box2.min[axis]);
|
return !(endPointsArray[box1.max[axis]].value < box2.min[axis]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,7 +140,7 @@ inline bool SweepAndPruneAlgorithm::testIntersect1DSortedAABBs(const BoxAABB& bo
|
||||||
// that two boxes already overlap on one axis and when want to test
|
// that two boxes already overlap on one axis and when want to test
|
||||||
// if they also overlap on the two others axis.
|
// if they also overlap on the two others axis.
|
||||||
inline bool SweepAndPruneAlgorithm::testIntersect2D(const BoxAABB& box1, const BoxAABB& box2,
|
inline bool SweepAndPruneAlgorithm::testIntersect2D(const BoxAABB& box1, const BoxAABB& box2,
|
||||||
luint axis1, luint axis2) const {
|
luint axis1, uint axis2) const {
|
||||||
return !(box2.max[axis1] < box1.min[axis1] || box1.max[axis1] < box2.min[axis1] ||
|
return !(box2.max[axis1] < box1.min[axis1] || box1.max[axis1] < box2.min[axis1] ||
|
||||||
box2.max[axis2] < box1.min[axis2] || box1.max[axis2] < box2.min[axis2]);
|
box2.max[axis2] < box1.min[axis2] || box1.max[axis2] < box2.min[axis2]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,9 @@
|
||||||
#define LINUX_OS
|
#define LINUX_OS
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Namespace reactphysics3d
|
||||||
|
namespace reactphysics3d {
|
||||||
|
|
||||||
// Type definitions
|
// Type definitions
|
||||||
typedef unsigned int uint;
|
typedef unsigned int uint;
|
||||||
typedef long unsigned int luint;
|
typedef long unsigned int luint;
|
||||||
|
@ -75,5 +78,6 @@ const uint DEFAULT_LCP_ITERATIONS_ERROR_CORRECTION = 5;
|
||||||
const bool ERROR_CORRECTION_PROJECTION_ENABLED = true; // True if the error correction projection (first order world) is active in the constraint solver
|
const bool ERROR_CORRECTION_PROJECTION_ENABLED = true; // True if the error correction projection (first order world) is active in the constraint solver
|
||||||
const reactphysics3d::decimal PENETRATION_DEPTH_THRESHOLD_ERROR_CORRECTION = 0.20; // Contacts with penetration depth (in meters) larger that this use error correction with projection
|
const reactphysics3d::decimal PENETRATION_DEPTH_THRESHOLD_ERROR_CORRECTION = 0.20; // Contacts with penetration depth (in meters) larger that this use error correction with projection
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -46,7 +46,7 @@ PhysicsWorld::~PhysicsWorld() {
|
||||||
RigidBody* PhysicsWorld::createRigidBody(const Transform& transform, decimal mass, const Matrix3x3& inertiaTensorLocal, Collider* collider) {
|
RigidBody* PhysicsWorld::createRigidBody(const Transform& transform, decimal mass, const Matrix3x3& inertiaTensorLocal, Collider* collider) {
|
||||||
|
|
||||||
// Compute the body ID
|
// Compute the body ID
|
||||||
luint bodyID;
|
bodyindex bodyID;
|
||||||
if (!freeRigidBodyIDs.empty()) {
|
if (!freeRigidBodyIDs.empty()) {
|
||||||
bodyID = freeRigidBodyIDs.back();
|
bodyID = freeRigidBodyIDs.back();
|
||||||
freeRigidBodyIDs.pop_back();
|
freeRigidBodyIDs.pop_back();
|
||||||
|
|
|
@ -51,13 +51,13 @@ namespace reactphysics3d {
|
||||||
class PhysicsWorld {
|
class PhysicsWorld {
|
||||||
protected :
|
protected :
|
||||||
CollisionDetection* collisionDetection; // Reference to the collision detection
|
CollisionDetection* collisionDetection; // Reference to the collision detection
|
||||||
std::set<Body*> bodies; // All the bodies (rigid and soft) of the physics world
|
std::set<Body*> bodies; // All the bodies (rigid and soft) of the physics world
|
||||||
std::set<RigidBody*> rigidBodies; // All the rigid bodies of the physics world
|
std::set<RigidBody*> rigidBodies; // All the rigid bodies of the physics world
|
||||||
std::vector<luint> freeRigidBodyIDs; // List of free ID for rigid bodies
|
std::vector<luint> freeRigidBodyIDs; // List of free ID for rigid bodies
|
||||||
std::vector<Constraint*> constraints; // List that contains all the current constraints
|
std::vector<Constraint*> constraints; // List that contains all the current constraints
|
||||||
Vector3 gravity; // Gravity vector of the world
|
Vector3 gravity; // Gravity vector of the world
|
||||||
bool isGravityOn; // True if the gravity force is on
|
bool isGravityOn; // True if the gravity force is on
|
||||||
luint currentBodyID; // Current body ID
|
bodyindex currentBodyID; // Current body ID
|
||||||
MemoryPool<RigidBody> memoryPoolRigidBodies; // Memory pool for rigid bodies memory allocation
|
MemoryPool<RigidBody> memoryPoolRigidBodies; // Memory pool for rigid bodies memory allocation
|
||||||
|
|
||||||
public :
|
public :
|
||||||
|
|
Loading…
Reference in New Issue
Block a user