Delayed Contact Remove Event?

Consider this scenario: A ball is resting on a box. An upward force / impulse is applied to the ball that is strong enough to lift it into the air. I would expect the sequence to be something like:

1. Force applied.
2. World time step.
3. Remove contact event.

But this is what's hapening:

1. Force applied.
2. World time step.
3. Persist contact event.
4. World time step.
5. Remove contact event.

This complicates games that differentiate between when a player jumps versus when a player falls off something. Can anyone give any sort of insight into this?

Thank you!
Well, I think you just have to be careful determining when you can jump (if I understand your problem). Something like if (onground && !jumping_previous_frame), or if (onground && velocity.y <= 0). I've been meaning to add these kinds of tests to my platformer as well, since my character will sometimes jump huge distances.
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.

Evaluating contact points twice a time step would be too expensive.

Here is the time step flow:
1. Evaluate contacts and report add/persist/remove.
2. Modify velocities according to external forces (e.g. gravity).
3. Solve for constraint forces.
4. Modify velocities according to constraint forces.
5. Modify positions according to velocities.
6. Correct position errors in constraints (e.g. reduce overlap).
7. Update broad-phase, potentially destroying some contacts and report removals.
I was with you until here:

Erin Catto wrote:
7. Update broad-phase, potentially destroying some contacts and report removals.


That bit about "report removals" - do you mean the ContactListener should receive a "remove" event here? Or is this event not reported to the listener until the next timestep (which seems to be the case).

I now understand why I receive a "persist" event when I apply a force that will definitely result in a contact removal (because the event is reported before the force affects position), but if I read the above sentence literally, it seems like I should receive both a "persist" and "remove" event in the same timestep.

Thanks... just trying to understand!
If the shapes are still "close to each other", then it will have to be evaluated in step one before it can be determined to be removed. It's only for things that have moved far away (AABBs don't overlap), which will get detected in step 7.
I threw something together in the test bed and took a screenshot so I can better explain this problem:

Attachment:
testshot.gif
testshot.gif [ 9.88 KiB | Viewed 516 times ]


This is what's going on here:

1. An upward impulse is applied to the ball as soon as it is created.
2. The first time step is run - an Add event is received.
3. This screen shot is taken before the next time step - visually we can see there is no contact, but we have not received a Remove event yet.
4. The next time step is run - a Remove event is received.

Interestingly, it looks like the AABB of the ball is stretched to contain it's original location and destination - so there's no way I'll get a Remove event in that first time step, no matter what.

Here lies the problem: Say an actor wants to maintain three states:

1. Touching something
2. Not touching anything (because it jumped)
3. Not touching anything (because it fell off something, or external forces were applied).

A naive contact listener may process contacts like so:

1. Jump! Apply a force and set the "jumped" flag to true.
2. Persist or Add event - Ok, we've landed or something obstructed our jump. Set "jumped" flag to false.
3. Remove event - No more contacts! Must've fallen off a cliff or been moved by something else.

Of course, this is easily solved by allowing the "jumped" flag to persist for a time step.
Have you considered ignoring removes in full, and only careing about add's presists?

Consider the following:

A player character is standing on a ground object, getting a add/presist.
The user presses 'jump'. The jump flag is set.
timestep
The player chacacter has a force applied that removes them from the ground object. While still getting the add/presist, we ignore it because the jump flag is set.
timestep
The player character no longer gets an add/presist event for the ground, we clear the jump flag, set an "in air" flag because there is no ground colision.
timestep timestep timestep
The player character lands on the ground, gets an add/presist event. We clear the "in air" flag.

So, in pseudo code, something like (provided as is (i.e. napkin code)):
Code:
def jump():
  if not self.inair:
    self.jump=True
    self.body.apply_upward_jumping_force()

def collide(self, other):
  if not self.jump and not self.inair
    if other == "ground":
      self.inair = False
  else:
    if other != "ground":
      self.inair = True
      self.jump = False
    else:
      self.inair = False

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