Delayed Contact Events - Dead Simple Solution

In b2World -> Step move this call:

Code: Select all

m_contactManager.Collide();


To here (after Solve & SolveTOI calls):

Code: Select all

m_contactManager.Collide();
DrawDebugData();


Now the contact listener will report contacts in sync with the position of your bodies.

I looked back at Erin's comments when I first brought this up:

I have to evaluate contact points at the beginning of the time step so that newly added bodies generate contact points in the first time step.


So, if you add a new body/shape, this is what's going to happen (with this change implemented):

1. Your body/shape wont be moved by the contact solver until the next timestep.
2. You'll still get contact events for the new body/shape (I'm pretty sure of this, anyway).

The more I think about this the more I'm fine with it... If I add a body right on top of another body, it will stay there for one time step, and then pushed outwards the next. Lets say I click to add a body... I'd expect to see that body under my cursor for at least a brief moment.

This also makes the calls to Solve and SolveTOI in the first timestep essentially useless.

EDIT: Result events are still delayed.
It's not just added bodies though. If I move a body, you won't have re-evaluated it's contacts until after a step either. This could conceivably result in strange effects like a body bouncing off what it used to be touching, etc.
I still think it would be relatively simple to just put dirty bits on shapes.
This creates a problem for stacks. When the stack is simulated, it falls downward unrestrained in the first time step.

The solution would be to compute contacts when shapes/bodies are added to the world. Then I think this technique would work.

But why do you need contact points to be in sync at the end of the time step? Is it worth making the engine more complex?
Erin Catto wrote:But why do you need contact points to be in sync at the end of the time step?


to have better control over your character. i'm using a simple circle for the charcter shape. if the contacs are in sync with the timestep, i can calculate:

knowing, if jumping is allowed or not (i.e. angle between -60° and 60°) --> player is touching ground.

correct rotation of the char when standing on objects (getting min and max angles in both directions i.e.). there are also "old" contact points, which are not valid anymore, causing strange results 🙁

knowing when the character is in the air (it's a difference if you jump or if you left a platform, because of movement or an external force)

getting corect wall touching information (my charcter is "hanging" one second at walls, if angle and keyboard events are both ok)


there is no problem, with "small" delay, if movement is very slow. but moving a high speeds, the gap between the position of the body and the contact points are to big. you already answered in my post there: viewtopic.php?f=8&t=2867
have you checked the swf file? there you can see the problems.

there are a lot of workarounds, but none of them are very "accurate". use can use sensors at the bottom of your char. but what if moving on skewed ground? small sensors will lose contact, big sensors will make you jump in the air. another approach is to use small aabbqueries at different positions of your char. thats's working too, but is more like a workaround. if you want to detect real rotations, like i mentioned before, you have to do a lot of queries...
Erin Catto wrote:This creates a problem for stacks. When the stack is simulated, it falls downward unrestrained in the first time step.

The solution would be to compute contacts when shapes/bodies are added to the world. Then I think this technique would work.


Yeah... I considered exposing m_contactManager.Collide(); so that it can be called whenever you want (like before the first time step, or when you add a body). Haven't needed it yet though - really, everything looks fine. Having it called automatically might just complicate things.

Erin Catto wrote:But why do you need contact points to be in sync at the end of the time step? Is it worth making the engine more complex?


One example: I'm making a "sticky ball" that can roll up walls + ceilings by applying a force at the point of contact. With delayed contacts the ball drags since the point of contact (and the point at which the force is applied) is always "behind" the ball. Also if the ball is moving really fast, then suddenly becomes "sticky", it'll snap backwards, since the reported contact point is significantly far away from the ball. I was able to hack around this... but it's so much easier with the correct points.
What if you could update a particular contact? Would you have other stuff in the scene that doesn't need the current contact points? Another solution would be to apply the force in the Add/Persist callback. It is okay to apply forces in these callbacks.
Erin Catto wrote:What if you could update a particular contact? Would you have other stuff in the scene that doesn't need the current contact points?


this would be great. only a small amount of contact points is needed to be updated. mainly those contacts which are used by the player or enemies. contacts between "static" (i mean objects not controlled by a person or ai, like falling boxes etc.) objects are not interesting in this scenario.
Yeah, I needed to make the update function public to do "shapecasts," too.

Anyway, why wouldn't this work? (From the contact callbacks thread.)


OK. If you want to fix it, then it wouldn't be too expensive to evaluate the contacts early, assuming manual shape movement is more rare.

Step 7:

Code: Select all

for each shape:
    shape.dirty = false
for each contact:
    contact.Evaluate()


Step 1:

Code: Select all

for each contact:
    if (contact.shape1.dirty || contact.shape2.dirty)
        contact.Evaluate()

The dirty bit defaults to true, and anything that modifies the shape sets the dirty bit.

The only problems I can see with this solution are things like resetting a shape to the same position each time might experience more instability.



You can also make it so that SetXForm doesn't update the pair buffer immediately. The pair buffer only needs to be updated when used for things like raycasts or to manually check contacts.

BTW, if you check relative velocity, you don't need to have up-to-date contacts to determine if you're touching the ground:

Code: Select all

// Parameters
body = character body
direction = normalized direction of gravity
angleMargin = margin on either side of direction to consider ground (the angle of the steepest slope; I use 55 degrees)

// some precalculated values
velocity = dot(direction, body.linearVelocity) + epsilon
angleMarginCos = cos(angleMargin)

// loop over all valid contacts
for each contact involving body:
    shape1 = contact.shape1
    shape2 = contact.shape2
    signedDirection = direction
   
    // Flip so shape1 involves the current body
    if shape1.body != body:
       swap(shape1, shape2)
       signedDirection = -signedDirection   
   
    // If moving faster in that direction, we're not touching it
    if dot( direction, shape2.body.linearVelocity ) > velocity:
        continue
   
    // Check relative orientation
    for each manifold:
       if dot(manifold.normal, signedDirection) >= angleMarginCos:
           return true

// None work; we're not touching the ground
return false
Hangar: Dirty contacts wouldn't eliminate out of date contacts - it would just make them rarer. Once I've moved a body, it's got dirty contacts from it's old joints, and probably doesn't even have its new ones. How should users going to deal with this fact?

(rest of post deleted - I need to think more about the whole problem)

Original thread: https://box2d.org/forum/viewtopic.php?t=2875