Mirroring Box2D shape

Viewed 827

I use the following code to flip the shape. When I flip along x or y, it appears that shape is flipped.

b2FixtureDef fixd = fix->fixture;
const b2Shape *shape = fixd.shape;
if(shape->GetType()== b2Shape::e_polygon && flip)
{
        b2PolygonShape* ps = (b2PolygonShape*)shape;
        for(int i=0;i<ps->m_vertexCount;i++)
        {
            ps->m_vertices[i].x *= -1;
            //ps->m_vertices[i].y *= -1; // causing assert later
        }

        // 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--];

        ps->Set(&reVert[0], ps->m_vertexCount);
        body->CreateFixture(ps, 1.0f);
}

But if I flip x and y together I get an assertion b2PolygonShape.cpp, because area is negative.

// Centroid
b2Assert(area > b2_epsilon);

I'm not sure how else to flip both x and y. I need to mirror the shape and Box2D forum says

To mirror a polygon, mirror every vertex, then reverse the order of the vertices. I'm already considering adding a method for this.

2 Answers
Related