Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
    
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? –  Ryan Maloney Dec 9 '12 at 2:51
    
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[]. –  B3nGr33ni3r Dec 9 '12 at 20:16

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.