box2d崩溃了小多边形

时间:2015-02-06 16:09:33

标签: java box2d

我试图在我的世界中切割身体。我的代码基于this。但是当新的多边形形状非常小时,我会收到此错误。

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
AL lib: (EE) alc_cleanup: 1 device not closed
Assertion failed!

Program: C:\Program Files\Java\jdk1.7.0_65\bin\java.exe   
File: /var/lib/jenkins/workspace/libgdx/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp, Line 158

Expression: false

以下是我创建新主体的代码

 private void creatBody(Array<Vector2> vs){
        if (vs.size >= 8){
            System.out.println("Vectors = " + vs.size);
        }
        BodyDef bodyDef = new BodyDef();
        PolygonShape shape = new PolygonShape();
        FixtureDef fixtureDef = new FixtureDef();

        Vector2 centre = findCentroid(vs, vs.size);
        if (Float.isInfinite(centre.x) || Float.isNaN(centre.x) || (Float.isInfinite(centre.y) || Float.isNaN(centre.y))){
            System.out.println("centre : " + centre.toString());
            return;
        }

        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(centre.x, centre.y);

        Vector2[] v = new Vector2[vs.size];
        // System.out.println("Vectors");
        for (int i = 0; i < vs.size; i++) {
            // System.out.println("pre : " + vs.get(i).toString());
            v[i] = new Vector2(vs.get(i).x * PPM, vs.get(i).y  * PPM);
            v[i].sub(centre);
            // System.out.println("aft : " + v[i].toString());
        }
        System.out.println("pre");
        shape.set(v);
        System.out.println("after");

        fixtureDef.shape = shape;
        fixtureDef.density = 1;
        Body body = world.createBody(bodyDef);
        body.createFixture(fixtureDef);
        for (int i = 0; i<vs.size; i++) {
            v[i].add(centre);
        }
    }


    private Vector2 findCentroid(Array<Vector2> vs, int count) {
        Vector2 c = new Vector2();
        float area = 0.0f;
        float p1X = 0.0f;
        float p1Y = 0.0f;
        float inv3 = 1.0f / 3.0f;
        for (int i = 0; i < count; ++i) {
            Vector2 p2 = vs.get(i);
            Vector2 p3;
            if (i + 1 < count){
                p3 = vs.get(i + 1);
            }else{
                p3 = vs.get(0);
            }
            float e1X = p2.x - p1X;
            float e1Y = p2.y - p1Y;
            float e2X = p3.x - p1X;
            float e2Y = p3.y - p1Y;
            float D = (e1X * e2Y - e1Y * e2X);
            float triangleArea = 0.5f*D;
            area += triangleArea;
            c.x += triangleArea * inv3 * (p1X + p2.x + p3.x);
            c.y += triangleArea * inv3 * (p1Y + p2.y + p3.y);
        }
        c.x*=1.0/area;
        c.y*=1.0/area;
        return new Vector2(c.x * PPM, c.y * PPM);
    }

这些是我从切割中获得的顶点。但它在这些顶点上崩溃所以我做了这个代码的和平来测试它以便更容易复制。

    Array<Vector2> verts = new Array<Vector2>();
    verts.add(new Vector2(0.022f,0.015407243f));
    verts.add(new Vector2(0.0020000006f,0.015407243f));
    verts.add(new Vector2(0.022000004f,0.015f));
    verts.add(new Vector2(0.0020000006f,0.015f));
    creatBody(verts);

我搜索了错误,我能找到的是我的polygonShape很小。

0 个答案:

没有答案