From 3acdeb8cd2814b3c6f0a1b072b70419fde1e8cd1 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Mon, 12 Oct 2015 18:37:18 +0200 Subject: [PATCH] Remove dynamic memory allocation of ContactPointInfo instances during narrow-phase --- src/collision/CollisionDetection.cpp | 17 +++++----------- src/collision/CollisionDetection.h | 4 ++-- .../narrowphase/EPA/EPAAlgorithm.cpp | 5 ++--- .../narrowphase/GJK/GJKAlgorithm.cpp | 20 ++++++++----------- .../narrowphase/NarrowPhaseAlgorithm.h | 3 ++- .../narrowphase/SphereVsSphereAlgorithm.cpp | 7 +++---- 6 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/collision/CollisionDetection.cpp b/src/collision/CollisionDetection.cpp index d1e97942..cdee7927 100644 --- a/src/collision/CollisionDetection.cpp +++ b/src/collision/CollisionDetection.cpp @@ -413,37 +413,30 @@ void CollisionDetection::removeProxyCollisionShape(ProxyShape* proxyShape) { } // Called by a narrow-phase collision algorithm when a new contact has been found -void CollisionDetection::notifyContact(OverlappingPair* overlappingPair, ContactPointInfo* contactInfo) { +void CollisionDetection::notifyContact(OverlappingPair* overlappingPair, const ContactPointInfo& contactInfo) { assert(contactInfo != NULL); // If it is the first contact since the pairs are overlapping if (overlappingPair->getNbContactPoints() == 0) { // Trigger a callback event - if (mWorld->mEventListener != NULL) mWorld->mEventListener->beginContact(*contactInfo); + if (mWorld->mEventListener != NULL) mWorld->mEventListener->beginContact(contactInfo); } - // TODO : Try not to allocate ContactPointInfo dynamically - // Create a new contact createContact(overlappingPair, contactInfo); // Trigger a callback event for the new contact - if (mWorld->mEventListener != NULL) mWorld->mEventListener->newContact(*contactInfo); - - // Delete and remove the contact info from the memory allocator - contactInfo->~ContactPointInfo(); - mWorld->mMemoryAllocator.release(contactInfo, sizeof(ContactPointInfo)); + if (mWorld->mEventListener != NULL) mWorld->mEventListener->newContact(contactInfo); } // Create a new contact void CollisionDetection::createContact(OverlappingPair* overlappingPair, - const ContactPointInfo* contactInfo) { + const ContactPointInfo& contactInfo) { // Create a new contact ContactPoint* contact = new (mWorld->mMemoryAllocator.allocate(sizeof(ContactPoint))) - ContactPoint(*contactInfo); - assert(contact != NULL); + ContactPoint(contactInfo); // Add the contact to the contact manifold set of the corresponding overlapping pair overlappingPair->addContact(contact); diff --git a/src/collision/CollisionDetection.h b/src/collision/CollisionDetection.h index 398460cc..3140a5f8 100644 --- a/src/collision/CollisionDetection.h +++ b/src/collision/CollisionDetection.h @@ -204,10 +204,10 @@ class CollisionDetection : public NarrowPhaseCallback { MemoryAllocator& getWorldMemoryAllocator(); /// Called by a narrow-phase collision algorithm when a new contact has been found - virtual void notifyContact(OverlappingPair* overlappingPair, ContactPointInfo* contactInfo); + virtual void notifyContact(OverlappingPair* overlappingPair, const ContactPointInfo& contactInfo); /// Create a new contact - void createContact(OverlappingPair* overlappingPair, const ContactPointInfo* contactInfo); + void createContact(OverlappingPair* overlappingPair, const ContactPointInfo& contactInfo); // -------------------- Friendship -------------------- // diff --git a/src/collision/narrowphase/EPA/EPAAlgorithm.cpp b/src/collision/narrowphase/EPA/EPAAlgorithm.cpp index 02cc148a..213169d8 100644 --- a/src/collision/narrowphase/EPA/EPAAlgorithm.cpp +++ b/src/collision/narrowphase/EPA/EPAAlgorithm.cpp @@ -427,9 +427,8 @@ bool EPAAlgorithm::computePenetrationDepthAndContactPoints(const Simplex& simple assert(penetrationDepth > 0.0); // Create the contact info object - ContactPointInfo* contactInfo = new (mMemoryAllocator->allocate(sizeof(ContactPointInfo))) - ContactPointInfo(shape1Info.proxyShape, shape2Info.proxyShape, normal, - penetrationDepth, pALocal, pBLocal); + ContactPointInfo contactInfo(shape1Info.proxyShape, shape2Info.proxyShape, + normal, penetrationDepth, pALocal, pBLocal); narrowPhaseCallback->notifyContact(shape1Info.overlappingPair, contactInfo); diff --git a/src/collision/narrowphase/GJK/GJKAlgorithm.cpp b/src/collision/narrowphase/GJK/GJKAlgorithm.cpp index 7554d6d1..caa574a4 100644 --- a/src/collision/narrowphase/GJK/GJKAlgorithm.cpp +++ b/src/collision/narrowphase/GJK/GJKAlgorithm.cpp @@ -147,9 +147,8 @@ bool GJKAlgorithm::testCollision(const CollisionShapeInfo& shape1Info, if (penetrationDepth <= 0.0) return false; // Create the contact info object - ContactPointInfo* contactInfo = new (mMemoryAllocator->allocate(sizeof(ContactPointInfo))) - ContactPointInfo(shape1Info.proxyShape, shape2Info.proxyShape, normal, - penetrationDepth, pA, pB); + ContactPointInfo contactInfo(shape1Info.proxyShape, shape2Info.proxyShape, + normal, penetrationDepth, pA, pB); narrowPhaseCallback->notifyContact(shape1Info.overlappingPair, contactInfo); @@ -181,9 +180,8 @@ bool GJKAlgorithm::testCollision(const CollisionShapeInfo& shape1Info, if (penetrationDepth <= 0.0) return false; // Create the contact info object - ContactPointInfo* contactInfo = new (mMemoryAllocator->allocate(sizeof(ContactPointInfo))) - ContactPointInfo(shape1Info.proxyShape, shape2Info.proxyShape, normal, - penetrationDepth, pA, pB); + ContactPointInfo contactInfo(shape1Info.proxyShape, shape2Info.proxyShape, + normal, penetrationDepth, pA, pB); narrowPhaseCallback->notifyContact(shape1Info.overlappingPair, contactInfo); @@ -213,9 +211,8 @@ bool GJKAlgorithm::testCollision(const CollisionShapeInfo& shape1Info, if (penetrationDepth <= 0.0) return false; // Create the contact info object - ContactPointInfo* contactInfo = new (mMemoryAllocator->allocate(sizeof(ContactPointInfo))) - ContactPointInfo(shape1Info.proxyShape, shape2Info.proxyShape, normal, - penetrationDepth, pA, pB); + ContactPointInfo contactInfo(shape1Info.proxyShape, shape2Info.proxyShape, + normal, penetrationDepth, pA, pB); narrowPhaseCallback->notifyContact(shape1Info.overlappingPair, contactInfo); @@ -252,9 +249,8 @@ bool GJKAlgorithm::testCollision(const CollisionShapeInfo& shape1Info, if (penetrationDepth <= 0.0) return false; // Create the contact info object - ContactPointInfo* contactInfo = new (mMemoryAllocator->allocate(sizeof(ContactPointInfo))) - ContactPointInfo(shape1Info.proxyShape, shape2Info.proxyShape, normal, - penetrationDepth, pA, pB); + ContactPointInfo contactInfo(shape1Info.proxyShape, shape2Info.proxyShape, + normal, penetrationDepth, pA, pB); narrowPhaseCallback->notifyContact(shape1Info.overlappingPair, contactInfo); diff --git a/src/collision/narrowphase/NarrowPhaseAlgorithm.h b/src/collision/narrowphase/NarrowPhaseAlgorithm.h index 725ba6c5..41ecc00c 100644 --- a/src/collision/narrowphase/NarrowPhaseAlgorithm.h +++ b/src/collision/narrowphase/NarrowPhaseAlgorithm.h @@ -48,7 +48,8 @@ class NarrowPhaseCallback { public: /// Called by a narrow-phase collision algorithm when a new contact has been found - virtual void notifyContact(OverlappingPair* overlappingPair, ContactPointInfo* contactInfo)=0; + virtual void notifyContact(OverlappingPair* overlappingPair, + const ContactPointInfo& contactInfo)=0; }; diff --git a/src/collision/narrowphase/SphereVsSphereAlgorithm.cpp b/src/collision/narrowphase/SphereVsSphereAlgorithm.cpp index 72655632..c58048b0 100644 --- a/src/collision/narrowphase/SphereVsSphereAlgorithm.cpp +++ b/src/collision/narrowphase/SphereVsSphereAlgorithm.cpp @@ -70,10 +70,9 @@ bool SphereVsSphereAlgorithm::testCollision(const CollisionShapeInfo& shape1Info decimal penetrationDepth = sumRadius - std::sqrt(squaredDistanceBetweenCenters); // Create the contact info object - ContactPointInfo* contactInfo = new (mMemoryAllocator->allocate(sizeof(ContactPointInfo))) - ContactPointInfo(shape1Info.proxyShape, shape2Info.proxyShape, - vectorBetweenCenters.getUnit(), penetrationDepth, - intersectionOnBody1, intersectionOnBody2); + ContactPointInfo contactInfo(shape1Info.proxyShape, shape2Info.proxyShape, + vectorBetweenCenters.getUnit(), penetrationDepth, + intersectionOnBody1, intersectionOnBody2); // Notify about the new contact narrowPhaseCallback->notifyContact(shape1Info.overlappingPair, contactInfo);