libGDX Box2D 断言失败

libGDX Box2D Assertion Failed

我正在使用 libGDX,从 box2D 收到 Assertion failed! 错误:

Assertion failed!

Program: C:\Program Files\Java\jre1.8.0_31\bin\javaw.exe
File: /var/lib/jenkins/workspace/libgdx/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp, Line 158

Expression: false

查看 file,第 158 行:b2Asert(false);,它位于 ::Set 函数中

n = tempCount;
if (n < 3)
{
    // Polygon is degenerate.
    b2Assert(false);
    SetAsBox(1.0f, 1.0f);
    return;
}

我发现这(很可能)源自一种调整对象形状大小的方法:

 private void updateShape(CubeComponent cc) {

    // Kill any existing shapes.
    if (cc.fixture != null && cc.fixture.getBody() != null) {
        cc.body.destroyFixture(cc.fixture);
        cc.fixture = null;
    }

    // don't even think about making a non-existing shape. It's almost as
    // bad as dividing by zero.
    if (cc.scale <= 0) {
        return;
    }

    // Make a fixture
    FixtureDef fdef = new FixtureDef();

    fdef.density = 0.1f;
    fdef.friction = 0.2f;
    fdef.restitution = 0.5f;

    ///////////////Most likely coming from here////////
    // Create the shape.
    PolygonShape shape = new PolygonShape();
    shape.set(new float[] {
            -cc.scale / 2f, -cc.scale / 2f, -cc.scale / 2f, cc.scale / 2f,
            cc.scale / 2f, -cc.scale / 2f, cc.scale / 2f, cc.scale / 2f
    });
    fdef.shape = shape;
    ///////////////Most likely coming from here////////

    // Create the fixture.
    cc.fixture = cc.body.createFixture(fdef);

    // dispose of the bad shape.
    shape.dispose();
}

这不应该是由宽度或高度为 0 的形状引起的,因为我已经检查过了。

我发现尺寸非常接近0的形状,比如0.000001,会被当成0。

此修复将更改以下内容

if (cc.scale <= 0) {
    return;
}

if (cc.scale < 0.001f) {
    return;
}

尝试按逆时针方向重新排列形状定义中的顶点。