Replace linked-list by rp3d::List for contactPoints of NarrowPhaseInfo

This commit is contained in:
Daniel Chappuis 2018-09-18 07:35:11 +02:00
parent ea523e47d3
commit cf3d76ce45
5 changed files with 19 additions and 24 deletions

View File

@ -242,7 +242,7 @@ void CollisionDetection::computeNarrowPhase() {
NarrowPhaseInfo* narrowPhaseInfo = mNarrowPhaseInfos[i]; NarrowPhaseInfo* narrowPhaseInfo = mNarrowPhaseInfos[i];
assert(narrowPhaseInfo->contactPoints == nullptr); assert(narrowPhaseInfo->contactPoints.size() == 0);
// Select the narrow phase algorithm to use according to the two collision shapes // Select the narrow phase algorithm to use according to the two collision shapes
const CollisionShapeType shape1Type = narrowPhaseInfo->collisionShape1->getType(); const CollisionShapeType shape1Type = narrowPhaseInfo->collisionShape1->getType();
@ -434,7 +434,7 @@ void CollisionDetection::processAllPotentialContacts(const List<NarrowPhaseInfo*
NarrowPhaseInfo* narrowPhaseInfo = collidingNarrowPhaseInfos[i]; NarrowPhaseInfo* narrowPhaseInfo = collidingNarrowPhaseInfos[i];
assert(narrowPhaseInfo != nullptr); assert(narrowPhaseInfo != nullptr);
assert(narrowPhaseInfo->contactPoints != nullptr); assert(narrowPhaseInfo->contactPoints.size() > 0);
// Transfer the contact points from the narrow phase info to the overlapping pair // Transfer the contact points from the narrow phase info to the overlapping pair
narrowPhaseInfo->overlappingPair->addPotentialContactPoints(narrowPhaseInfo); narrowPhaseInfo->overlappingPair->addPotentialContactPoints(narrowPhaseInfo);

View File

@ -51,11 +51,12 @@ ContactManifoldSet::~ContactManifoldSet() {
void ContactManifoldSet::addContactPoints(NarrowPhaseInfo* narrowPhaseInfo) { void ContactManifoldSet::addContactPoints(NarrowPhaseInfo* narrowPhaseInfo) {
assert(narrowPhaseInfo->contactPoints != nullptr); assert(narrowPhaseInfo->contactPoints.size() > 0);
// For each potential contact point to add // For each potential contact point to add
ContactPointInfo* contactPoint = narrowPhaseInfo->contactPoints; for (uint i=0; i < narrowPhaseInfo->contactPoints.size(); i++) {
while (contactPoint != nullptr) {
ContactPointInfo* contactPoint = narrowPhaseInfo->contactPoints[i];
// Look if the contact point correspond to an existing potential manifold // Look if the contact point correspond to an existing potential manifold
// (if the contact point normal is similar to the normal of an existing manifold) // (if the contact point normal is similar to the normal of an existing manifold)
@ -91,8 +92,6 @@ void ContactManifoldSet::addContactPoints(NarrowPhaseInfo* narrowPhaseInfo) {
// Add the contact point to the manifold // Add the contact point to the manifold
manifold->addContactPoint(contactPoint); manifold->addContactPoint(contactPoint);
} }
contactPoint = contactPoint->next;
} }
} }

View File

@ -37,7 +37,7 @@ NarrowPhaseInfo::NarrowPhaseInfo(OverlappingPair* pair, CollisionShape* shape1,
const Transform& shape2Transform, MemoryAllocator& shapeAllocator) const Transform& shape2Transform, MemoryAllocator& shapeAllocator)
: overlappingPair(pair), collisionShape1(shape1), collisionShape2(shape2), : overlappingPair(pair), collisionShape1(shape1), collisionShape2(shape2),
shape1ToWorldTransform(shape1Transform), shape2ToWorldTransform(shape2Transform), shape1ToWorldTransform(shape1Transform), shape2ToWorldTransform(shape2Transform),
contactPoints(nullptr), collisionShapeAllocator(shapeAllocator) { contactPoints(overlappingPair->getTemporaryAllocator()), collisionShapeAllocator(shapeAllocator) {
// Add a collision info for the two collision shapes into the overlapping pair (if not present yet) // Add a collision info for the two collision shapes into the overlapping pair (if not present yet)
overlappingPair->addLastFrameInfoIfNecessary(shape1->getId(), shape2->getId()); overlappingPair->addLastFrameInfoIfNecessary(shape1->getId(), shape2->getId());
@ -46,7 +46,7 @@ NarrowPhaseInfo::NarrowPhaseInfo(OverlappingPair* pair, CollisionShape* shape1,
// Destructor // Destructor
NarrowPhaseInfo::~NarrowPhaseInfo() { NarrowPhaseInfo::~NarrowPhaseInfo() {
assert(contactPoints == nullptr); assert(contactPoints.size() == 0);
// Release the memory of the TriangleShape (this memory was allocated in the // Release the memory of the TriangleShape (this memory was allocated in the
// MiddlePhaseTriangleCallback::testTriangle() method) // MiddlePhaseTriangleCallback::testTriangle() method)
@ -73,9 +73,8 @@ void NarrowPhaseInfo::addContactPoint(const Vector3& contactNormal, decimal penD
ContactPointInfo* contactPointInfo = new (allocator.allocate(sizeof(ContactPointInfo))) ContactPointInfo* contactPointInfo = new (allocator.allocate(sizeof(ContactPointInfo)))
ContactPointInfo(contactNormal, penDepth, localPt1, localPt2); ContactPointInfo(contactNormal, penDepth, localPt1, localPt2);
// Add it into the linked list of contact points // Add it into the list of contact points
contactPointInfo->next = contactPoints; contactPoints.add(contactPointInfo);
contactPoints = contactPointInfo;
} }
// Reset the remaining contact points // Reset the remaining contact points
@ -85,19 +84,16 @@ void NarrowPhaseInfo::resetContactPoints() {
MemoryAllocator& allocator = overlappingPair->getTemporaryAllocator(); MemoryAllocator& allocator = overlappingPair->getTemporaryAllocator();
// For each remaining contact point info // For each remaining contact point info
ContactPointInfo* element = contactPoints; for (uint i=0; i < contactPoints.size(); i++) {
while(element != nullptr) {
ContactPointInfo* elementToDelete = element; ContactPointInfo* contactPoint = contactPoints[i];
element = element->next;
// Call the destructor // Call the destructor
elementToDelete->~ContactPointInfo(); contactPoint->~ContactPointInfo();
// Delete the current element // Delete the current element
allocator.release(elementToDelete, sizeof(ContactPointInfo)); allocator.release(contactPoint, sizeof(ContactPointInfo));
} }
contactPoints = nullptr; contactPoints.clear();
} }

View File

@ -62,8 +62,8 @@ struct NarrowPhaseInfo {
/// Transform that maps from collision shape 2 local-space to world-space /// Transform that maps from collision shape 2 local-space to world-space
Transform shape2ToWorldTransform; Transform shape2ToWorldTransform;
/// Linked-list of contact points created during the narrow-phase /// List of contact points created during the narrow-phase
ContactPointInfo* contactPoints; List<ContactPointInfo*> contactPoints;
/// Memory allocator for the collision shape (Used to release TriangleShape memory in destructor) /// Memory allocator for the collision shape (Used to release TriangleShape memory in destructor)
MemoryAllocator& collisionShapeAllocator; MemoryAllocator& collisionShapeAllocator;

View File

@ -76,8 +76,8 @@ bool CapsuleVsConvexPolyhedronAlgorithm::testCollision(NarrowPhaseInfo* narrowPh
// two contact points instead of a single one (as in the deep contact case with SAT algorithm) // two contact points instead of a single one (as in the deep contact case with SAT algorithm)
// Get the contact point created by GJK // Get the contact point created by GJK
ContactPointInfo* contactPoint = narrowPhaseInfo->contactPoints; assert(narrowPhaseInfo->contactPoints.size() > 0);
assert(contactPoint != nullptr); ContactPointInfo*& contactPoint = narrowPhaseInfo->contactPoints[0];
bool isCapsuleShape1 = narrowPhaseInfo->collisionShape1->getType() == CollisionShapeType::CAPSULE; bool isCapsuleShape1 = narrowPhaseInfo->collisionShape1->getType() == CollisionShapeType::CAPSULE;