My Refilter() problem

I'm working on a Platform Game engine, and I have a class called a "Trigger", which essentially dispatches events to the developer when a character enters or exits the trigger (The trigger is a sensor).

Everything works great, except I'm getting rogue ENTER and EXIT dispatches (while I'm inside the Trigger), which I've found only occur immediately following a call to Refilter() on a shape that is inside the Trigger. I call Refilter() periodically to change things such as friction, filtering, sensor, etc.

The Enter and Exit events get dispatched when all shapes in a body completely leave or enter the trigger's area. I'm assuming these incorrect dispatches are happening because shape contact points get removed and re-added (via the ContactListener) when a shape is refiltered. Is this correct? How can I get around this?

My best option so far has been to not dispatch ENTER and EXIT events during a refiltering phase, but then there is a chance of missing a proper ENTER or EXIT event.

Any guesses? I'm completely out of answers and my client REALLY needs this figured out.

Thanks,

Eric Smith
http://blueflamedev.com
Good question. I think you are best off (when Refiltering), deferring checking your overlap counts for your Enter and Exit events until after the Refiltering.

I.e.
deferEvents = true;
for each (shape in body.shapes)
refilter(shape)
commitEvents();
deferEvents = false;

Refilter does destroy and recreate contacts, as it's main use is for changing filtering, where this is necessary. If you want to change other variables, like cached friction etc, you could also delve into the Box2D internals, and change it in place, but I would recommond the first instead.
Here's my game loop:

Code:
box2D.step();
updateAllProps(); //props are wrappers for Box2D objects
refilterMarkedProps()
destroyMarkedProps()
draw() //move everything, change animations, etc


I wouldn't be against some critique of my loop, but the reason I show that is to point out the refiltering phase. You see, I let Box2D do all its stepping, then I do all my custom updates, during which time some shapes may get marked for refiltering. Then, during the refiltering phase, if any props need refiltering, I can set engine.refiltered to true, so that during the next game loop I can not listen for contacts. This worked quite well, but I found that I could sneak into a Trigger area (or out of one) without being noticed if I happened to enter a trigger area during a loop that has refiltered set to true.

Much of my problem is that I change the Hero's friction when the user presses the run key (and then also when they release it. I also change the top half of the hero to a sensor when the user ducks. If there was a way to not need a refilter after a change to friction and isSensor, that may help, unless based on my loop and explanation, we can think of a better way...

Thanks for all your help!
Is there a problem with implementing what I suggested, I cannot quite see what you mean?

I do not want to go into hacking through the Box2D internals, as it is quite involved. I believe mayobutter currently uses this trick though, you may want to PM him.
motionsmith wrote:
Much of my problem is that I change the Hero's friction

Hi there why do you need to change the hero's friction ?
I guess I'm not understanding what you mean when you say to defer checking my overlap counts while it's refiltering. I thought maybe you meant that I shouldn't check the trigger overlap during a frame that I refilter a shape, but maybe you mean something different. I hate to say it, but I might need a bit less pseudo-code in the example. If I don't check for a collision during a frame where a refilter happens, then sometimes I miss a collision event that SHOULD occur.

The reason I change friction is because complete realism is not always desirable in games. Some things need to be fidgeted with in order to create better control. For instance, if the hero is standing on top of a crate, if both objects have friction, then the crate will move with the hero, which I do NOT want. The player expects to run and jump off the crate without it moving, so the hero needs 0 friction. However, when standing on a moving platform, if the hero has 0 friction, then the moving platform will move right out from under him. The hero needs the friction in order to stay on it. These are just two cases, not the only ones. I might be able to come up with different methods for each situation, but controlling friction works VERY well. I just set it to 0 when an arrow key is pressed, and when the user releases the key, I set friction back on so that he slows down.

I realize some of this may be difficult to explain (on both sides) without the actual code in front of us. The engine is actually in a beta stage at www.citrusengine.com if you'd like to request a copy of it.
You have enter and exit events for your engine, so I assume you were using a count to determine when they happen:
Code:
ContactAdd(){
if(GetAppropriateCounter()==0)
  OnEnter();
IncrementAppropriateCounter()
}
ContactRemove(){
DecrementAppropriateCounter();
if(GetAppropriateCounter()==0)
  OnExit();
}


I mean changing it to
Code:
ContactAdd(){
if(!deferEvents&&GetAppropriateCounter()==0)
  OnEnter();
IncrementAppropriateCounter()
}
ContactRemove(){
DecrementAppropriateCounter();
if(!deferEvents&&GetAppropriateCounter()==0)
  OnExit();
}
CommitEvents(){
for each pair(){
  if(GetOldCounter()&&!GetNewCounter())
    OnExit();
  if(!GetOldCounter()&&GetNewCounter())
    OnExit();
}
}


Pretty ugly, but you get the idea.Instead of running your custom events immediately, you wait until you've received all events that Refilter is going to show, and then decide what to dispatch.
motionsmith wrote:
The reason I change friction is because complete realism is not always desirable in games. Some things need to be fidgeted with in order to create better control. For instance, if the hero is standing on top of a crate, if both objects have friction, then the crate will move with the hero, which I do NOT want. The player expects to run and jump off the crate without it moving, so the hero needs 0 friction. However, when standing on a moving platform, if the hero has 0 friction, then the moving platform will move right out from under him. The hero needs the friction in order to stay on it. These are just two cases, not the only ones. I might be able to come up with different methods for each situation, but controlling friction works VERY well. I just set it to 0 when an arrow key is pressed, and when the user releases the key, I set friction back on so that he slows down.

ok thanks for sharing that with me.
I also noticed the thing with walking on boxes it was usually when the boxes had a big density diff to the player.

Seems like you got it all under control but maybe a rolling ball would work better than a sliding ball.
btw nice looking engine. beautiful graphics.
it would be cool if the box rolled over if you duck after running .
Like in kirby when you get the rock ability you can roll down hills its pretty fun.

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