0
votes

Im trying to cut bodys in my world. My code is based off this. But when the new polygon shape is very small i get this error.

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

Below is my code to create the new body

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

And these are the vertices i got from cutting. But it crashes on these vertices So i made this peace of code to test it for easier replication.

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

I searched for the error and what i could find is that my polygonShape is to small.

1
Correct, it is too small. Make it bigger.LearnCocos2D
@LearnCocos2D is it possible to change something that it would allow small shapes? here. It is possible to make realy small shapes. (I know its not in java).Tom
No, it's not. That limit is set for a reason.iforce2d

1 Answers

0
votes

Index: Box2D/Box2D/Collision/Shapes/b2PolygonShape.cpp

open b2PolygonShape.cpp and apply this patch

//if (b2DistanceSquared(v, ps[j]) < 0.5f * b2_linearSlop)   
if (b2DistanceSquared(v, ps[j]) < ((0.5f * b2_linearSlop) * (0.5f * b2_linearSlop)))