Delayed Contact Events - Dead Simple Solution

Quote:
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.


The problem with this is sometimes I need all the contacts points in order to evaluate which one is ideal. For example, when jumping, I want to use the most ground-like contact.

This is the next solution I'm going to try: Put the "m_contactManager.Collide()" call back where it belongs, but put a callback function right after it where I can execute my own code. That way I can handle my ContactPoint-sensitive stuff there before "Solve()" gets its dirty hands on it and repositions stuff, and I'll have all my up-to-date buffered ContactPoints to play with.

With "Collide()" called after "Solve()" I seem to have a problem with projectiles created on top of other objects (like a cannonball created over a cannon and given a high velocity). Depending on the initial velocity of the projectile, sometimes I get that initial overlap contact point, sometimes I don't. Getting that point reported is critical if I want to filter the first contact for overlapping shapes, but let them interact later (once they naturally move apart). If I don't get that first contact point, I'll end up filtering the contact I don't want to filter.

...Yeah, that probably made no sense at all.
In most of the games I've worked on in Box2D, I have done exactly as mayobutter suggests in the first post. Yes, it puts things in free fall for the first time step after construction, but this is way more acceptable for my games than continuously delayed contact reporting. Of course, an ideal solution solves both problems, but I just wanted to emphasize that some applications need a tighter interface between the physics engine and the game engine, even at the cost of physical accuracy. It would be nice if people had the option to do what I do, without modifying Box2D's source code.
Just to be clear, moving the evaluation to after the loop doesn't cause any problems? I would have thought it plays badly with altering the world.
Moving the collision evaluation to after the velocity/position integration does not cause any problems that last longer than one frame after constructing or teleporting shapes. Even within that one frame, the issue is usually not noticeable, in my experience, or at least less noticeable than a constantly laggy player character. Constructing and teleporting are usually rare events in my use cases, compared to collisions that need immediate qualitative responses.
99% of the time it works fine. Unfortunately I'm working on that 1% where it's not so ideal.

I managed to get the ContactManager to immediately call Update on contacts generated while creating a new shape using this snippit in PairAdded():

Code:
if(shape1.isNew || shape2.isNew) {
c.Update(m_world.m_contactListener);
}


so, great! Now my 1% of shapes that are super-sensitive on creation can properly enter their first time step (and the other 99% can leave "isNew" false).

But then I immediately get a remove event when calling SetMassFromShapes(), because the engine thinks the body is going from static to dynamic and refiltering proxies. So, fine, I'll give the body a tiny mass before adding the shape. And it works! I don't like this little SetMass() hack though, so if anyone wants to send me in a more proper direction feel free.

P.S. The b2ContactManager and b2PairManager classes make my head hurt...
I've posted my thoughts on the other thread. It's a proposal that includes a lot of what has been discussed here, and more.

Though you have all been reporting that things work quite well with a bit of sloppiness (missing 1st step contacts, or ignoring manual moves, or requiring manual hinting), I have still gone out of my way to avoid it. Engines need a solid theoretical base, and need to work in all use cases, and I'm just too worried it'd fall apart in other circumstances.
I like the ideas being tossed around over there but they are very ambitious, and I don't see them trickling down to the AS version for a long time.

I thought about Hangar's "dirty bit" suggestion and that's led me down another route. I've restored the correct order of "Solve()" and "Collide()" and added a function to b2World to immediately update a contact:

Code:
public function updateContact(c:b2Contact) {
   c.Update(m_contactListener);
   c.skipUpdate = true;
}


the "skipUpdate" flag (which is functionally opposite from Hangar's "dirty flag" - it could be called "is clean") just tells the "Collide()" function to skip updating that contact (since it's already been updated).

I'm using a slightly tweaked copy of Box2d that passes the b2Contact along with events in the contact listener, which is what makes this all work. Without access to the b2Contact, the "updateContact" function above would be useless.

So now my player can call "updateContact" for all of it's buffered contacts to get everything in sync. I'm probably going to move things around a bit (the only reason I put "updateContact" in the world is because b2Contacts don't have access to the contact listener themselves) but this seems like a reasonable approach.

I also suspect I'm going to get duplicated events this way - going to have to test and tweak.
That's a much simpler solution than lazy evaluation of contacts, albeit with more cost to the user. Whether you call it a clean bit or a dirty bit is irrelevant though, you still need to go through every mutating Box2D operation and have it toggle the bit.

I should point out that "obscuring contacts" was meant to be an indepentand suggestion, just one not practical/plausible until the rest of the structure was in place.

I have this nasty habit of lumping together unrelated ideas. I shall try to clarify this in the other thread.

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