From c7faae9b20b27c5a3565848279d5dbb6b04e8ed9 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Thu, 2 Aug 2012 23:33:56 +0200 Subject: [PATCH] Add the bodyindex type to represent the index number of a body --- src/body/Body.cpp | 2 +- src/body/Body.h | 10 ++-- src/body/RigidBody.cpp | 2 +- src/body/RigidBody.h | 12 ++--- src/collision/CollisionDetection.cpp | 10 ++-- src/collision/CollisionDetection.h | 12 ++--- src/collision/broadphase/PairManager.cpp | 53 ++++++++++--------- src/collision/broadphase/PairManager.h | 51 +++++++++--------- .../broadphase/SweepAndPruneAlgorithm.cpp | 13 +++-- .../broadphase/SweepAndPruneAlgorithm.h | 34 +++++------- src/configuration.h | 4 ++ src/engine/PhysicsWorld.cpp | 2 +- src/engine/PhysicsWorld.h | 6 +-- 13 files changed, 107 insertions(+), 104 deletions(-) diff --git a/src/body/Body.cpp b/src/body/Body.cpp index 2bdff9fe..7d7103bf 100644 --- a/src/body/Body.cpp +++ b/src/body/Body.cpp @@ -31,7 +31,7 @@ using namespace reactphysics3d; // 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) { assert(mass > 0.0); assert(collider); diff --git a/src/body/Body.h b/src/body/Body.h index 98dd957d..dac4a6a4 100644 --- a/src/body/Body.h +++ b/src/body/Body.h @@ -55,14 +55,14 @@ class Body { bool isMotionEnabled; // True if the body is able to move bool isCollisionEnabled; // True if the body can collide with others bodies 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 public : - Body(const Transform& transform, Collider* collider, decimal mass, long unsigned int id); // Constructor - virtual ~Body(); // Destructor + Body(const Transform& transform, Collider* collider, decimal mass, bodyindex id); // Constructor + 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 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 @@ -91,7 +91,7 @@ class Body { }; // Return the id of the body -inline luint Body::getID() const { +inline bodyindex Body::getID() const { return id; } diff --git a/src/body/RigidBody.cpp b/src/body/RigidBody.cpp index 1886af24..45967c40 100644 --- a/src/body/RigidBody.cpp +++ b/src/body/RigidBody.cpp @@ -31,7 +31,7 @@ using namespace reactphysics3d; // 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), inertiaTensorLocalInverse(inertiaTensorLocal.getInverse()), massInverse(1.0/mass) { diff --git a/src/body/RigidBody.h b/src/body/RigidBody.h index 80eed40b..7e59bfcb 100644 --- a/src/body/RigidBody.h +++ b/src/body/RigidBody.h @@ -47,7 +47,7 @@ class RigidBody : public Body { Vector3 linearVelocity; // Linear velocity of the body Vector3 angularVelocity; // Angular velocity of 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 inertiaTensorLocalInverse; // Inverse of the inertia tensor of the body (in body coordinates) decimal massInverse; // Inverse of the mass of the body @@ -55,7 +55,7 @@ class RigidBody : public Body { public : 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 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 Vector3 getExternalForce() const; // Return the current external force of 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 - void setExternalTorque(const Vector3& torque); // Set 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 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) 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; } -// Return the current external torque on the body +// Return the current external torque on the body inline Vector3 RigidBody::getExternalTorque() const { 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) { this->externalTorque = torque; } diff --git a/src/collision/CollisionDetection.cpp b/src/collision/CollisionDetection.cpp index 90d82722..58dbc6ba 100644 --- a/src/collision/CollisionDetection.cpp +++ b/src/collision/CollisionDetection.cpp @@ -58,7 +58,7 @@ CollisionDetection::CollisionDetection(PhysicsWorld* world) // Destructor CollisionDetection::~CollisionDetection() { // Delete the remaining overlapping pairs - for (map, OverlappingPair*>::iterator it=overlappingPairs.begin(); it != overlappingPairs.end(); it++) { + for (map, OverlappingPair*>::iterator it=overlappingPairs.begin(); it != overlappingPairs.end(); it++) { // Delete the overlapping pair (*it).second->OverlappingPair::~OverlappingPair(); memoryPoolOverlappingPairs.freeObject((*it).second); @@ -117,7 +117,7 @@ void CollisionDetection::computeBroadPhase() { // Compute the narrow-phase collision detection bool CollisionDetection::computeNarrowPhase() { bool collisionExists = false; - map, OverlappingPair*>::iterator it; + map, OverlappingPair*>::iterator it; // For each possible collision pair of bodies 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; // Construct the pair of body index - pair indexPair = addedPair->body1->getID() < addedPair->body2->getID() ? make_pair(addedPair->body1->getID(), addedPair->body2->getID()) : + pair indexPair = addedPair->body1->getID() < addedPair->body2->getID() ? make_pair(addedPair->body1->getID(), addedPair->body2->getID()) : make_pair(addedPair->body2->getID(), addedPair->body1->getID()); assert(indexPair.first != indexPair.second); // Add the pair into the set of overlapping pairs (if not there yet) OverlappingPair* newPair = new (memoryPoolOverlappingPairs.allocateObject()) OverlappingPair(addedPair->body1, addedPair->body2, memoryPoolContacts); - pair, OverlappingPair*>::iterator, bool> check = overlappingPairs.insert(make_pair(indexPair, newPair)); + pair, OverlappingPair*>::iterator, bool> check = overlappingPairs.insert(make_pair(indexPair, newPair)); // If the overlapping pair was already in the set of overlapping pair if (!check.second) { @@ -188,7 +188,7 @@ void CollisionDetection::broadPhaseNotifyAddedOverlappingPair(const BroadPhasePa void CollisionDetection::broadPhaseNotifyRemovedOverlappingPair(const BroadPhasePair* removedPair) { // Construct the pair of body index - pair indexPair = removedPair->body1->getID() < removedPair->body2->getID() ? make_pair(removedPair->body1->getID(), removedPair->body2->getID()) : + pair indexPair = removedPair->body1->getID() < removedPair->body2->getID() ? make_pair(removedPair->body1->getID(), removedPair->body2->getID()) : make_pair(removedPair->body2->getID(), removedPair->body1->getID()); // Remove the overlapping pair from the memory pool diff --git a/src/collision/CollisionDetection.h b/src/collision/CollisionDetection.h index ef5cf3e1..a0507a12 100644 --- a/src/collision/CollisionDetection.h +++ b/src/collision/CollisionDetection.h @@ -59,9 +59,9 @@ class PhysicsWorld; class CollisionDetection { private : PhysicsWorld* world; // Pointer to the physics world - std::map, OverlappingPair*> overlappingPairs; // Broad-phase overlapping pairs of bodies - std::set > currentStepOverlappingPairs; // Overlapping pairs of bodies at the current collision detection step - std::set > lastStepOverlappingPairs; // Overlapping pairs of bodies at the last collision detection step + std::map, OverlappingPair*> overlappingPairs; // Broad-phase overlapping pairs of bodies + std::set > currentStepOverlappingPairs; // Overlapping pairs of bodies at the current collision detection step + std::set > lastStepOverlappingPairs; // Overlapping pairs of bodies at the last collision detection step BroadPhaseAlgorithm* broadPhaseAlgorithm; // Broad-phase algorithm GJKAlgorithm narrowPhaseGJKAlgorithm; // Narrow-phase GJK 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 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 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 @@ -88,8 +88,8 @@ class CollisionDetection { // Return an overlapping pair of bodies according to the given bodies ID // The method returns null if the pair of bodies is not overlapping -inline OverlappingPair* CollisionDetection::getOverlappingPair(luint body1ID, luint body2ID) { - std::pair pair = (body1ID < body2ID) ? std::make_pair(body1ID, body2ID) : std::make_pair(body2ID, body1ID); +inline OverlappingPair* CollisionDetection::getOverlappingPair(bodyindex body1ID, bodyindex body2ID) { + std::pair pair = (body1ID < body2ID) ? std::make_pair(body1ID, body2ID) : std::make_pair(body2ID, body1ID); if (overlappingPairs.count(pair) == 1) { return overlappingPairs[pair]; } diff --git a/src/collision/broadphase/PairManager.cpp b/src/collision/broadphase/PairManager.cpp index 33b193c2..5b232270 100644 --- a/src/collision/broadphase/PairManager.cpp +++ b/src/collision/broadphase/PairManager.cpp @@ -30,6 +30,9 @@ using namespace reactphysics3d; +// Initialization of static variables +bodyindex PairManager::INVALID_INDEX = std::numeric_limits::max(); + // Constructor of PairManager PairManager::PairManager(CollisionDetection& collisionDetection) : collisionDetection(collisionDetection) { hashTable = 0; @@ -59,8 +62,8 @@ BroadPhasePair* PairManager::addPair(Body* body1, Body* body2) { sortBodiesUsingID(body1, body2); // Get the bodies IDs - luint id1 = body1->getID(); - luint id2 = body2->getID(); + bodyindex id1 = body1->getID(); + bodyindex id2 = body2->getID(); // Compute the hash value of the two bodies 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 // 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 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 -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 // the corresponding hash value - luint offset = hashTable[hashValue]; - assert(offset != INVALID_OFFSET); + bodyindex offset = hashTable[hashValue]; + assert(offset != INVALID_INDEX); // Look for the pair in the set of overlapping pairs - luint previousPair = INVALID_OFFSET; + bodyindex previousPair = INVALID_INDEX; while(offset != indexPair) { previousPair = 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 // value in the hash table - if (previousPair == INVALID_OFFSET) { + if (previousPair == INVALID_INDEX) { // Replace the pair to remove in the // hash table by the next one hashTable[hashValue] = offsetNextPair[indexPair]; @@ -167,7 +170,7 @@ void PairManager::removePairWithHashValue(luint id1, luint id2, luint hashValue, 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 (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; // Compute the initial offset of the last pair - luint offset = hashTable[lastPairHashValue]; - assert(offset != INVALID_OFFSET); + bodyindex offset = hashTable[lastPairHashValue]; + assert(offset != INVALID_INDEX); // Go through the pairs with the same hash value // and find the offset of the last pair - luint previous = INVALID_OFFSET; + bodyindex previous = INVALID_INDEX; while(offset != indexLastPair) { previous = offset; offset = offsetNextPair[offset]; } // 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 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 -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 - luint offset = hashTable[hashValue]; - while (offset != INVALID_OFFSET && isDifferentPair(overlappingPairs[offset], id1, id2)) { + bodyindex offset = hashTable[hashValue]; + while (offset != INVALID_INDEX && isDifferentPair(overlappingPairs[offset], id1, id2)) { offset = offsetNextPair[offset]; } // If the pair has not been found in the overlapping pairs - if (offset == INVALID_OFFSET) { + if (offset == INVALID_INDEX) { // Return null return 0; } @@ -246,18 +249,18 @@ void PairManager::reallocatePairs() { // Reallocate the hash table and initialize it free(hashTable); - hashTable = (luint*) malloc(nbElementsHashTable * sizeof(luint)); + hashTable = (bodyindex*) malloc(nbElementsHashTable * sizeof(bodyindex)); assert(hashTable); - for (luint i=0; igetID(), overlappingPairs[i].body2->getID()) & hashMask; - newNextOffset[i] = hashTable[newHashValue]; + newOffsetNextPair[i] = hashTable[newHashValue]; hashTable[newHashValue] = i; } @@ -278,6 +281,6 @@ void PairManager::reallocatePairs() { // Replace by the new data overlappingPairs = newOverlappingPairs; - offsetNextPair = newNextOffset; + offsetNextPair = newOffsetNextPair; } diff --git a/src/collision/broadphase/PairManager.h b/src/collision/broadphase/PairManager.h index 084354be..b39b521c 100644 --- a/src/collision/broadphase/PairManager.h +++ b/src/collision/broadphase/PairManager.h @@ -44,7 +44,6 @@ struct BroadPhasePair { Body* body2; // Pointer to the second body }; -// TODO : Change and use uint instead of luint here /* -------------------------------------------------------------------- Class PairManager : @@ -56,42 +55,42 @@ struct BroadPhasePair { */ class PairManager { 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 - luint nbOverlappingPairs; // Number of overlapping pairs - luint* hashTable; // Hash table that contains the offset of the first pair of the list of + bodyindex nbOverlappingPairs; // Number of overlapping pairs + 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 - 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 // for a given same hash value 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 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) - bool isDifferentPair(const BroadPhasePair& pair1, luint pair2ID1, - luint pair2ID2) const; // Return true if pair1 and pair2 are the same + void sortIDs(bodyindex& id1, bodyindex& id2) const; // Sort the IDs (smallest ID first) + bool isDifferentPair(const BroadPhasePair& pair1, bodyindex pair2ID1, + 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 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 void reallocatePairs(); // Reallocate memory for more pairs void shrinkMemory(); // Shrink the allocated memory - luint computePairOffset(const BroadPhasePair* pair) const; // Compute the offset of a given pair - BroadPhasePair* lookForAPair(luint id1, luint id2, + bodyindex computePairOffset(const BroadPhasePair* pair) const; // Compute the offset of a given pair + BroadPhasePair* lookForAPair(bodyindex id1, bodyindex id2, 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 - void removePairWithHashValue(luint id1, luint id2, luint hashValue, - luint indexPair); // Remove a pair from the set of active pair + void removePairWithHashValue(bodyindex id1, bodyindex id2, luint hashValue, + bodyindex indexPair); // Remove a pair from the set of active pair public : PairManager(CollisionDetection& collisionDetection); // Constructor ~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 - bool removePair(luint id1, luint id2); // Remove a pair of bodies from the pair manager - BroadPhasePair* findPair(luint id1, luint id2) const; // Find a pair given two body IDs + bool removePair(bodyindex id1, bodyindex id2); // Remove a pair of bodies from the pair manager + 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* 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 @@ -101,7 +100,7 @@ class PairManager { }; // Return the number of overlapping pairs -inline uint PairManager::getNbOverlappingPairs() const { +inline bodyindex PairManager::getNbOverlappingPairs() const { return nbOverlappingPairs; } @@ -111,7 +110,7 @@ inline uint PairManager::computeHashBodies(uint id1, uint id2) const { } // 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()); } @@ -140,9 +139,9 @@ inline void PairManager::sortBodiesUsingID(Body*& body1, Body*& body2) const { } // 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) { - luint temp = id2; + bodyindex temp = id2; id2 = id1; id1 = temp; } @@ -162,7 +161,7 @@ inline int PairManager::computeHash32Bits(int key) const { } // 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 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 // This internal version is used to avoid computing multiple times in the // 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 if (!hashTable) return 0; @@ -193,7 +192,7 @@ inline BroadPhasePair* PairManager::findPairWithHashValue(luint id1, luint id2, inline void PairManager::shrinkMemory() { // Check if the allocated memory can be reduced - const luint correctNbElementsHashTable = computeNextPowerOfTwo(nbOverlappingPairs); + const bodyindex correctNbElementsHashTable = computeNextPowerOfTwo(nbOverlappingPairs); if (nbElementsHashTable == correctNbElementsHashTable) return; // 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 -inline luint PairManager::computePairOffset(const BroadPhasePair* pair) const { - return ((luint)((size_t(pair) - size_t(overlappingPairs))) / sizeof(BroadPhasePair)); +inline bodyindex PairManager::computePairOffset(const BroadPhasePair* pair) const { + 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 diff --git a/src/collision/broadphase/SweepAndPruneAlgorithm.cpp b/src/collision/broadphase/SweepAndPruneAlgorithm.cpp index b1d6e77e..ab05c07e 100644 --- a/src/collision/broadphase/SweepAndPruneAlgorithm.cpp +++ b/src/collision/broadphase/SweepAndPruneAlgorithm.cpp @@ -33,6 +33,9 @@ using namespace reactphysics3d; using namespace std; +// Initialization of static variables +bodyindex SweepAndPruneAlgorithm::INVALID_INDEX = std::numeric_limits::max(); + // Constructor of AABBInt AABBInt::AABBInt(const AABB& aabb) { for (int axis=0; axis<3; axis++) { @@ -63,7 +66,7 @@ SweepAndPruneAlgorithm::~SweepAndPruneAlgorithm() { // Notify the broad-phase about a new object in the world // This method adds the AABB of the object ion to broad-phase void SweepAndPruneAlgorithm::addObject(Body* body, const AABB& aabb) { - luint boxIndex; + bodyindex boxIndex; // 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 @@ -86,7 +89,7 @@ void SweepAndPruneAlgorithm::addObject(Body* body, const AABB& aabb) { // at the end-points array in all three axis const luint nbSentinels = 2; 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]; assert(endPoints[axis][0].boxID == INVALID_INDEX && endPoints[axis][0].isMin == true); 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 - mapBodyToBoxIndex.insert(pair(body, boxIndex)); + mapBodyToBoxIndex.insert(pair(body, boxIndex)); nbBoxes++; @@ -131,7 +134,7 @@ void SweepAndPruneAlgorithm::removeObject(Body* body) { updateObject(body, aabb); // Get the corresponding box - luint boxIndex = mapBodyToBoxIndex[body]; + bodyindex boxIndex = mapBodyToBoxIndex[body]; BoxAABB* box = &boxes[boxIndex]; // 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); // Get the corresponding box - luint boxIndex = mapBodyToBoxIndex[body]; + bodyindex boxIndex = mapBodyToBoxIndex[body]; BoxAABB* box = &boxes[boxIndex]; // Current axis diff --git a/src/collision/broadphase/SweepAndPruneAlgorithm.h b/src/collision/broadphase/SweepAndPruneAlgorithm.h index a79f0239..298b2be7 100644 --- a/src/collision/broadphase/SweepAndPruneAlgorithm.h +++ b/src/collision/broadphase/SweepAndPruneAlgorithm.h @@ -42,15 +42,15 @@ struct EndPoint { public: // 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 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 -inline void EndPoint::setValues(luint boxID, bool isMin, uint value) { +inline void EndPoint::setValues(bodyindex boxID, bool isMin, uint value) { this->boxID = boxID; this->isMin = isMin; this->value = value; @@ -60,8 +60,8 @@ inline void EndPoint::setValues(luint boxID, bool isMin, uint value) { // Sweep-And-Prune algorithm struct BoxAABB { public: - luint 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 min[3]; // Index of the three minimum 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 }; @@ -75,12 +75,6 @@ struct AABBInt { 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 : @@ -93,21 +87,21 @@ struct AABBInt { class SweepAndPruneAlgorithm : public BroadPhaseAlgorithm { 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 EndPoint* endPoints[3]; // Array of end-points on the three axis - luint nbBoxes; // Number of AABB boxes in the broad-phase - luint nbMaxBoxes; // Maximum number of boxes in the boxes array - std::vector freeBoxIndices; // Indices that are not used by any boxes - std::map mapBodyToBoxIndex; // Map a body pointer to its box index + bodyindex nbBoxes; // Number of AABB boxes in the broad-phase + bodyindex nbMaxBoxes; // Maximum number of boxes in the boxes array + std::vector freeBoxIndices; // Indices that are not used by any boxes + std::map mapBodyToBoxIndex; // Map a body pointer to its box index 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 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, - luint axis1, luint axis2) const; // Check for 2D box intersection + luint axis1, uint axis2) const; // Check for 2D box intersection public : 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 // minimum of box1 cannot be larger that the maximum of box2 on the axis. 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]); } @@ -146,7 +140,7 @@ inline bool SweepAndPruneAlgorithm::testIntersect1DSortedAABBs(const BoxAABB& bo // that two boxes already overlap on one axis and when want to test // if they also overlap on the two others axis. 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] || box2.max[axis2] < box1.min[axis2] || box1.max[axis2] < box2.min[axis2]); } diff --git a/src/configuration.h b/src/configuration.h index 36cf5948..b1a12f81 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -40,6 +40,9 @@ #define LINUX_OS #endif +// Namespace reactphysics3d +namespace reactphysics3d { + // Type definitions typedef unsigned int uint; 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 reactphysics3d::decimal PENETRATION_DEPTH_THRESHOLD_ERROR_CORRECTION = 0.20; // Contacts with penetration depth (in meters) larger that this use error correction with projection +} #endif diff --git a/src/engine/PhysicsWorld.cpp b/src/engine/PhysicsWorld.cpp index 29bd04d3..1e48f000 100644 --- a/src/engine/PhysicsWorld.cpp +++ b/src/engine/PhysicsWorld.cpp @@ -46,7 +46,7 @@ PhysicsWorld::~PhysicsWorld() { RigidBody* PhysicsWorld::createRigidBody(const Transform& transform, decimal mass, const Matrix3x3& inertiaTensorLocal, Collider* collider) { // Compute the body ID - luint bodyID; + bodyindex bodyID; if (!freeRigidBodyIDs.empty()) { bodyID = freeRigidBodyIDs.back(); freeRigidBodyIDs.pop_back(); diff --git a/src/engine/PhysicsWorld.h b/src/engine/PhysicsWorld.h index 1434ac4d..77f7bbfd 100644 --- a/src/engine/PhysicsWorld.h +++ b/src/engine/PhysicsWorld.h @@ -51,13 +51,13 @@ namespace reactphysics3d { class PhysicsWorld { protected : CollisionDetection* collisionDetection; // Reference to the collision detection - std::set bodies; // All the bodies (rigid and soft) of the physics world - std::set rigidBodies; // All the rigid bodies of the physics world + std::set bodies; // All the bodies (rigid and soft) of the physics world + std::set rigidBodies; // All the rigid bodies of the physics world std::vector freeRigidBodyIDs; // List of free ID for rigid bodies std::vector constraints; // List that contains all the current constraints Vector3 gravity; // Gravity vector of the world bool isGravityOn; // True if the gravity force is on - luint currentBodyID; // Current body ID + bodyindex currentBodyID; // Current body ID MemoryPool memoryPoolRigidBodies; // Memory pool for rigid bodies memory allocation public :