Contact events triggered between static objects (?) [fixed]

Seems the only problems I have posted to this forum are contactListener-related. *sigh*

At least I have a working sample this time: http://www.andymoore.ca/chemistry/dev/10.swf (243Kb)

(excuse the mess, it's a work in progress - WASD to move)

Short story: contactListener.REMOVE is not being called all the time.

Long story:

The code allows the player to jump only if he is not touching 0 surfaces.

My custom contactListener class increments/decrements this accordingly:

Code:
      public override function Add(point:b2ContactPoint) : void {
               if (point.shape1.GetUserData() == "GroundSensor") {
                  point.shape1.GetBody().GetUserData().groundCount++;
               }
               if (point.shape2.GetUserData() == "GroundSensor") {
                  point.shape2.GetBody().GetUserData().groundCount++;
               }
         }

(remove class identical just -- instead of ++)


This code works - if you load up the SWF and just hit "W", I've traced out the results:

Code:
GroundCount: 0
GroundCount: 1 // player just settled upon execution
Player Jumped // registered "W" keypress
GroundCount: 0 // player is in the air
GroundCount: 1 // player landed again


Here's the strange bit. Go walk up to the left wall and jump while pressing against it (just hold down W and A). The player will start to "jitter" up the wall. After a second of this let go of the keys and you will settle back to the ground. The trace shows:

Code:
GroundCount: 1
Player Jumped
Jumped
Jumped
Jumped
Jumped
GroundCount: 2 // settled back to ground
Jumped
Jumped
Jumped
GroundCount: 3 // settled down again


The groundCounter keeps incrementing every time the player "lands" on the ground surface, which shows ContactListener.ADD is performing properly. REMOVE also works fine as long as you aren't up against that wall. But when up against the wall - remove no longer works.

The above linked .SWF has debugDraw on - the player char is a circle, and you can see another smaller circle overlapping it to the bottom - this is the "GroundSensor". You can see the groundsensor is not colliding with the left wall.

The unfortunate - and game-killing side effect of this - is the GroundCount will now remain above 0 always and the player can "fly" by holding down the jump button. 🙁

Any clues as to what is causing this?
One thing I notice is that the problem doesn't start to happen until you're holding down W while pressed against the wall. Merely pressing against the wall without trying to jump doesn't start the trouble, so the contact listeners are working correctly up until the jump code path runs while up against the wall.

I'd focus in on the code that's executed when the key is pressed, maybe it's doing something to the listeners?
Nope, the code is really quite simple and straightforward:

Code:
                            if (playerIsOnTheGround()) {
            if (Input.isKeyDown(87) && body.GetLinearVelocity().y > -9) {
               y += -5*forceAmp; // W
               trace("jumped");
            }

            forceVec = new b2Vec2(x,y)
         body.ApplyImpulse(forceVec, body.GetWorldCenter())


I've ended up trimming out a lot of extraneous code in my efforts to find the source of this. 🙁
Have you got some code running when you hit the wall? It seems like something different happens even if you're not pressing W depending on whether the count has gotten off or not (you slide down before it goes wrong, even if holding left, whereas after it happens, you stick).
Yeah, I have some code that turns the friction really low if you are in the air (hence you slide down the wall), but friction is high if you are on the ground. Since it thinks you are on the ground still it keeps the high friction going.

Code:
         if (grounded > 0) setFriction(10);
            else setFriction(0);


Ooh - that block of code runs during the contactFilter. That's probably not a good thing - changing the object properties during the step. I'll rip that out and see if it fixes.

edit: no dice. commenting out that block of code has no effect on the glitch.
Ok, well at least we're ruling some things out.

Looking through that trace, you're right, the problem is definitely the remove event. Can you trace those callbacks to see if one is ever getting called at all upon the remove that should be coming from the ground (even if it doesn't pass your if tests)?
Well, I did up a trace to record every single add/remove. - is remove, + is add:

First settling the player into place, then jumping once normally as a baseline test:

Code:
+ [object WallObj] <- floor contact with player
+ [object PlayerObj] <- player circle contact
+ [object WallObj] <- floor contact with GroundSensor
+ Groundsensor, trigger ++ <- groundSensor contact with floor

jumped
- [object WallObj]
- [object PlayerObj]
- [object WallObj]
- GroundSensor, triggering --
+ [object WallObj]
+ [object PlayerObj]
+ [object WallObj]
+ Groundsensor, trigger ++


All seems good.

Walking over towards the wall:

Code:
+ [object WallObj]
+ [object PlayerObj]
- [object WallObj]
- [object PlayerObj]
+ [object WallObj]
+ [object PlayerObj]


Looks like the player character is bouncing a tiny bit as he walks, but not enough to change the intersection of the groundsensor.

Here's pressing against the wall and jumping:

Code:
jumped
- [object WallObj]
- [object PlayerObj]
+ [object WallObj]
+ [object PlayerObj]
- [object WallObj]
- [object WallObj]
- [object WallObj]
- [object WallObj]

And that looks like it could be the problem, but I'm not sure how. It just removed 3 more "WallObj"'s than already existed, if I'm reading it right. Continuing to hold the jump button down:

Code:
jumped
- [object WallObj]
- [object PlayerObj]
+ [object WallObj]
+ [object PlayerObj]
(repeat x6)
jumped
- [object WallObj]
- [object PlayerObj]
+ [object WallObj]
+ [object PlayerObj]
+ [object WallObj]
+ Groundsensor, trigger ++

right at the end there is giving up and falling back to the ground, triggering GroundSensor and adding to the contactCount.

I followed this up with more jumping around, but they all looked identical to the first one so I won't include them here
Yep, those wall object/wall object removals look suspicious - two static objects should never have remove events between them. It looks to me like what's really happening is that one of those should be the sensor/floor remove event, but somehow it's getting reported as a floor-floor or floor-wall remove, instead (perhaps one of the arguments is being overwritten with the other or something like that).

If it's true that contact callbacks are receiving two static objects, then this does indeed appear to be an engine bug. I unfortunately don't know much about the AS3 internals (I took a look through and compared to the C++ code, but didn't see anything obviously wrong), but maybe someone that has hacked around them a bit can take a look - I've re-titled the thread to catch Boris' attention, he may have some idea what's happening here.

To get a little more information here and confirm that this really is an internal thing, can you verify that the callbacks are receiving two static objects (or even the same static object)? You should be able to check the shape.GetBody().IsStatic() value to see this.
Hmm, looks like that might be the case:

Code:
jumped
- [object WallObj] isStatic: true
- [object PlayerObj] isStatic: false
+ [object WallObj] isStatic: true
+ [object PlayerObj] isStatic: false
- [object WallObj] isStatic: true
- [object WallObj] isStatic: true
- [object WallObj] isStatic: true
- [object WallObj] isStatic: true


Edit: Better understanding what exactly that means now, and I'm pretty sure it's an engine bug now too.

PS: Changing the "subject" at the top of a post only changes the subject for that reply, which is a real silly feature if you ask me. I edited my original post to reflect your change though.
I've been reading the post since the beginning, you don't need say my name three times to summon me 🙂.

I've kept quiet because I cannot see any discernible cause. I agree with the diagnosis, you've kept things simple enough to indicate a problem with the engine. I'll have a look at the code, but I cannot think of any particularly dodgy subsystem or recent change that might have caused this. I suppose you could try rolling back to revision 35 - before some broadphase code I'm not entirely happy with.

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