1
\$\begingroup\$

So here's the deal, i'm parsing a file that contains the vertices for a polygon, that i want to create in box2d. I create a new PolygonShape() and then call .set() giving it a defined array of Vec, and that defined array's .length property. I expected this to work, since the documentation for jbox2d says this method takes a Vec array, and the count of Vec objects in that array. However, it errors out, and it seems to be unrelated to my code. The error i get is

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at org.jbox2d.collision.shapes.PolygonShape.set(PolygonShape.java:174)
and, upon looking at that line in the jbox2d svn repository, i still cannot figure out the issue.

Any help is appreciated!

\$\endgroup\$
2
  • \$\begingroup\$ Looking at line 174: int[] hull = (intPool != null) ? intPool.get(Settings.maxPolygonVertices) : new int[Settings.maxPolygonVertices]; It looks like the problem is that intPool is not null and its size is less than maxPolygonVertices. That intPool is null if called from public final void set(final Vec2[] vertices, final int count) - are you calling the public final void set(final Vec2[] verts, final int num, final Vec2Array vecPool, final IntArray intPool) version and if so what value is being sent in for intPool? \$\endgroup\$ Dec 9 '12 at 2:51
  • \$\begingroup\$ i don't believe i'm using that 2nd set method. I appear to be using the first. I'm doing the following PolygonShape pshape = new PolygonShape(); pshape.set(vertices, vertices.length); b.createFixture(pshape, 0); where b is a properly created (using World.createBody(BodyDef)) body. where vertices is a properly filled and sized Vec[]. \$\endgroup\$ Dec 9 '12 at 20:16
1
\$\begingroup\$

Your problem is that you are trying to create a PolygonShape with more than 8 vertices. Box2D, and thus JBox2D, default to a maximum of 8 vertices per polygon. This is why you don't get an ArrayIndexOutOfBoundsException until index 8 (0 based indexing means it's vertex #9).

You can change the maximum number of vertices by simply modifying org.jbox2d.common.Settings.maxPolygonVertices.

However, if you take a look how PolyShape utilizes this setting you'll see why changes can have a global performance impact.

In this conversation iforce2d suggests you can increase the limit a little, if you need, but should consider alternatives before making big changes.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .