From 5f7af615931e239f5734eb0bdcb7f78fb3b854bb Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Fri, 21 Nov 2014 21:27:09 +0100 Subject: [PATCH] -Remove unnecessary contact manifold -Delete the BroadPhasePair class --- CMakeLists.txt | 2 - examples/collisionshapes/Scene.cpp | 38 +-------- examples/collisionshapes/Scene.h | 5 +- examples/cubes/Scene.cpp | 6 +- examples/cubes/Scene.h | 2 +- src/body/CollisionBody.cpp | 22 +++++- src/body/CollisionBody.h | 3 + src/collision/BroadPhasePair.cpp | 40 ---------- src/collision/BroadPhasePair.h | 111 --------------------------- src/collision/CollisionDetection.cpp | 3 - src/collision/CollisionDetection.h | 4 - src/engine/CollisionWorld.h | 3 - src/engine/ContactManifold.h | 1 + src/engine/DynamicsWorld.cpp | 22 ++---- src/engine/DynamicsWorld.h | 18 ----- 15 files changed, 37 insertions(+), 243 deletions(-) delete mode 100644 src/collision/BroadPhasePair.cpp delete mode 100644 src/collision/BroadPhasePair.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c25e0c6..10ddb5cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,8 +79,6 @@ SET (REACTPHYSICS3D_SOURCES "src/collision/shapes/CylinderShape.cpp" "src/collision/shapes/SphereShape.h" "src/collision/shapes/SphereShape.cpp" - "src/collision/BroadPhasePair.h" - "src/collision/BroadPhasePair.cpp" "src/collision/RaycastInfo.h" "src/collision/RaycastInfo.cpp" "src/collision/ProxyShape.h" diff --git a/examples/collisionshapes/Scene.cpp b/examples/collisionshapes/Scene.cpp index c6fc330e..b789c1c9 100644 --- a/examples/collisionshapes/Scene.cpp +++ b/examples/collisionshapes/Scene.cpp @@ -102,7 +102,7 @@ Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath, const std::str } // Create all the spheres of the scene - for (int i=0; i::iterator it = mContactPoints.begin(); - it != mContactPoints.end(); ++it) { - delete (*it); - } - - // Destroy the static data for the visual contact points - VisualContactPoint::destroyStaticData(); - // Destroy the rigid body of the floor mDynamicsWorld->destroyRigidBody(mFloor->getRigidBody()); @@ -380,27 +371,6 @@ void Scene::simulate() { (*it)->updateTransform(); } - // Destroy all the visual contact points - for (std::vector::iterator it = mContactPoints.begin(); - it != mContactPoints.end(); ++it) { - delete (*it); - } - mContactPoints.clear(); - - // Generate the new visual contact points - const std::vector& manifolds = mDynamicsWorld->getContactManifolds(); - for (std::vector::const_iterator it = manifolds.begin(); - it != manifolds.end(); ++it) { - for (unsigned int i=0; i<(*it)->getNbContactPoints(); i++) { - rp3d::ContactPoint* point = (*it)->getContactPoint(i); - - const rp3d::Vector3 pos = point->getWorldPointOnBody1(); - openglframework::Vector3 position(pos.x, pos.y, pos.z); - VisualContactPoint* visualPoint = new VisualContactPoint(position); - mContactPoints.push_back(visualPoint); - } - } - mFloor->updateTransform(); } } @@ -466,12 +436,6 @@ void Scene::render() { (*it)->render(mPhongShader, worldToCameraMatrix); } - // Render all the visual contact points - for (std::vector::iterator it = mContactPoints.begin(); - it != mContactPoints.end(); ++it) { - (*it)->render(mPhongShader, worldToCameraMatrix); - } - // Render the floor mFloor->render(mPhongShader, worldToCameraMatrix); diff --git a/examples/collisionshapes/Scene.h b/examples/collisionshapes/Scene.h index 0fe055bf..f06bb72b 100644 --- a/examples/collisionshapes/Scene.h +++ b/examples/collisionshapes/Scene.h @@ -41,7 +41,7 @@ // Constants const int NB_BOXES = 2; -const int NB_SPHERES = 1; +const int NB_CUBES = 1; const int NB_CONES = 3; const int NB_CYLINDERS = 2; const int NB_CAPSULES = 1; @@ -97,9 +97,6 @@ class Scene { /// All the dumbbell of the scene std::vector mDumbbells; - /// All the visual contact points - std::vector mContactPoints; - /// Box for the floor Box* mFloor; diff --git a/examples/cubes/Scene.cpp b/examples/cubes/Scene.cpp index a794e713..a0b5a59e 100644 --- a/examples/cubes/Scene.cpp +++ b/examples/cubes/Scene.cpp @@ -60,7 +60,7 @@ Scene::Scene(Viewer* viewer, const std::string& shaderFolderPath) float radius = 2.0f; // Create all the cubes of the scene - for (int i=0; iupdate(); diff --git a/examples/cubes/Scene.h b/examples/cubes/Scene.h index dc60a127..efd2a232 100644 --- a/examples/cubes/Scene.h +++ b/examples/cubes/Scene.h @@ -33,7 +33,7 @@ #include "Viewer.h" // Constants -const int NB_SPHERES = 200; // Number of boxes in the scene +const int NB_CUBES = 200; // Number of boxes in the scene const openglframework::Vector3 BOX_SIZE(2, 2, 2); // Box dimensions in meters const openglframework::Vector3 FLOOR_SIZE(50, 0.5f, 50); // Floor dimensions in meters const float BOX_MASS = 1.0f; // Box mass in kilograms diff --git a/src/body/CollisionBody.cpp b/src/body/CollisionBody.cpp index e0107f28..05c03e4b 100644 --- a/src/body/CollisionBody.cpp +++ b/src/body/CollisionBody.cpp @@ -246,10 +246,30 @@ void CollisionBody::askForBroadPhaseCollisionCheck() const { // For all the proxy collision shapes of the body for (ProxyShape* shape = mProxyCollisionShapes; shape != NULL; shape = shape->mNext) { - mWorld.mCollisionDetection.askForBroadPhaseCollisionCheck(shape); + mWorld.mCollisionDetection.askForBroadPhaseCollisionCheck(shape); } } +// Reset the mIsAlreadyInIsland variable of the body and contact manifolds. +/// This method also returns the number of contact manifolds of the body. +int CollisionBody::resetIsAlreadyInIslandAndCountManifolds() { + + mIsAlreadyInIsland = false; + + int nbManifolds = 0; + + // Reset the mIsAlreadyInIsland variable of the contact manifolds for + // this body + ContactManifoldListElement* currentElement = mContactManifoldsList; + while (currentElement != NULL) { + currentElement->contactManifold->mIsAlreadyInIsland = false; + currentElement = currentElement->next; + nbManifolds++; + } + + return nbManifolds; +} + // Return true if a point is inside the collision body bool CollisionBody::testPointInside(const Vector3& worldPoint) const { diff --git a/src/body/CollisionBody.h b/src/body/CollisionBody.h index 7678ed59..39baf710 100644 --- a/src/body/CollisionBody.h +++ b/src/body/CollisionBody.h @@ -118,6 +118,9 @@ class CollisionBody : public Body { /// (as if the body has moved). void askForBroadPhaseCollisionCheck() const; + /// Reset the mIsAlreadyInIsland variable of the body and contact manifolds + int resetIsAlreadyInIslandAndCountManifolds(); + public : // -------------------- Methods -------------------- // diff --git a/src/collision/BroadPhasePair.cpp b/src/collision/BroadPhasePair.cpp deleted file mode 100644 index c54d8c52..00000000 --- a/src/collision/BroadPhasePair.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/******************************************************************************** -* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * -* Copyright (c) 2010-2013 Daniel Chappuis * -********************************************************************************* -* * -* This software is provided 'as-is', without any express or implied warranty. * -* In no event will the authors be held liable for any damages arising from the * -* use of this software. * -* * -* Permission is granted to anyone to use this software for any purpose, * -* including commercial applications, and to alter it and redistribute it * -* freely, subject to the following restrictions: * -* * -* 1. The origin of this software must not be misrepresented; you must not claim * -* that you wrote the original software. If you use this software in a * -* product, an acknowledgment in the product documentation would be * -* appreciated but is not required. * -* * -* 2. Altered source versions must be plainly marked as such, and must not be * -* misrepresented as being the original software. * -* * -* 3. This notice may not be removed or altered from any source distribution. * -* * -********************************************************************************/ - -// Libraries -#include "BroadPhasePair.h" - -using namespace reactphysics3d; - - // Constructor -BroadPhasePair::BroadPhasePair(CollisionBody* body1, CollisionBody* body2) - : body1(body1), body2(body2), previousSeparatingAxis(Vector3(1,1,1)) { - -} - -// Destructor -BroadPhasePair::~BroadPhasePair() { - -} diff --git a/src/collision/BroadPhasePair.h b/src/collision/BroadPhasePair.h deleted file mode 100644 index a6edeaf8..00000000 --- a/src/collision/BroadPhasePair.h +++ /dev/null @@ -1,111 +0,0 @@ -/******************************************************************************** -* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * -* Copyright (c) 2010-2013 Daniel Chappuis * -********************************************************************************* -* * -* This software is provided 'as-is', without any express or implied warranty. * -* In no event will the authors be held liable for any damages arising from the * -* use of this software. * -* * -* Permission is granted to anyone to use this software for any purpose, * -* including commercial applications, and to alter it and redistribute it * -* freely, subject to the following restrictions: * -* * -* 1. The origin of this software must not be misrepresented; you must not claim * -* that you wrote the original software. If you use this software in a * -* product, an acknowledgment in the product documentation would be * -* appreciated but is not required. * -* * -* 2. Altered source versions must be plainly marked as such, and must not be * -* misrepresented as being the original software. * -* * -* 3. This notice may not be removed or altered from any source distribution. * -* * -********************************************************************************/ - -#ifndef REACTPHYSICS3D_BROAD_PHASE_PAIR_H -#define REACTPHYSICS3D_BROAD_PHASE_PAIR_H - -// Libraries -#include "body/CollisionBody.h" - -/// ReactPhysics3D namespace -namespace reactphysics3d { - -// TODO : DELETE THIS CLASS -// Structure BroadPhasePair -/** - * This structure represents a pair of bodies - * during the broad-phase collision detection. - */ -struct BroadPhasePair { - - public: - - // -------------------- Attributes -------------------- // - - /// Pointer to the first body - CollisionBody* body1; - - /// Pointer to the second body - CollisionBody* body2; - - /// Previous cached separating axis - Vector3 previousSeparatingAxis; - - // -------------------- Methods -------------------- // - - /// Constructor - BroadPhasePair(CollisionBody* body1, CollisionBody* body2); - - /// Destructor - ~BroadPhasePair(); - - /// Return the pair of bodies index - static bodyindexpair computeBodiesIndexPair(CollisionBody* body1, CollisionBody* body2); - - /// Return the pair of bodies index - bodyindexpair getBodiesIndexPair() const; - - /// Smaller than operator - bool operator<(const BroadPhasePair& broadPhasePair2) const; - - /// Larger than operator - bool operator>(const BroadPhasePair& broadPhasePair2) const; - - /// Equal operator - bool operator==(const BroadPhasePair& broadPhasePair2) const; - - /// Not equal operator - bool operator!=(const BroadPhasePair& broadPhasePair2) const; -}; - -// Return the pair of bodies index -inline bodyindexpair BroadPhasePair::getBodiesIndexPair() const { - - return computeBodiesIndexPair(body1, body2); -} - -// Smaller than operator -inline bool BroadPhasePair::operator<(const BroadPhasePair& broadPhasePair2) const { - return (body1 < broadPhasePair2.body1 ? true : (body2 < broadPhasePair2.body2)); -} - -// Larger than operator -inline bool BroadPhasePair::operator>(const BroadPhasePair& broadPhasePair2) const { - return (body1 > broadPhasePair2.body1 ? true : (body2 > broadPhasePair2.body2)); -} - -// Equal operator -inline bool BroadPhasePair::operator==(const BroadPhasePair& broadPhasePair2) const { - return (body1 == broadPhasePair2.body1 && body2 == broadPhasePair2.body2); -} - -// Not equal operator -inline bool BroadPhasePair::operator!=(const BroadPhasePair& broadPhasePair2) const { - return (body1 != broadPhasePair2.body1 || body2 != broadPhasePair2.body2); -} - -} - -#endif diff --git a/src/collision/CollisionDetection.cpp b/src/collision/CollisionDetection.cpp index 261c27aa..cbb652b5 100644 --- a/src/collision/CollisionDetection.cpp +++ b/src/collision/CollisionDetection.cpp @@ -220,9 +220,6 @@ void CollisionDetection::createContact(OverlappingPair* overlappingPair, // Add the contact to the contact cache of the corresponding overlapping pair overlappingPair->addContact(contact); - // Add the contact manifold to the list of contact manifolds - mContactManifolds.push_back(overlappingPair->getContactManifold()); - // Add the contact manifold into the list of contact manifolds // of the two bodies involved in the contact addContactManifoldToBody(overlappingPair->getContactManifold(), diff --git a/src/collision/CollisionDetection.h b/src/collision/CollisionDetection.h index f41655ef..8d619621 100644 --- a/src/collision/CollisionDetection.h +++ b/src/collision/CollisionDetection.h @@ -80,10 +80,6 @@ class CollisionDetection { /// True if some collision shapes have been added previously bool mIsCollisionShapesAdded; - /// All the contact constraints - // TODO : Remove this variable (we will use the ones in the island now) - std::vector mContactManifolds; - // -------------------- Methods -------------------- // /// Private copy-constructor diff --git a/src/engine/CollisionWorld.h b/src/engine/CollisionWorld.h index 17ae3862..8a33da30 100644 --- a/src/engine/CollisionWorld.h +++ b/src/engine/CollisionWorld.h @@ -66,9 +66,6 @@ class CollisionWorld { /// All the collision shapes of the world std::list mCollisionShapes; - /// Broad-phase overlapping pairs of bodies - std::map mOverlappingPairs; - /// Current body ID bodyindex mCurrentBodyID; diff --git a/src/engine/ContactManifold.h b/src/engine/ContactManifold.h index 922f0943..7d6c5d19 100644 --- a/src/engine/ContactManifold.h +++ b/src/engine/ContactManifold.h @@ -210,6 +210,7 @@ class ContactManifold { friend class DynamicsWorld; friend class Island; + friend class CollisionBody; }; // Return a pointer to the first body of the contact manifold diff --git a/src/engine/DynamicsWorld.cpp b/src/engine/DynamicsWorld.cpp index 284197e5..e6d0ae16 100644 --- a/src/engine/DynamicsWorld.cpp +++ b/src/engine/DynamicsWorld.cpp @@ -57,14 +57,6 @@ DynamicsWorld::DynamicsWorld(const Vector3 &gravity, decimal timeStep = DEFAULT_ // Destructor DynamicsWorld::~DynamicsWorld() { - // Delete the remaining overlapping pairs - map, OverlappingPair*>::iterator it; - for (it = mOverlappingPairs.begin(); it != mOverlappingPairs.end(); it++) { - // Delete the overlapping pair - (*it).second->OverlappingPair::~OverlappingPair(); - mMemoryAllocator.release((*it).second, sizeof(OverlappingPair)); - } - // Release the memory allocated for the islands for (uint i=0; i::iterator it = mRigidBodies.begin(); it != mRigidBodies.end(); ++it) { - (*it)->mIsAlreadyInIsland = false; - } - for (std::vector::iterator it = mCollisionDetection.mContactManifolds.begin(); - it != mCollisionDetection.mContactManifolds.end(); ++it) { - (*it)->mIsAlreadyInIsland = false; + int nbBodyManifolds = (*it)->resetIsAlreadyInIslandAndCountManifolds(); + nbContactManifolds += nbBodyManifolds; } for (std::set::iterator it = mJoints.begin(); it != mJoints.end(); ++it) { (*it)->mIsAlreadyInIsland = false; @@ -715,7 +703,7 @@ void DynamicsWorld::computeIslands() { // Create the new island void* allocatedMemoryIsland = mMemoryAllocator.allocate(sizeof(Island)); mIslands[mNbIslands] = new (allocatedMemoryIsland) Island(nbBodies, - mCollisionDetection.mContactManifolds.size(), + nbContactManifolds, mJoints.size(), mMemoryAllocator); // While there are still some bodies to visit in the stack diff --git a/src/engine/DynamicsWorld.h b/src/engine/DynamicsWorld.h index 58f2fc03..7fd53cd6 100644 --- a/src/engine/DynamicsWorld.h +++ b/src/engine/DynamicsWorld.h @@ -242,9 +242,6 @@ class DynamicsWorld : public CollisionWorld { /// Return the number of joints in the world uint getNbJoints() const; - /// Return the number of contact manifolds in the world - uint getNbContactManifolds() const; - /// Return the current physics time (in seconds) long double getPhysicsTime() const; @@ -254,9 +251,6 @@ class DynamicsWorld : public CollisionWorld { /// Return an iterator to the end of the rigid bodies of the physics world std::set::iterator getRigidBodiesEndIterator(); - /// Return a reference to the contact manifolds of the world - const std::vector& getContactManifolds() const; - /// Return true if the sleeping technique is enabled bool isSleepingEnabled() const; @@ -382,18 +376,6 @@ inline std::set::iterator DynamicsWorld::getRigidBodiesEndIterator() return mRigidBodies.end(); } -// Return a reference to the contact manifolds of the world -// TODO : DELETE THIS METHOD AND USE EVENT LISTENER IN EXAMPLES INSTEAD -inline const std::vector& DynamicsWorld::getContactManifolds() const { - return mCollisionDetection.mContactManifolds; -} - -// Return the number of contact manifolds in the world -// TODO : DELETE THIS METHOD AND USE EVENT LISTENER IN EXAMPLES INSTEAD -inline uint DynamicsWorld::getNbContactManifolds() const { - return mCollisionDetection.mContactManifolds.size(); -} - // Return the current physics time (in seconds) inline long double DynamicsWorld::getPhysicsTime() const { return mTimer.getPhysicsTime();