The other answer is basically correct; however, I had some problems with positioning polygons using this method. Just a clarification:
LibGDX does not support rotating rectangles when using Intersector to detect conflicts. If you need rotating rectangles, you should use Polygon to detect conflicts.
Building a rectangular polygon:
polygon = new Polygon(new float[]{0,0,bounds.width,0,bounds.width,bounds.height,0,bounds.height});
Remember to set the source of the polygon if you are going to rotate it:
polygon.setOrigin(bounds.width/2, bounds.height/2);
Now you can rotate the collision polygon:
polygon.setRotation(degrees);
In addition, somewhere in your code, you most likely want to update the position of the collision polygon in accordance with your sprite:
polygon.setPosition(x, y);
We can even draw our polygon on the screen (for debugging purposes):
drawDebug(ShapeRenderer shapeRenderer) { shapeRenderer.begin(ShapeRenderer.ShapeType.Line); shapeRenderer.polygon(polygon.getTransformedVertices()); shapeRenderer.end(); }
Collision Detection:
ConvexPolygons () Intersector Overlays:
boolean collision = Intersector.overlapConvexPolygons(polygon1, polygon2)
As mentioned in another answer, this method only works if:
- using convex polygons whose rectangle
- polygon execution for polygons, for example: you cannot mix rectangles and polygons