0
votes

i work with libGDX. When i am trying in Box2D to create a fixture having a polygon-shape i get following error.:

java: ./Box2D/Collision/b2Distance.h:103: const b2Vec2& b2DistanceProxy::GetVertex(int32) const: Assertion `0 <= index && index < m_count' failed.

When i don't do Box2D's world.step(), i don't get this error any more.

So i commented out everything in my WorldContactListener and i added world.step() again.

I am still getting the same Error.

When i replace the polygon-shape with a circle-shape everything works fine. So here is how i am creating my polygon-shape:

 PolygonShape shape = new PolygonShape();

 float ppm = Game.PixelsPerMeter;

 Vector2[] vertices = new Vector2[3];

 vertices[0] = new Vector2(0f/ppm  , 0f  );
 vertices[1] = new Vector2(1/ppm , 1f/ppm  );
 vertices[2] = new Vector2(0f/ppm ,1f/ppm);

 shape.set(vertices);

And here is how i am adding everything it the Box2D world:

    float ppm = Game.PixelsPerMeter

    BodyDef bdef = new BodyDef();
    bdef.position.set(100/ ppm,  200/ ppm);
    bdef.type = BodyDef.BodyType.DynamicBody;

    b2dbody = world.createBody(bdef);

    FixtureDef mainFdef = new FixtureDef();


    mainFdef.shape = Shape; //this is the shape from above of course
    b2dbody.createFixture(mainFdef).setUserData(this);

I would be really happy if you could tell me whats wrong!

Thank You

1
What is the value of ppm?Jim
@IronMonkey it is 75strammermax
Try shape.set(vertices, vertices.length);Jim
@IronMonkey thank you, but when i do that it throws following error :Error:(382, 17) Gradle: error: no suitable method found for set(Vector2[],int) method PolygonShape.set(Vector2[]) is not applicable (actual and formal argument lists differ in length) method PolygonShape.set(float[]) is not applicable (actual and formal argument lists differ in length) method PolygonShape.set(float[],int,int) is not applicable (actual and formal argument lists differ in length) . There is no method that takes these two parameters.strammermax

1 Answers

1
votes

More of a wild guess, but do you have your ppm conversion going the right way? 1/ppm (which you indicate is 75) gives quite a small value. I didn't dig into the bowels of the box2d code, but as it works best when objects are defined in meters, creating a polygon with vertices 0,0 and 0,0.0133 (1cm) might "confuse it" (meaning some kind of rounding error or something somewhere in the code so it can't distinguish vertices and thinks there are not at least 3 of them.)

For example, a bare bones app with 3 versions of your vertices code yields runtime exceptions on the first 2 versions (small values) but no runtime exception with larger values:

/*  Version 1 (your code) - Runtime error
vertices[0] = new Vector2(0f/ppm  , 0f  );
vertices[1] = new Vector2(1f/ppm , 1f/ppm  );
vertices[2] = new Vector2(0f/ppm ,1f/ppm);
*/

/* Version 2 (your actual values) - Runtime error
vertices[0] = new Vector2(0f  , 0f  );
vertices[1] = new Vector2(0.0133f , .0133f  );
vertices[2] = new Vector2(0f , 0.0133f);
*/

/* Version 3 (larger values) - No error
vertices[0] = new Vector2(0f  , 0f  );
vertices[1] = new Vector2(1f , 1f  );
vertices[2] = new Vector2(0f ,1f);
*/