镜像Box2D形状

我使用以下代码来翻转形状.当我沿着x或y翻转时,看起来形状被翻转.

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);
}

但是,如果我将x和y一起翻转,我会得到一个断言b2PolygonShape.cpp,因为区域是负数.

// Centroid
b2Assert(area > b2_epsilon);

我不确定如何翻转x和y.我需要反映形状和Box2D论坛说

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

最佳答案 如果同时翻转x和y轴,则无需反转顶点的顺序.

可以这样想……如果你翻过两个轴,那么你基本上是在x轴和y轴上翻转.如果每次翻转一个轴时反转顶点,就会反转两次顶点,最终会得到与开始时相同的顺序.

点赞