Search code examples
javaandroidandenginejbox2d

Strange error when creating physics body in Andengine


I`m trying to create a polygonal physics body by such code:

        final float halfWidth = pAreaShape.getWidthScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
        final float halfHeight = pAreaShape.getHeightScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;            
        final float centerX = 0;
        final float left = -halfWidth;
        final float right = halfWidth;
        final float top = -halfHeight + 9f / PIXEL_TO_METER_RATIO_DEFAULT;
        final float bottom = halfHeight;

        final PolygonShape shape1 = new PolygonShape();

        final Vector2[] vertices = {
                new Vector2(centerX, bottom),
                new Vector2(left+8f/PIXEL_TO_METER_RATIO_DEFAULT, bottom-19f/PIXEL_TO_METER_RATIO_DEFAULT),
                new Vector2(left+7f/PIXEL_TO_METER_RATIO_DEFAULT, bottom-23f/PIXEL_TO_METER_RATIO_DEFAULT),
                new Vector2(left+10f/PIXEL_TO_METER_RATIO_DEFAULT, top+14f/PIXEL_TO_METER_RATIO_DEFAULT),
                new Vector2(left+13f/PIXEL_TO_METER_RATIO_DEFAULT, top+8f/PIXEL_TO_METER_RATIO_DEFAULT),

                new Vector2(right-14f/PIXEL_TO_METER_RATIO_DEFAULT, top+8f/PIXEL_TO_METER_RATIO_DEFAULT),
                new Vector2(right-11f/PIXEL_TO_METER_RATIO_DEFAULT, top+14f/PIXEL_TO_METER_RATIO_DEFAULT),
                new Vector2(right-9f/PIXEL_TO_METER_RATIO_DEFAULT, bottom-23f/PIXEL_TO_METER_RATIO_DEFAULT),
                new Vector2(right-10f/PIXEL_TO_METER_RATIO_DEFAULT, bottom-19f/PIXEL_TO_METER_RATIO_DEFAULT)
        };

        Body body = PhysicsFactory.createPolygonBody(pPhysicsWorld, pAreaShape, vertices, pBodyType, pFixtureDef);

Variable vertices contains vertices of this shape.

And it doesn`t work — applications starts(shows black screen and applications title bar), than it quits without any errors(there is no crash dialog and no errors in LogCat).

BUT when i delete one of the elements of vertices array, it works fine.

What i`m doing wrong?


Solution

  • Box2D has a maximum of 8 vertices in a polygon, and will fail by assertion if you specify more. I'm not sure what version of Box2D ANDEngine uses, but try making a loop or edge shape