DanielChappuis / reactphysics3d

Open source C++ physics engine library in 3D
http://www.reactphysics3d.com
zlib License
1.54k stars 224 forks source link

Capsule vs Capsule Collision Make sure the penetration depth is not zero #382

Closed robertocapuano closed 5 months ago

robertocapuano commented 7 months ago

Sometimes assertion fails in NarrowPhaseInfoBatch::addContactPoint(): assert(penDepth > decimal(0.0)); This happens for Capsule/Capsule collision check.

Probable cause is float approssimation in bool CapsuleVsCapsuleAlgorithm::testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint32 batchStartIndex, uint32 batchNbItems, MemoryAllocator& /*memoryAllocator*/) ;

I inserted a double check on penetrationDepth.

if (penetrationDepth > 0)

The logic is the same of SphereVsSphereAlgorithm::testCollision().

AthosArantes commented 5 months ago

Is the sqrt() really needed? Why not compare the difference of closestPointsDistanceSquare and sumRadiusSquare with an epsilon instead?

robertocapuano commented 5 months ago

I tried with if ( fabsf(closestPointsDistanceSquare - sumRadius * sumRadius )<eps )

with different values for eps:

but after few seconds of simulation I always get assert(penDepth > decimal(0.0)); in NarrowPhaseInfoBatch::addContactPoint

AthosArantes commented 5 months ago

After reading a second time, what about something like std::max(sumRadius - closestPointsDistance, MACHINE_EPSILON) in https://github.com/DanielChappuis/reactphysics3d/blob/c2201c50ea57ec4ed6c8e718981f0df56dfa9450/src/collision/narrowphase/CapsuleVsCapsuleAlgorithm.cpp#L192

robertocapuano commented 5 months ago

It works well, I tried it for a few minutes without errors.

robertocapuano commented 5 months ago

Reverted the previous change and pushed the fix on this branch.

DanielChappuis commented 5 months ago

Thanks a lot for your pull request. I have merged your first commit into the develop branch and refactored it a bit myself before your last two commits. Are you able to try the develop branch with your test and see if the issue is now fixed?

robertocapuano commented 5 months ago

Just tried it for a few minutes: it works. Thank you.

DanielChappuis commented 5 months ago

Just tried it for a few minutes: it works. Thank you.

Thanks a lot for your feedback. That's very helpful.