Strange collision response for static b2LoopShape vs. b2Poly

Hi Everybody!

I am integrating Box2D with my engine and I am impressed with it's features.
I am making static walls using b2LoopShape and steer dynamic box/b2PolygonShape using velocities.
Very seldom my body behaves like it's sucked inside wall instead of being pushed away (even when there is no
velocity or force applied).

Please have a look at following images, one before simulation:

Image

and one after:

Image

I prepared test case, just copy a code to MyTest.h and include in test suite.
(pause, restart and step thru simulation).
I work with latest Box2D svn code.
I tried to debug it myself but I lacks understanding of Box2D internals 🙁.
Please help,
Cheers
Tristan

Code:

#ifndef MYTEST_H
#define MYTEST_H

class MyTest : public Test
{
   b2Body* ground, *stickBody;

public:
   MyTest()
   {
      m_world->SetGravity(b2Vec2(0.0f, 0.0f));

      b2BodyDef groundDef;
      ground = m_world->CreateBody(&groundDef);

      b2Vec2 verts[12] = {
         b2Vec2(3.250000f, 1.827835f),
         b2Vec2(2.225373f, 2.750000f),
         b2Vec2(3.250000f, 2.750000f),
         b2Vec2(3.250000f, 1.827835f),
         b2Vec2(3.375000f, 1.827835f),
         b2Vec2(3.375000f, 2.812500f),
         b2Vec2(3.312500f, 2.875000f),
         b2Vec2(2.062500f, 2.875000f),
         b2Vec2(2.020690f, 2.766044f),
         b2Vec2(3.270690f, 1.641044f),
         b2Vec2(3.375000f, 1.687500f),
         b2Vec2(3.375000f, 1.827835f),
      };

      float scale = 4;
      for ( int i=0; i<12; ++i )
         verts[i] *= scale;

      b2LoopShape groundShape;
      groundShape.Create(verts, 12);

      b2FixtureDef groundFixtureDef;
      groundFixtureDef.shape = &groundShape;
      ground->CreateFixture(&groundFixtureDef);

      b2BodyDef stickDef;
      stickDef.type = b2_dynamicBody;
      stickBody = m_world->CreateBody(&stickDef);

      b2PolygonShape stickShape;
      stickShape.SetAsBox(scale*0.51562500f, scale*0.093750000f);
      b2FixtureDef stickFixtureDef;
      stickFixtureDef.shape = &stickShape;
      stickFixtureDef.density = 1;
      stickBody->CreateFixture(&stickFixtureDef);
      stickBody->SetTransform(scale*b2Vec2(3.4060252f,1.4993361f), 2.892967f);
   }

   static Test* Create()
   {
      return new MyTest;
   }
};

#endif

I filed an issue for this: http://code.google.com/p/box2d/issues/detail?id=159

I'll try to look at this soon.

For now, you can probably avoid the problem by making two loop shapes and getting rid of the junction. Or you could just make one outer loop shape.
I implemented an improved edge chain shape, however I don't think self-intersection will ever work well.

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