OutOfBounds Exception when creating a PolygonShape using jbox2d

24 Views Asked by At

I'm trying to create a body with a fixture in jbox2d TestBed, the fixture has 9 vertices. When I try to run the test I get the following error:

Exception in thread "Testbed" java.lang.ArrayIndexOutOfBoundsException: 8
    at org.jbox2d.collision.Distance$DistanceProxy.set(Distance.java:510)
    at org.jbox2d.dynamics.World.solveTOI(World.java:1326)
    at org.jbox2d.dynamics.World.step(World.java:642)
    at org.jbox2d.testbed.framework.TestbedTest.step(TestbedTest.java:447)
    at org.jbox2d.testbed.tests.FixtureTest.step(FixtureTest.java:34)
    at org.jbox2d.testbed.framework.AbstractTestbedController.updateTest(AbstractTestbedController.java:274)
    at org.jbox2d.testbed.framework.AbstractTestbedController.render(AbstractTestbedController.java:406)
    at org.jbox2d.testbed.framework.AbstractTestbedController.stepAndRender(AbstractTestbedController.java:392)
    at org.jbox2d.testbed.framework.TestbedController.stepAndRender(TestbedController.java:68)
    at org.jbox2d.testbed.framework.TestbedController.run(TestbedController.java:62)
    at java.lang.Thread.run(Thread.java:745)

This error only occurs when I change the limit of vertices. If I don't change it, when I run the test, I get a fixture with only 8 vertices, the last vertex of the array is ignored. enter image description here

Here is what I am doing for creating the fixture, I can't figure out what I'm doing wrong:

        Settings.maxPolygonVertices = 20;

        BodyDef myBodyDef = new BodyDef();
        myBodyDef2.type = BodyType.DYNAMIC;
        myBodyDef2.position.set(new Vec2(2,0.925F));
        
        Body egg = m_world.createBody(myBodyDef);        
        
        vertices = new Vec2[9];
        vertices[0] = new Vec2(-0.1f,0.4f);
        vertices[1] = new Vec2(-0.2f,0.3f);
        vertices[2] = new Vec2(-0.3f,0.1f);
        vertices[3] = new Vec2(-0.3f,-0.1f);
        vertices[4] = new Vec2(-0.2f,-0.3f);
        vertices[5] = new Vec2(0.1f,-0.3f);
        vertices[6] = new Vec2(0.2f,-0.1f);
        vertices[7] = new Vec2(0.2f,0.1f);
        vertices[8] = new Vec2(0.1f,0.3f);

        PolygonShape myPolygonShape = new PolygonShape();
        myPolygonShape.set(vertices, 9);

        FixtureDef myFixtureDef = new FixtureDef();
        myFixtureDef.shape = myPolygonShape;
        myFixtureDef.restitution = 0.4f;
        myFixtureDef.density = 1;
        myFixtureDef.friction = 1f;
        egg.createFixture(myFixtureDef);
0

There are 0 best solutions below