ContactListener.Remove() called without Add()

So, in Box2DFlash 2.0, I understand that Add() and Remove() can be called more than once per timestep, but I'm having issues with Remove(). Most of the time, every call to Remove() for a shape pair is preceded with a call to Add() for the same shape pair, and I rely on this to manage my own array of contacts, but occasionally I'll get a collision between dynamic, non-bullet objects that only calls Persist() and then Remove(). Any ideas?
This happened with circle vs polygon. It was fixed in 2.0.1 (C++).
Thank you for the fast reply! 😀

This is kinda important to me right now, so I guess I'll have to compare 2.0.0 to 2.0.1 in C++, and then try to carry those changes over to the Flash port...
*boggles at the code*

...On second thought I'll let skatehead do it. 😕
I'm pretty sure that if you replace the existing Evaluate function in b2PolyAndCircleContact.as with this it should work:
Code:
public override function Evaluate(listener:b2ContactListener): void{
   
   var b1:b2Body = m_shape1.GetBody();
   var b2:b2Body = m_shape2.GetBody();
   
   //b2Manifold m0;
   //memcpy(&m0, &m_manifold, sizeof(b2Manifold));
   // TODO: make sure this is completely necessary
   m0.Set(m_manifold);
   
   b2Collision.b2CollidePolygonAndCircle(m_manifold, m_shape1 as b2PolygonShape, b1.m_xf, m_shape2 as b2CircleShape, b2.m_xf);
   
   // MY EDITS FOR 2.0.0->2.0.1ish BEGIN ABOUT HERE.
   var cp:b2ContactPoint = new b2ContactPoint();
   cp.shape1 = m_shape1;
   cp.shape2 = m_shape2;
   //cp.friction = m_friction;
   //cp.restitution = m_restitution;
   
   if ( m_manifold.pointCount > 0)
   {
      m_manifoldCount = 1;
      var mp:b2ManifoldPoint = b2ManifoldPoint(m_manifold.points[0]);
      
      if (m0.pointCount == 0)
      {
         mp.normalForce = 0.0;
         mp.tangentForce = 0.0;
         
         if (listener)
         {
            cp.position = b1.GetWorldPoint(mp.localPoint1);
            //var v1:b2Vec2 = b1.GetLinearVelocityFromLocalPoint(mp.localPoint1);
            //var v2:b2Vec2 = b2.GetLinearVelocityFromLocalPoint(mp.localPoint2);
            //var v1:b2Vec2 = b2Math.AddVV(b1.m_linearVelocity, b2Math.b2CrossFV(b1.m_angularVelocity, b2Math.SubtractVV(b1.GetWorldPoint(mp.localPoint1), b1.m_sweep.c)));
            //var v2:b2Vec2 = b2Math.AddVV(b2.m_linearVelocity, b2Math.b2CrossFV(b2.m_angularVelocity, b2Math.SubtractVV(b2.GetWorldPoint(mp.localPoint2), b2.m_sweep.c)));
            //cp.velocity = b2Math.SubtractVV(v2, v1);
            cp.normal = m_manifold.normal;
            cp.separation = mp.separation;
            cp.id = mp.id;
            listener.Add(cp);
         }
      }
      else
      {
         var mp0:b2ManifoldPoint = b2ManifoldPoint(m0.points[0]);
         mp.normalForce = mp0.normalForce;
         mp.tangentForce = mp0.tangentForce;
         
         if (listener)
         {
            cp.position = b1.GetWorldPoint(mp.localPoint1);
            //var v1:b2Vec2 = b1.GetLinearVelocityFromLocalPoint(mp.localPoint1);
            //var v2:b2Vec2 = b2.GetLinearVelocityFromLocalPoint(mp.localPoint2);
            //var v1:b2Vec2 = b2Math.AddVV(b1.m_linearVelocity, b2Math.b2CrossFV(b1.m_angularVelocity, b2Math.SubtractVV(b1.GetWorldPoint(mp.localPoint1), b1.m_sweep.c)));
            //var v2:b2Vec2 = b2Math.AddVV(b2.m_linearVelocity, b2Math.b2CrossFV(b2.m_angularVelocity, b2Math.SubtractVV(b2.GetWorldPoint(mp.localPoint2), b2.m_sweep.c)));
            //cp.velocity = b2Math.SubtractVV(v2, v1);
            cp.normal = m_manifold.normal;
            cp.separation = mp.separation;
            cp.id = mp.id;
            listener.Persist(cp);
         }
      }
   }
   else
   {
      m_manifoldCount = 0;
      if (m0.pointCount > 0 && listener)
      {
         var mp0:b2ManifoldPoint = b2ManifoldPoint(m0.points[0]);
         cp.position = b1.GetWorldPoint(mp0.localPoint1);
         //var v1:b2Vec2 = b1.GetLinearVelocityFromLocalPoint(mp0.localPoint1);
         //var v2:b2Vec2 = b2.GetLinearVelocityFromLocalPoint(mp0.localPoint2);
         //var v1:b2Vec2 = b2Math.AddVV(b1.m_linearVelocity, b2Math.b2CrossFV(b1.m_angularVelocity, b2Math.SubtractVV(b1.GetWorldPoint(mp0.localPoint1), b1.m_sweep.c)));
         //var v2:b2Vec2 = b2Math.AddVV(b2.m_linearVelocity, b2Math.b2CrossFV(b2.m_angularVelocity, b2Math.SubtractVV(b2.GetWorldPoint(mp0.localPoint2), b2.m_sweep.c)));
         //cp.velocity = b2Math.SubtractVV(v2, v1);
         cp.normal = m0.normal;
         cp.separation = mp0.separation;
         cp.id = mp0.id;
         listener.Remove(cp);
      }
   }
}

I commented out the code that doesn't work because b2ContactPoint (a struct in the C++ code) in version 2.0.0 doesn't support those variables yet. Commented as is you should be able to comment out / overwrite the current Evaluate function with this new one. Things appear to work okay for me so far but I'm not really 100% certain how to really check...
Oh wow, thanks! ^_^ Preliminary tests seem pretty positive on my side too.
Glad to hear it's working so far! 🙂
Okay, I discovered an issue with the new function you posted. The normalForce is getting set to NaN. 😐
shaktool wrote:
Okay, I discovered an issue with the new function you posted. The normalForce is getting set to NaN. 😐

I believe this happens because b2contactPoint is a struct in the C++ code. I'm super rusty on my C++ but it would stand to reason that uninitialized variables return 0 when accessed. If NaN frightens you (you can test for it in an if-block), you could simply add the last two lines in the following code snippet to your own code:
Code:
      var cp:b2ContactPoint = new b2ContactPoint();
      cp.shape1 = m_shape1;
      cp.shape2 = m_shape2;
      //cp.friction = m_friction;
      //cp.restitution = m_restitution;
      cp.normalForce = 0.0;      // NEW LINE!
      cp.tangentForce = 0.0;      // NEW LINE!

I made the above version working from revision 139. It is not the current head version in SVN. I went back a few because I didn't want to tread too far into 2.0.1 territory for fear of running into other unimplemented stuff. (On closer inspection it appears that my fears were unfounded - nothing really new that I didn't already side-step from version 139... oh well!)

Looking at the current head it appears that the only major change was to "fix contact id handling" (I assume it's broken in the code I posted above).

Actually, unless I'm missing something it doesn't look like the normalForce or tangentForce (renamed to normalImpulse and tangentImpulse in 2.0.1 respectively) get set to anything in the latest C++ version (revision 144) either. Someone more familiar with the inner workings of the C++ version have any insight? Erin?
b2ContactPoint no longer has impulse data because it is created before results have been computed. You can infer the strength of the collision using the relative velocity at the contact point (possibly projected onto the normal).

2.0.1 provides contact point impulse results separately in b2ContactResult.

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