From 5467e09020e8f47f58a7aa760ca2b94df89d0e25 Mon Sep 17 00:00:00 2001 From: "chappuis.daniel" Date: Thu, 10 Nov 2011 23:21:45 +0000 Subject: [PATCH] Remove dynamic casting git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@449 92aac97c-a6ce-11dd-a772-7fcde58d38e6 --- src/collision/CollisionDetection.cpp | 4 +- .../broadphase/BroadPhaseAlgorithm.h | 8 ++-- src/collision/broadphase/SAPAlgorithm.cpp | 8 ++-- src/collision/broadphase/SAPAlgorithm.h | 4 +- src/engine/PhysicsEngine.cpp | 11 ++--- src/engine/PhysicsEngine.h | 2 +- src/engine/PhysicsWorld.cpp | 4 +- src/engine/PhysicsWorld.h | 48 +++++++++---------- 8 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/collision/CollisionDetection.cpp b/src/collision/CollisionDetection.cpp index 130467fa..ca923a4d 100644 --- a/src/collision/CollisionDetection.cpp +++ b/src/collision/CollisionDetection.cpp @@ -79,8 +79,8 @@ bool CollisionDetection::computeCollisionDetection() { void CollisionDetection::computeBroadPhase() { // Notify the broad-phase algorithm about new and removed bodies in the physics world - broadPhaseAlgorithm->notifyAddedBodies(world->getAddedBodies()); - broadPhaseAlgorithm->notifyRemovedBodies(world->getRemovedBodies()); + broadPhaseAlgorithm->notifyAddedBodies(world->getAddedRigidBodies()); + broadPhaseAlgorithm->notifyRemovedBodies(world->getRemovedRigidBodies()); // Clear the set of the overlapping pairs in the current step currentStepOverlappingPairs.clear(); diff --git a/src/collision/broadphase/BroadPhaseAlgorithm.h b/src/collision/broadphase/BroadPhaseAlgorithm.h index aa81eaf1..1eb4aa8e 100644 --- a/src/collision/broadphase/BroadPhaseAlgorithm.h +++ b/src/collision/broadphase/BroadPhaseAlgorithm.h @@ -27,7 +27,7 @@ // Libraries #include -#include "../../body/Body.h" +#include "../../body/RigidBody.h" // Namespace ReactPhysics3D namespace reactphysics3d { @@ -56,9 +56,9 @@ class BroadPhaseAlgorithm { BroadPhaseAlgorithm(CollisionDetection& collisionDetection); // Constructor virtual ~BroadPhaseAlgorithm(); // Destructor - virtual void computePossibleCollisionPairs()=0; // Compute the possible collision pairs of bodies - virtual void notifyAddedBodies(std::vector bodies)=0; // Notify the broad-phase algorithm about new bodies in the physics world - virtual void notifyRemovedBodies(std::vector bodies)=0; // Notify the broad-phase algorithm about removed bodies in the physics world + virtual void computePossibleCollisionPairs()=0; // Compute the possible collision pairs of bodies + virtual void notifyAddedBodies(std::vector bodies)=0; // Notify the broad-phase algorithm about new bodies in the physics world + virtual void notifyRemovedBodies(std::vector bodies)=0; // Notify the broad-phase algorithm about removed bodies in the physics world }; } // End of reactphysics3d namespace diff --git a/src/collision/broadphase/SAPAlgorithm.cpp b/src/collision/broadphase/SAPAlgorithm.cpp index 78eb50ea..8b3caeb5 100644 --- a/src/collision/broadphase/SAPAlgorithm.cpp +++ b/src/collision/broadphase/SAPAlgorithm.cpp @@ -47,12 +47,12 @@ SAPAlgorithm::~SAPAlgorithm() { // Notify the broad-phase algorithm about new bodies in the physics world // This method removes the AABB representation of a given set of bodies from the sortedAABBs set -void SAPAlgorithm::notifyRemovedBodies(vector bodies) { +void SAPAlgorithm::notifyRemovedBodies(vector bodies) { vector::iterator elemToRemove; const AABB* aabb; // Removed the AABB of the bodies that have been removed - for (vector::iterator it = bodies.begin(); it != bodies.end(); ++it) { + for (vector::iterator it = bodies.begin(); it != bodies.end(); ++it) { aabb = (*it)->getAABB(); assert(aabb); elemToRemove = find(sortedAABBs.begin(), sortedAABBs.end(), aabb); @@ -63,10 +63,10 @@ void SAPAlgorithm::notifyRemovedBodies(vector bodies) { // Notify the broad-phase algorithm about new bodies in the physics world // This method adds the AABB representation of a given body in the sortedAABBs set -void SAPAlgorithm::notifyAddedBodies(vector bodies) { +void SAPAlgorithm::notifyAddedBodies(vector bodies) { const AABB* aabb; - for (vector::iterator it = bodies.begin(); it != bodies.end(); ++it) { + for (vector::iterator it = bodies.begin(); it != bodies.end(); ++it) { aabb = 0; aabb = (*it)->getAABB(); assert(aabb); diff --git a/src/collision/broadphase/SAPAlgorithm.h b/src/collision/broadphase/SAPAlgorithm.h index e18fc940..4b08caf4 100644 --- a/src/collision/broadphase/SAPAlgorithm.h +++ b/src/collision/broadphase/SAPAlgorithm.h @@ -63,8 +63,8 @@ class SAPAlgorithm : public BroadPhaseAlgorithm { virtual ~SAPAlgorithm(); // Destructor virtual void computePossibleCollisionPairs(); // Compute the possible collision pairs of bodies - virtual void notifyAddedBodies(std::vector bodies); // Notify the broad-phase algorithm about new bodies in the physics world - virtual void notifyRemovedBodies(std::vector bodies); // Notify the broad-phase algorithm about removed bodies in the physics world + virtual void notifyAddedBodies(std::vector bodies); // Notify the broad-phase algorithm about new bodies in the physics world + virtual void notifyRemovedBodies(std::vector bodies); // Notify the broad-phase algorithm about removed bodies in the physics world }; // Static method that compare two AABBs. This method will be used to compare to AABBs diff --git a/src/engine/PhysicsEngine.cpp b/src/engine/PhysicsEngine.cpp index d83f2999..dbd31463 100644 --- a/src/engine/PhysicsEngine.cpp +++ b/src/engine/PhysicsEngine.cpp @@ -100,9 +100,9 @@ void PhysicsEngine::updateAllBodiesMotion() { Vector3 newAngularVelocity; // For each body of thephysics world - for (vector::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); ++it) { + for (vector::iterator it=world->getRigidBodiesBeginIterator(); it != world->getRigidBodiesEndIterator(); ++it) { - RigidBody* rigidBody = dynamic_cast(*it); + RigidBody* rigidBody = *it; assert(rigidBody); // If the body is able to move @@ -138,10 +138,9 @@ void PhysicsEngine::updateAllBodiesMotion() { // Update the position and orientation of a body // Use the Semi-Implicit Euler (Sympletic Euler) method to compute the new position and the new // orientation of the body -void PhysicsEngine::updatePositionAndOrientationOfBody(Body* body, const Vector3& newLinVelocity, const Vector3& newAngVelocity) { +void PhysicsEngine::updatePositionAndOrientationOfBody(RigidBody* rigidBody, const Vector3& newLinVelocity, const Vector3& newAngVelocity) { double dt = timer.getTimeStep(); - RigidBody* rigidBody = dynamic_cast(body); assert(rigidBody); // Update the old position and orientation of the body @@ -168,7 +167,7 @@ void PhysicsEngine::setInterpolationFactorToAllBodies() { assert(factor >= 0.0 && factor <= 1.0); // Set the factor to all bodies - for (vector::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); ++it) { + for (vector::iterator it=world->getRigidBodiesBeginIterator(); it != world->getRigidBodiesEndIterator(); ++it) { RigidBody* rigidBody = dynamic_cast(*it); assert(rigidBody); @@ -181,7 +180,7 @@ void PhysicsEngine::setInterpolationFactorToAllBodies() { void PhysicsEngine::applyGravity() { // For each body of the physics world - for (vector::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); ++it) { + for (vector::iterator it=world->getRigidBodiesBeginIterator(); it != world->getRigidBodiesEndIterator(); ++it) { RigidBody* rigidBody = dynamic_cast(*it); assert(rigidBody); diff --git a/src/engine/PhysicsEngine.h b/src/engine/PhysicsEngine.h index 43cd1bbd..fb69501c 100644 --- a/src/engine/PhysicsEngine.h +++ b/src/engine/PhysicsEngine.h @@ -50,7 +50,7 @@ class PhysicsEngine { ConstraintSolver constraintSolver; // Constraint solver void updateAllBodiesMotion(); // Compute the motion of all bodies and update their positions and orientations - void updatePositionAndOrientationOfBody(Body* body, const Vector3& newLinVelocity, const Vector3& newAngVelocity); // Update the position and orientation of a body + void updatePositionAndOrientationOfBody(RigidBody* body, const Vector3& newLinVelocity, const Vector3& newAngVelocity); // Update the position and orientation of a body void setInterpolationFactorToAllBodies(); // Compute and set the interpolation factor to all bodies void applyGravity(); // Apply the gravity force to all bodies diff --git a/src/engine/PhysicsWorld.cpp b/src/engine/PhysicsWorld.cpp index 6645d4b1..918a906d 100644 --- a/src/engine/PhysicsWorld.cpp +++ b/src/engine/PhysicsWorld.cpp @@ -52,13 +52,13 @@ RigidBody* PhysicsWorld::createRigidBody(const Transform& transform, double mass currentBodyID++; // Add the rigid body to the physics world and return it - addBody(rigidBody); + addRigidBody(rigidBody); return rigidBody; } // Destroy a rigid body void PhysicsWorld::destroyRigidBody(RigidBody* rigidBody) { - removeBody(rigidBody); + removeRigidBody(rigidBody); delete rigidBody; } diff --git a/src/engine/PhysicsWorld.h b/src/engine/PhysicsWorld.h index a53cd8a4..a6d77f8e 100644 --- a/src/engine/PhysicsWorld.h +++ b/src/engine/PhysicsWorld.h @@ -47,16 +47,16 @@ namespace reactphysics3d { */ class PhysicsWorld { protected : - std::vector bodies; // All the rigid bodies of the physics world - std::vector addedBodies; // Added bodies since last update - std::vector removedBodies; // Removed bodies since last update + std::vector rigidBodies; // All the rigid bodies of the physics world + std::vector addedBodies; // Added bodies since last update + std::vector removedBodies; // Removed bodies since last update 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 long unsigned int currentBodyID; // Current body ID - void addBody(Body* body); // Add a body to the physics world - void removeBody(Body* body); // Remove a body from the physics world + void addRigidBody(RigidBody* body); // Add a body to the physics world + void removeRigidBody(RigidBody* body); // Remove a body from the physics world public : PhysicsWorld(const Vector3& gravity); // Constructor @@ -75,23 +75,23 @@ class PhysicsWorld { void removeAllConstraints(); // Remove all constraints and delete them (free their memory) std::vector::iterator getConstraintsBeginIterator(); // Return a start iterator on the constraint list std::vector::iterator getConstraintsEndIterator(); // Return a end iterator on the constraint list - std::vector::iterator getBodiesBeginIterator(); // Return an iterator to the beginning of the bodies of the physics world - std::vector::iterator getBodiesEndIterator(); // Return an iterator to the end of the bodies of the physics world - std::vector& getAddedBodies(); // Return the added bodies since last update of the physics engine - std::vector& getRemovedBodies(); // Retrun the removed bodies since last update of the physics engine + std::vector::iterator getRigidBodiesBeginIterator(); // Return an iterator to the beginning of the bodies of the physics world + std::vector::iterator getRigidBodiesEndIterator(); // Return an iterator to the end of the bodies of the physics world + std::vector& getAddedRigidBodies(); // Return the added bodies since last update of the physics engine + std::vector& getRemovedRigidBodies(); // Retrun the removed bodies since last update of the physics engine }; // Add a body to the physics world -inline void PhysicsWorld::addBody(Body* body) { - std::vector::iterator it; +inline void PhysicsWorld::addRigidBody(RigidBody* body) { + std::vector::iterator it; assert(body); - it = std::find(bodies.begin(), bodies.end(), body); - assert(it == bodies.end()); + it = std::find(rigidBodies.begin(), rigidBodies.end(), body); + assert(it == rigidBodies.end()); // The body isn't already in the bodyList, therefore we add it to the list - bodies.push_back(body); + rigidBodies.push_back(body); addedBodies.push_back(body); it = std::find(removedBodies.begin(), removedBodies.end(), body); if (it != removedBodies.end()) { @@ -100,13 +100,13 @@ inline void PhysicsWorld::addBody(Body* body) { } // Remove a body from the physics world -inline void PhysicsWorld::removeBody(Body* body) { - std::vector::iterator it; +inline void PhysicsWorld::removeRigidBody(RigidBody* body) { + std::vector::iterator it; assert(body); - it = std::find(bodies.begin(), bodies.end(), body); + it = std::find(rigidBodies.begin(), rigidBodies.end(), body); assert(*it == body); - bodies.erase(it); + rigidBodies.erase(it); it = std::find(addedBodies.begin(), addedBodies.end(), body); if (it != addedBodies.end()) { @@ -165,22 +165,22 @@ inline std::vector::iterator PhysicsWorld::getConstraintsEndIterato } // Return an iterator to the beginning of the bodies of the physics world -inline std::vector::iterator PhysicsWorld::getBodiesBeginIterator() { - return bodies.begin(); +inline std::vector::iterator PhysicsWorld::getRigidBodiesBeginIterator() { + return rigidBodies.begin(); } // Return an iterator to the end of the bodies of the physics world -inline std::vector::iterator PhysicsWorld::getBodiesEndIterator() { - return bodies.end(); +inline std::vector::iterator PhysicsWorld::getRigidBodiesEndIterator() { + return rigidBodies.end(); } // Return the added bodies since last update of the physics engine -inline std::vector& PhysicsWorld::getAddedBodies() { +inline std::vector& PhysicsWorld::getAddedRigidBodies() { return addedBodies; } // Retrun the removed bodies since last update of the physics engine -inline std::vector& PhysicsWorld::getRemovedBodies() { +inline std::vector& PhysicsWorld::getRemovedRigidBodies() { return removedBodies; }