2

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?

ThomasW
  • 16,981
  • 4
  • 79
  • 106
Ashot
  • 640
  • 1
  • 6
  • 14

1 Answers1

3

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

Daniel Murphy
  • 852
  • 7
  • 14
  • yes, you`re right. but i`m still wondering, why application crashes without any error report. Thanks anyway – Ashot Apr 18 '12 at 20:53
  • possible because it failed by assertion, which usually crashes the process. If assertions weren't enabled for the native build, then it would have failed by illegal memory location probably. Basically, it probably was a hard crash – Daniel Murphy May 01 '12 at 02:15