Best way to detect contact points / manifolds of overlapping kinematic bodies?

Post Reply
bennicus
Posts: 4
Joined: Wed Mar 30, 2022 9:29 am

Best way to detect contact points / manifolds of overlapping kinematic bodies?

Post by bennicus »

I have a world with multiple kinematic bodies and I want to be able to find all contact points between them. If I do something like:

Code: Select all

m_dynamicsWorld->stepSimulation(dt);
int numManifolds = m_dynamicsWorld->getDispatcher()->getNumManifolds();
for (int i = 0; i < numManifolds; i++) {
    btPersistentManifold* manifold = m_dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
...
I do get sensible looking manifolds and contact points, but the contact points shift slightly every frame. So for example, if I have a capsule intersecting the floor, I find the contact point and use it to "correct" the capsule location and move it up so that its sitting on top of the floor. On the next frame I move the capsule down again with some fake gravity and get another contact, but the contact point on the floor is slightly different so moving the capsule back to this position results in noticeable jittering.

I tried doing a capsule sweep instead, and get consistent results each time - I can set the capsule position to the sweep contact point and it's stable. Is there something different about how a sweep works, should I not be using the dispatcher manifolds in this way?
bennicus
Posts: 4
Joined: Wed Mar 30, 2022 9:29 am

Re: Best way to detect contact points / manifolds of overlapping kinematic bodies?

Post by bennicus »

I noticed that btKinematicCharacterController::recoverFromPenetration is actually doing something very similar to what I'm doing with my kinematic objects. However, when it applies the correction I don't understand why it multiplies the correction distance by 0.2?

Code: Select all

m_currentPosition += pt.m_normalWorldOnB * directionSign * dist * btScalar(0.2);
penetration = true;
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Best way to detect contact points / manifolds of overlapping kinematic bodies?

Post by drleviathan »

I would guess the 0.2 is a tuned magic number that allows the btKinematicCharacterController to extract itself from penetration over several substeps rather than in one. It moves out 20% every substep and if your simulation is running at 60 substeps per second then the character could reduce penetration to less than 1% after 21 substeps (so about 1/3 of a second), which is probably fast enough for human experience.
bennicus
Posts: 4
Joined: Wed Mar 30, 2022 9:29 am

Re: Best way to detect contact points / manifolds of overlapping kinematic bodies?

Post by bennicus »

I thought that initally, it's limited to 4 iterations per frame (see btKinematicCharacterController::preStep) so it will correct about 2/3rd of the error in a single frame. But if you are applying movement to your character every frame (e.g. walking into a wall) the error will be back again straight away and it'll never get corrected. Why not just correct the entire interpenetration in a single frame? (This is what I'm doing but I still get jittery motion because the reported contact point shifts a little every frame)

edit: I guess it could be a fudge to handle the case where the character has multiple contacts in a single frame. I'd prefer if it was a bit less magical and weighted the corrections by the amount of interpenetration of each, for example :lol:
Post Reply