Contact issue

Yes, only two bodies in the world. I was debugging just this task. I also use BeginContact and EndContact to store list, but in that debug proccess it was clearer to see everything in the same update function 🙂

Thanks a lot for that link, I'll read it meticulously, maybe I should use other method to make bodies jump. Anyway the bug of IsTouching=true while the body is flying is still alive jejeje I'll post a link to this post in the bug section.

Thanks!!
At the point where you show the debugger window in the previous post, what output do you get if you put this code in there (just after the Col_AddB2Contact). You might need to #include <stdio.h>
Code:
bool logVertices = false;
//break here to set logVertices to true when the time comes
if ( logVertices ) {

   b2Fixture* f[2];
   b2Body* b[2];
   b2PolygonShape* s[2];

   f[0] = ce->contact->GetFixtureA();
   f[1] = ce->contact->GetFixtureB();

   for (int i = 0; i < 2; i++) {
      b[i] = f[i]->GetBody();
      s[i] = (b2PolygonShape*)f[i]->GetShape();
   
      int numVerts = s[i]->GetVertexCount();
      printf( "Fixture %d has %d vertices:\n", i, numVerts );
      for (int k = 0; k < numVerts; k++) {
         b2Vec2 v = b[i]->GetWorldPoint( s[i]->GetVertex(k) );
         printf( "%f, %f\n", v.x, v.y );
      }
   
   }

   fflush(stdout);
}

You can use Visual Studio's debugger to change the value of the 'logVertices' bool to true to avoid getting output every frame.
I uploaded the world info in this thread -> http://box2d.org/forum/viewtopic.php?f=4&t=8250
Thanks Loren. So that dump was taken immediately after the IsTouching returns true?
Do you have the same problem if you change
Code:
if ( _phy2dcontact->IsTouching() )
to
Code:
if ( ce->contact->IsTouching() )
Just wondering if that cast is safe...
Yes, the problem is the same. My class inherit from b2Contact.
I see. I'm just asking because there is no guarantee that c-style downcasts are safe. You could maybe try a dynamic_cast but it would be better to set the b2Contact* as a member in your Phy2DContact class instead of inheriting.

Hmm... how about if you log the calls to BeginContact and EndContact during this run, do you get one call to each?
Well.. in that example I use Phy2DContact which inherit from b2Contact, but i tried many ways, and not using my own class, and IsTouching also return true. BeginContact and EndContact are called in the right way. The problem is that EndContact is called some frames after the box gets separated from the grouond (for some reason the contact keep alive for a while, and also before the box fall and touch the ground). So it is not useful for my objetive (which is to know which bodies are in contact, and what face of them). Trying it in post-solve callback is also the same than doing it on the main update, when the body gets separated in the first frame, IsTouching returns true as well. So.. I don't know, dump information shows they are separated, I think it has to be a bug 😕 In fact in the dump log the bodies are separated. Has to be something about updating touch flag.
Okay. I am beginning to think that perhaps the contact is being corrupted.
- If you take out all the extra code you have (eg. Col_DelAllB2Contact, Col_AddB2Contact etc) do you still see the problem?
- After the contact is generated, how many steps pass before IsTouching starts to return true? Two?
- Just to be sure, you are not deleting any Phy2DContact inside Col_DelAllB2Contact right?

For example:
Code:
void Phy2DSystem::Update()
{
   for (int i = 0; i < num_scenes; i++)
   {
      Phy2DScene* _s = scene[i];

      _s->GetB2World()->Step(time_step, 10, 8);

      b2Body* body = ...set this to one of the bodies...;

      for (b2ContactEdge *ce = body->GetContactList(); ce; ce = ce->next)
      {
         if ( ce->contact->IsTouching() )
         {
            //check
         }
      }
   }
}
No problem with my code, it's quite simple, no phy2dcontact destroyed or something else.

About the steps.. firstly the box fall from air, contact is generated 3 frames before it touch ground. IsTouching is returning right values for those frames. It is false and when the body really touch ground it returns true. Then the body stand over the ground. Then LinearImpulse is applied, contact will be alive for 4 frames more (the frame where the body gets the impulse and separated from ground for the very first time, which is the frame where IsTouching() return wrong value, and three frames more where the contact is alive and IsTouching() returns false).
The results are the same with this code:

Code:
// Update del world
      _s->GetB2World()->Step(time_step, 10, 8);

      for (b2Body *_body = _s->GetB2World()->GetBodyList(); _body; _body = _body->GetNext())
      {
         for (b2ContactEdge *ce = _body->GetContactList(); ce; ce = ce->next)
         {
            b2Contact* _c = ce->contact;
            if (_c->IsTouching())
            {
               if (_s->debug)
               {
                  _s->GetB2World()->Dump();
               }
            }
         }
      }

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