Search code examples
javalibgdxbox2dmirroring

Mirror Body built with vertexes in box2d and libgdx


How can I mirror the fixture of a body in one axis(x or y), because when I want to mirror it only in one, an assertion error occurs, but when i mirror it in the two axis, no problem occur.

public Vector2[][] getPolygons(String bodyName, float scaleX, float scaleY)
{
    Vector2[][] vectors = null;

    Element fixture;
    Element semiPolygon;
    float auxX, auxY;

    this.element = reader.parse(xml);
    fixture = this.element.getChildByName(bodyName);

    vectors = new Vector2[fixture.getChildCount()][]; 
    for(int child = 0; child < fixture.getChildCount(); child++)
    {
        semiPolygon = fixture.getChild(child);
        vectors[child] = new Vector2[semiPolygon.getChildCount()];
        for(int part = 0; part < semiPolygon.getChildCount(); part++)
        {
            auxX = semiPolygon.getChild(part).getFloatAttribute("x")*-scaleX;
            auxY = semiPolygon.getChild(part).getFloatAttribute("y")*-scaleY;
            vectors[child][part] = World.toGameCoordinates(auxX, auxY);
        }
    }

    return vectors;
}

Solution

  • Polygon vertices must be specified in counter clockwise order. Each time the shape is mirrored the winding order is reversed, so with only one mirror the order will be backwards, then if you mirror it again it will be ok, as you have discovered. So if you only mirror over one axis, you'll need to reverse the order of the vertices.