Question

I get an unexpected ArrayIndexOutOfBoundsException with this code; can anyone help?

I create two polygons like this:

float[]vertice={.1f, 2.7f, .4f, 4.3f, 3.4f, 5.3f, 5.6f, 3.3f, 3.3f, .1f};
Polygon oPolygon1=new Polygon(vertice);

float[]vertice2={.2f,1.3f,1.9f,4.5f,4.1f,1.3f};
Polygon oPolygon2=new Polygon(vertice2);

And update their positions with:

oPolygon1.setPosition(x1,y1);
oPolygon2.setPosition(x2,y2);

But when I try to use Intersector to see if they overlap...

if(Intersector.overlapConvexPolygons( oPolygon1, oPolygon2)){
   //do something
}

... I get the following error:

Exception in thread "LWJGL Application" java.lang.ArrayIndexOutOfBoundsException:

In this block of code within the Intersector:

// projection axis is perpendicular to potential separation axis edge i->j
float projX = verts1[j + 1] - verts1[i + 1];
float projY = verts1[i] - verts1[j];
Was it helpful?

Solution

This appears to be a bug in LibGDX. In the line setting projX, it should wrap the index

float projX = verts1[(j + 1) % length1] - verts1[i + 1];

I'll get this fixed in the SVN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top