Flipping shape

Hi! I need some help to flip a fixture...
When i create a body,normally it collides with other object.
If i flip the body, with the following function, it doesn't collides anymore.
I don't understand... i reverse the vertices to make sure they are in counter-clockwise order.
It should be work :/

If i reverse the vertices and i destroy the old fixture, then i create a new one with the reversed vertices, it work...
Something "magic" happens on CreateFixture? 🙂

PS: i'm sorry for my bad english 🙁

Code: Select all

-(void)flipFixture:(BOOL)horizzontally
{
   b2Fixture* fix = physBody->GetFixtureList();
   
   while(fix)
   {
      b2Shape* shape = fix->GetShape();
      if(shape->GetType()== b2Shape::e_polygon)
      {   
// flipping x or y coordinates
         b2PolygonShape* ps = (b2PolygonShape*)shape;
         for(int i=0;i<ps->m_vertexCount;i++)
            horizzontally ? ps->m_vertices[i].x *= -1 : ps->m_vertices[i].y *= -1;

         //revert the vertices
         b2Vec2* reVert = new b2Vec2[ps->m_vertexCount];
         int j = ps->m_vertexCount -1;
         for(int i=0; i<ps->m_vertexCount;i++)
            reVert[i] = ps->m_vertices[j--];

#if 0 //destroy the fixture and create a new flipped fixture

         b2PolygonShape polyShapeSx;      
         polyShapeSx.Set(&reVert[0], ps->m_vertexCount);
         physBody->DestroyFixture(fix);
         physBody->CreateFixture(&polyShapeSx, 1.0f);
         return;
#else      
//copy the reverted vertices to the shape array.
         for(int k=0; k<ps->m_vertexCount;k++)
            ps->m_vertices[k] = reVert[k];   
#endif
      }
      fix = fix->GetNext();
   }
}
You have to also inverse the order of the vertices, not just the vertices themselves (doing that will make them CW). ie, vertice length-1 becomes vertice 0, length-2 becomes vertice 1, etc etc.

EDIT: oh, I see you did that. That should work; did you also recalculate the normals of the shape? Make sure you call the Set function.

-P
comment out:

Code: Select all

for(int k=0; k<vertexCount;k++)
     ps->m_vertices[k] = reVert[k];


code in instead:

Code: Select all

ps->Set(&reVert[0], vertexCount);


works for me...

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