Remove dynamic casting

git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@449 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
chappuis.daniel 2011-11-10 23:21:45 +00:00
parent 9bfb44597f
commit 5467e09020
8 changed files with 44 additions and 45 deletions

View File

@ -79,8 +79,8 @@ bool CollisionDetection::computeCollisionDetection() {
void CollisionDetection::computeBroadPhase() { void CollisionDetection::computeBroadPhase() {
// Notify the broad-phase algorithm about new and removed bodies in the physics world // Notify the broad-phase algorithm about new and removed bodies in the physics world
broadPhaseAlgorithm->notifyAddedBodies(world->getAddedBodies()); broadPhaseAlgorithm->notifyAddedBodies(world->getAddedRigidBodies());
broadPhaseAlgorithm->notifyRemovedBodies(world->getRemovedBodies()); broadPhaseAlgorithm->notifyRemovedBodies(world->getRemovedRigidBodies());
// Clear the set of the overlapping pairs in the current step // Clear the set of the overlapping pairs in the current step
currentStepOverlappingPairs.clear(); currentStepOverlappingPairs.clear();

View File

@ -27,7 +27,7 @@
// Libraries // Libraries
#include <vector> #include <vector>
#include "../../body/Body.h" #include "../../body/RigidBody.h"
// Namespace ReactPhysics3D // Namespace ReactPhysics3D
namespace reactphysics3d { namespace reactphysics3d {
@ -56,9 +56,9 @@ class BroadPhaseAlgorithm {
BroadPhaseAlgorithm(CollisionDetection& collisionDetection); // Constructor BroadPhaseAlgorithm(CollisionDetection& collisionDetection); // Constructor
virtual ~BroadPhaseAlgorithm(); // Destructor virtual ~BroadPhaseAlgorithm(); // Destructor
virtual void computePossibleCollisionPairs()=0; // Compute the possible collision pairs of bodies virtual void computePossibleCollisionPairs()=0; // Compute the possible collision pairs of bodies
virtual void notifyAddedBodies(std::vector<Body*> bodies)=0; // Notify the broad-phase algorithm about new bodies in the physics world virtual void notifyAddedBodies(std::vector<RigidBody*> bodies)=0; // Notify the broad-phase algorithm about new bodies in the physics world
virtual void notifyRemovedBodies(std::vector<Body*> bodies)=0; // Notify the broad-phase algorithm about removed bodies in the physics world virtual void notifyRemovedBodies(std::vector<RigidBody*> bodies)=0; // Notify the broad-phase algorithm about removed bodies in the physics world
}; };
} // End of reactphysics3d namespace } // End of reactphysics3d namespace

View File

@ -47,12 +47,12 @@ SAPAlgorithm::~SAPAlgorithm() {
// Notify the broad-phase algorithm about new bodies in the physics world // 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 // This method removes the AABB representation of a given set of bodies from the sortedAABBs set
void SAPAlgorithm::notifyRemovedBodies(vector<Body*> bodies) { void SAPAlgorithm::notifyRemovedBodies(vector<RigidBody*> bodies) {
vector<const AABB*>::iterator elemToRemove; vector<const AABB*>::iterator elemToRemove;
const AABB* aabb; const AABB* aabb;
// Removed the AABB of the bodies that have been removed // Removed the AABB of the bodies that have been removed
for (vector<Body*>::iterator it = bodies.begin(); it != bodies.end(); ++it) { for (vector<RigidBody*>::iterator it = bodies.begin(); it != bodies.end(); ++it) {
aabb = (*it)->getAABB(); aabb = (*it)->getAABB();
assert(aabb); assert(aabb);
elemToRemove = find(sortedAABBs.begin(), sortedAABBs.end(), aabb); elemToRemove = find(sortedAABBs.begin(), sortedAABBs.end(), aabb);
@ -63,10 +63,10 @@ void SAPAlgorithm::notifyRemovedBodies(vector<Body*> bodies) {
// Notify the broad-phase algorithm about new bodies in the physics world // 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 // This method adds the AABB representation of a given body in the sortedAABBs set
void SAPAlgorithm::notifyAddedBodies(vector<Body*> bodies) { void SAPAlgorithm::notifyAddedBodies(vector<RigidBody*> bodies) {
const AABB* aabb; const AABB* aabb;
for (vector<Body*>::iterator it = bodies.begin(); it != bodies.end(); ++it) { for (vector<RigidBody*>::iterator it = bodies.begin(); it != bodies.end(); ++it) {
aabb = 0; aabb = 0;
aabb = (*it)->getAABB(); aabb = (*it)->getAABB();
assert(aabb); assert(aabb);

View File

@ -63,8 +63,8 @@ class SAPAlgorithm : public BroadPhaseAlgorithm {
virtual ~SAPAlgorithm(); // Destructor virtual ~SAPAlgorithm(); // Destructor
virtual void computePossibleCollisionPairs(); // Compute the possible collision pairs of bodies virtual void computePossibleCollisionPairs(); // Compute the possible collision pairs of bodies
virtual void notifyAddedBodies(std::vector<Body*> bodies); // Notify the broad-phase algorithm about new bodies in the physics world virtual void notifyAddedBodies(std::vector<RigidBody*> bodies); // Notify the broad-phase algorithm about new bodies in the physics world
virtual void notifyRemovedBodies(std::vector<Body*> bodies); // Notify the broad-phase algorithm about removed bodies in the physics world virtual void notifyRemovedBodies(std::vector<RigidBody*> 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 // Static method that compare two AABBs. This method will be used to compare to AABBs

View File

@ -100,9 +100,9 @@ void PhysicsEngine::updateAllBodiesMotion() {
Vector3 newAngularVelocity; Vector3 newAngularVelocity;
// For each body of thephysics world // For each body of thephysics world
for (vector<Body*>::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); ++it) { for (vector<RigidBody*>::iterator it=world->getRigidBodiesBeginIterator(); it != world->getRigidBodiesEndIterator(); ++it) {
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it); RigidBody* rigidBody = *it;
assert(rigidBody); assert(rigidBody);
// If the body is able to move // If the body is able to move
@ -138,10 +138,9 @@ void PhysicsEngine::updateAllBodiesMotion() {
// Update the position and orientation of a body // Update the position and orientation of a body
// Use the Semi-Implicit Euler (Sympletic Euler) method to compute the new position and the new // Use the Semi-Implicit Euler (Sympletic Euler) method to compute the new position and the new
// orientation of the body // 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(); double dt = timer.getTimeStep();
RigidBody* rigidBody = dynamic_cast<RigidBody*>(body);
assert(rigidBody); assert(rigidBody);
// Update the old position and orientation of the body // Update the old position and orientation of the body
@ -168,7 +167,7 @@ void PhysicsEngine::setInterpolationFactorToAllBodies() {
assert(factor >= 0.0 && factor <= 1.0); assert(factor >= 0.0 && factor <= 1.0);
// Set the factor to all bodies // Set the factor to all bodies
for (vector<Body*>::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); ++it) { for (vector<RigidBody*>::iterator it=world->getRigidBodiesBeginIterator(); it != world->getRigidBodiesEndIterator(); ++it) {
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it); RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it);
assert(rigidBody); assert(rigidBody);
@ -181,7 +180,7 @@ void PhysicsEngine::setInterpolationFactorToAllBodies() {
void PhysicsEngine::applyGravity() { void PhysicsEngine::applyGravity() {
// For each body of the physics world // For each body of the physics world
for (vector<Body*>::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); ++it) { for (vector<RigidBody*>::iterator it=world->getRigidBodiesBeginIterator(); it != world->getRigidBodiesEndIterator(); ++it) {
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it); RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it);
assert(rigidBody); assert(rigidBody);

View File

@ -50,7 +50,7 @@ class PhysicsEngine {
ConstraintSolver constraintSolver; // Constraint solver ConstraintSolver constraintSolver; // Constraint solver
void updateAllBodiesMotion(); // Compute the motion of all bodies and update their positions and orientations 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 setInterpolationFactorToAllBodies(); // Compute and set the interpolation factor to all bodies
void applyGravity(); // Apply the gravity force to all bodies void applyGravity(); // Apply the gravity force to all bodies

View File

@ -52,13 +52,13 @@ RigidBody* PhysicsWorld::createRigidBody(const Transform& transform, double mass
currentBodyID++; currentBodyID++;
// Add the rigid body to the physics world and return it // Add the rigid body to the physics world and return it
addBody(rigidBody); addRigidBody(rigidBody);
return rigidBody; return rigidBody;
} }
// Destroy a rigid body // Destroy a rigid body
void PhysicsWorld::destroyRigidBody(RigidBody* rigidBody) { void PhysicsWorld::destroyRigidBody(RigidBody* rigidBody) {
removeBody(rigidBody); removeRigidBody(rigidBody);
delete rigidBody; delete rigidBody;
} }

View File

@ -47,16 +47,16 @@ namespace reactphysics3d {
*/ */
class PhysicsWorld { class PhysicsWorld {
protected : protected :
std::vector<Body*> bodies; // All the rigid bodies of the physics world std::vector<RigidBody*> rigidBodies; // All the rigid bodies of the physics world
std::vector<Body*> addedBodies; // Added bodies since last update std::vector<RigidBody*> addedBodies; // Added bodies since last update
std::vector<Body*> removedBodies; // Removed bodies since last update std::vector<RigidBody*> removedBodies; // Removed bodies since last update
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
long unsigned int currentBodyID; // Current body ID long unsigned int currentBodyID; // Current body ID
void addBody(Body* body); // Add a body to the physics world void addRigidBody(RigidBody* body); // Add a body to the physics world
void removeBody(Body* body); // Remove a body from the physics world void removeRigidBody(RigidBody* body); // Remove a body from the physics world
public : public :
PhysicsWorld(const Vector3& gravity); // Constructor PhysicsWorld(const Vector3& gravity); // Constructor
@ -75,23 +75,23 @@ class PhysicsWorld {
void removeAllConstraints(); // Remove all constraints and delete them (free their memory) void removeAllConstraints(); // Remove all constraints and delete them (free their memory)
std::vector<Constraint*>::iterator getConstraintsBeginIterator(); // Return a start iterator on the constraint list std::vector<Constraint*>::iterator getConstraintsBeginIterator(); // Return a start iterator on the constraint list
std::vector<Constraint*>::iterator getConstraintsEndIterator(); // Return a end iterator on the constraint list std::vector<Constraint*>::iterator getConstraintsEndIterator(); // Return a end iterator on the constraint list
std::vector<Body*>::iterator getBodiesBeginIterator(); // Return an iterator to the beginning of the bodies of the physics world std::vector<RigidBody*>::iterator getRigidBodiesBeginIterator(); // Return an iterator to the beginning of the bodies of the physics world
std::vector<Body*>::iterator getBodiesEndIterator(); // Return an iterator to the end of the bodies of the physics world std::vector<RigidBody*>::iterator getRigidBodiesEndIterator(); // Return an iterator to the end of the bodies of the physics world
std::vector<Body*>& getAddedBodies(); // Return the added bodies since last update of the physics engine std::vector<RigidBody*>& getAddedRigidBodies(); // Return the added bodies since last update of the physics engine
std::vector<Body*>& getRemovedBodies(); // Retrun the removed bodies since last update of the physics engine std::vector<RigidBody*>& getRemovedRigidBodies(); // Retrun the removed bodies since last update of the physics engine
}; };
// Add a body to the physics world // Add a body to the physics world
inline void PhysicsWorld::addBody(Body* body) { inline void PhysicsWorld::addRigidBody(RigidBody* body) {
std::vector<Body*>::iterator it; std::vector<RigidBody*>::iterator it;
assert(body); assert(body);
it = std::find(bodies.begin(), bodies.end(), body); it = std::find(rigidBodies.begin(), rigidBodies.end(), body);
assert(it == bodies.end()); assert(it == rigidBodies.end());
// The body isn't already in the bodyList, therefore we add it to the list // 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); addedBodies.push_back(body);
it = std::find(removedBodies.begin(), removedBodies.end(), body); it = std::find(removedBodies.begin(), removedBodies.end(), body);
if (it != removedBodies.end()) { if (it != removedBodies.end()) {
@ -100,13 +100,13 @@ inline void PhysicsWorld::addBody(Body* body) {
} }
// Remove a body from the physics world // Remove a body from the physics world
inline void PhysicsWorld::removeBody(Body* body) { inline void PhysicsWorld::removeRigidBody(RigidBody* body) {
std::vector<Body*>::iterator it; std::vector<RigidBody*>::iterator it;
assert(body); assert(body);
it = std::find(bodies.begin(), bodies.end(), body); it = std::find(rigidBodies.begin(), rigidBodies.end(), body);
assert(*it == body); assert(*it == body);
bodies.erase(it); rigidBodies.erase(it);
it = std::find(addedBodies.begin(), addedBodies.end(), body); it = std::find(addedBodies.begin(), addedBodies.end(), body);
if (it != addedBodies.end()) { if (it != addedBodies.end()) {
@ -165,22 +165,22 @@ inline std::vector<Constraint*>::iterator PhysicsWorld::getConstraintsEndIterato
} }
// Return an iterator to the beginning of the bodies of the physics world // Return an iterator to the beginning of the bodies of the physics world
inline std::vector<Body*>::iterator PhysicsWorld::getBodiesBeginIterator() { inline std::vector<RigidBody*>::iterator PhysicsWorld::getRigidBodiesBeginIterator() {
return bodies.begin(); return rigidBodies.begin();
} }
// Return an iterator to the end of the bodies of the physics world // Return an iterator to the end of the bodies of the physics world
inline std::vector<Body*>::iterator PhysicsWorld::getBodiesEndIterator() { inline std::vector<RigidBody*>::iterator PhysicsWorld::getRigidBodiesEndIterator() {
return bodies.end(); return rigidBodies.end();
} }
// Return the added bodies since last update of the physics engine // Return the added bodies since last update of the physics engine
inline std::vector<Body*>& PhysicsWorld::getAddedBodies() { inline std::vector<RigidBody*>& PhysicsWorld::getAddedRigidBodies() {
return addedBodies; return addedBodies;
} }
// Retrun the removed bodies since last update of the physics engine // Retrun the removed bodies since last update of the physics engine
inline std::vector<Body*>& PhysicsWorld::getRemovedBodies() { inline std::vector<RigidBody*>& PhysicsWorld::getRemovedRigidBodies() {
return removedBodies; return removedBodies;
} }