contact SetEnabled (false) leads to body getting "stuck"

Hi folks,

I'm going to do my best to explain this...

Part of the app (game) I am developing uses a fairly traditional "platform" system, and thanks to the new Kinematic bodies in 2.1 this has become a whole lot easier.
I'm trying to create a "one sided" platform (as suggested in the doco "Pre-Solve Event" section). Essentially I am checking the normal vector of the contact and disabling the contact if it's not "0,1".

I have a rectangular platform and the contact disabling works as expected (ie if I "hit" the platform with a body from underneath it passes through), however I've hit a really weird problem:

My "player" character is currently represented by a simple circle shape. As this shape approaches the edge of the platform it reaches a point (somewhere on the corner) where my contact normal vector is not within the accepted range and the contact is disabled. At this point, the circle shape gets "stuck". That is, I can't move it in any direction.

I have discovered that the reason I can't move "upwards" is that box2d is continually increasing the velocity of the body downwards (to infinity as far as I can tell). So every step of the world increases the downward velocity of the body, hence I can't move up. But I can't move left or right either. (btw, I am manually setting linear velocity to move).

This only seems to happen on the corner of the platform. I have verified that the contact is being disabled each step when on this point.

It seems somehow that the circle shape is "supposed" to be falling, but is still colliding with the platform.. however because I have disabled the contact I'm guessing that box2d is thinking it's falling so increasing its "fall rate" but also preventing the shape from moving due to a collision with the corner of the platform. Complete guesswork here.

Is there any condition in which the cancellation of a contact would NOT prevent a collision? As I mentioned, box2d is correctly identifying that there IS a contact (every step) and PreSolve is being called (every step) and I am disabling the contact.

Curiously, I see the PreSolve being called, but do not see BeginContact or EndContact.. which would be expected given I have disabled the contact. So it seems the contact disabling is being registered, but somehow my shape is still colliding.

Maybe it has something to do with using a circle shape?

Anyone got any ideas?

Cheers,

Jason.

P.S.

Some further info:

This "stickyness" does not manifest if I remove the contact disabling.
The circle only gets stuck if it "creeps" to the edge. If I move at full speed it flies of the platform as expected.
I WAS experimenting with fixed rotation on the circle, and thought this may be related but that same thing happens in either case.
While the contact is "valid" I am adjusting the circle's velocity to match the platforms (so the "player" stays on the platform), but I have also disabled this and same problem exists.


================
UPDATE
================

So after some experimentation, things have gotten VERY strange. I changed my player body to be a box shape (polygon, setasbox) to see if it was maybe an issue with circles. When I touched the platform on the "solid" side, the same problem occurred. That is, the player box got completely stuck, and the linear velocity just started climbing. The strange thing was that I have other "platforms" in the scene, and none of these other ones exhibited the same problem. The ONLY difference between the problematic platform and the others was the order of creation.

It seems if I create my "player" body BEFORE I create any platforms, those platforms exhibit the same problem. It was just coincidence that the problematic platform was created AFTER the player body.

I fear this is getting more and more complicated, and difficult to reproduce without a test case. I don't have the test bed operational (I am compiling/running in Android) so I'm not sure I can create a test case. Just hoping someone has an insight here...?
Did you try disabling continuous collision?

Are you able to run the testbed on a PC? Perhaps you can create a testbed example that reproduces the problem.
Haven't tried disabling continuous collision, will do that.

Yeah.. I think I'm going to have to get the testbed running. It's going to be impossible for you to reproduce unless I can give you a test case.

I'll see how I go.

Cheers,

Jason.
OK.. if it wasn't official enough before, it is now. I'm an idiot.

I had some rather simple (albeit elusive) problems in the logic of my code. Because of the inherent "slipperiness" of thinks like platforms (i.e a rapid change in direction of the platform will cause the body resting atop to continue at the previous velocity.. minus a bit of friction force.) Totally correct physics, but not desirable for your standard "platform game". So I was manually adjusting the linear velocity of said body to match the platform's in this case.

I had some crufty logic which was adjusting the horizontal velocity to zero. This was preventing box2d from relieving the body of its confinement on the platform and keeping it stationary.

Sorry guys... my bad. 😳
I'm glad you got the problem resolved. 🙂
Thanks. Actually I am still having the "getting stuck issue".. my crufty code was contributing to a magnification of the issue where I was getting stuck at any point, not just the corner of the platform. However given my code seems particularly cruft-prone at the moment I am running with the assumption that I have a problem somewhere else leading to this behavior.

I have not seen any other body (other than the one I am manually adjusting) get stuck like this. This may just be by chance, but I have a bunch of platforms and boxes bounding about the place so it seems unlikely that it wouldn't have happened at least once (which I would have noticed) if it was a problem with Box2d.

I'll continue under the assumption that it's my logic, however if I do find an issue I will certainly produce a test case to verify.

Cheers,

Jason.
Code:
package {
   import Box2D.Dynamics.*;
   import Box2D.Dynamics.Contacts.*;
   import Box2D.Collision.*;
   import Box2D.Common.Math.*;
   import flash.utils.*;
   
   public class TSFixtureBehavior {
      public static var Solid:int = 0;
      public static var OneSided:int = 1;
      public static var Empty:int = 2;
      
      public function TSFixtureBehavior() {
         this.behavior = Solid;
         this.ropeBehavior = Solid;
      }
      
      public var behavior:int;
      public var ropeBehavior:int;
      public var onewayReversed:Boolean = false;
      public var behaviorForContact:Dictionary = new Dictionary(true);
      
      public function BeginContact(contact:b2Contact):void {
         var activeBehavior:int = Solid;
         var thisObjectIsA:Boolean = (contact.GetFixtureA().GetUserData() == this);
         
         if(behavior == OneSided) {
            var contactNormal = contact.GetManifold().m_localPlaneNormal.Copy();
            if(!thisObjectIsA)
               contactNormal.Multiply(-1);
            
            var enabled:Boolean = contactNormal.y < 0.0;
            if(onewayReversed) enabled = !enabled;
            contact.SetEnabled(enabled);
            activeBehavior = enabled ? Solid : Empty;
         }
         else if(behavior == Empty) {
            contact.SetEnabled(false);
            activeBehavior = Empty;
         }
         
         behaviorForContact[contact] = activeBehavior;
      }
      
      public function EndContact(contact:b2Contact):void {
         contact.SetEnabled(behaviorForContact[contact] == Solid);
         
         behaviorForContact[contact] = null;
      }
      
      public function PreSolve(contact:b2Contact, oldManifold:b2Manifold):void {
         contact.SetEnabled(behaviorForContact[contact] == Solid);
      }
      
      public function PostSolve(contact:b2Contact, impulse:b2ContactImpulse):void {
         contact.SetEnabled(behaviorForContact[contact] == Solid);
      }
      
      public function isSolidForRope(normal:b2Vec2):Boolean {
         switch(ropeBehavior) {
            case Solid:
               return true;
            case Empty:
               return false;
            default:
               if(onewayReversed) return normal.y >= 0.0;
               return normal.y < 0.0;
         }
      }
   }
}
I have logged an issue in relation to this:

http://code.google.com/p/box2d/issues/detail?id=119
There were a couple bugs. 117 and 119 have been fixed.

Thanks!
Awesome. I can confirm the issue is resolved.

Thanks!

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