@Test public void testCircles() { Vector2 intersection = new Vector2(); BoundingAreaComponent boundingArea1 = new BoundingAreaComponent(); boundingArea1.set(false); BoundingAreaComponent boundingArea2 = new BoundingAreaComponent(); boundingArea2.set(false); EngineEntity circle1 = buildCircleEntity(10, 20, 100); boundingArea1.update(circle1); com.badlogic.gdx.math.Circle collisionCircle1 = BoundingAreaBuilder .getBoundingCircle(circle1).circle; assertEquals(110, collisionCircle1.x, 0.2F); assertEquals(120, collisionCircle1.y, 0.2F); assertEquals(100, collisionCircle1.radius, 0.2F); EngineEntity circle2 = buildCircleEntity(-400, 400, 200); boundingArea2.update(circle2); assertEquals(Math.sqrt(310 * 310 + 480 * 480), boundingArea1.distanceTo(boundingArea2, intersection, false), 0.1F); assertEquals(-310, intersection.x, 0.1F); assertEquals(480, intersection.y, 0.1F); assertEquals(Math.sqrt(310 * 310 + 480 * 480) - 300, boundingArea1.distanceTo(boundingArea2, intersection, true), 0.7F); EngineEntity circle3 = buildCircleEntity(-50, -50, 50); EngineEntity circle4 = buildCircleEntity(-20, -20, 20); boundingArea1.update(circle3); boundingArea2.update(circle4); assertEquals(-1, boundingArea1.distanceTo(boundingArea2, intersection, true), 0.0F); assertEquals(0, boundingArea1.distanceTo(boundingArea2, intersection, false), 0.1F); EngineEntity rect1 = buildRectEntity(-20, -20, 40, 40); EngineEntity rect2 = buildRectEntity(0, -20, 40, 40); BoundingAreaComponent r1 = new BoundingAreaComponent(); r1.set(true); r1.update(rect1); BoundingAreaComponent r2 = new BoundingAreaComponent(); r2.set(true); r2.update(rect2); assertEquals(20, r1.distanceTo(r2, intersection, true), 0.1F); EngineEntity circle5 = buildCircleEntity(-90, -50, 50); EngineEntity circle6 = buildCircleEntity(0, -50, 50); boundingArea1.update(circle5); boundingArea2.update(circle6); assertEquals(10, boundingArea1.distanceTo(boundingArea2, intersection, true), 0.2F); assertEquals(90, boundingArea1.distanceTo(boundingArea2, intersection, false), 0.0F); }
public static CircleWrapper getBoundingCircle(EngineEntity entity) { Polygon pol = getBoundingPolygon(entity); Vector2 center = Pools.obtain(Vector2.class); float maxDistance = Float.NEGATIVE_INFINITY; for (int i = 0; i < pol.getVertices().length; i += 2) { Vector2 vertex1 = Pools.obtain(Vector2.class); Vector2 vertex2 = Pools.obtain(Vector2.class); Vector2 vertex2toVertex1 = Pools.obtain(Vector2.class); vertex1.set(pol.getVertices()[i], pol.getVertices()[i + 1]); for (int j = 0; j < pol.getVertices().length - 1; j += 2) { if (i == j) { continue; } vertex2.set(pol.getVertices()[j], pol.getVertices()[j + 1]); vertex2toVertex1.set(vertex1); float distance = vertex2toVertex1.sub(vertex2).len(); if (distance > maxDistance) { maxDistance = distance; center.set(vertex2).add(vertex2toVertex1.scl(0.5f)); } } Pools.free(vertex1); Pools.free(vertex2); Pools.free(vertex2toVertex1); } Circle circle = Pools.obtain(Circle.class); circle.set(center.x, center.y, maxDistance / 2.0F); Pools.free(center); CircleWrapper circleWrapper = Pools.obtain(CircleWrapper.class); circleWrapper.set(circle); return circleWrapper; }
BoundingAreaBuilder { public static CircleWrapper getBoundingCircle(EngineEntity entity) { Polygon pol = getBoundingPolygon(entity); Vector2 center = Pools.obtain(Vector2.class); float maxDistance = Float.NEGATIVE_INFINITY; for (int i = 0; i < pol.getVertices().length; i += 2) { Vector2 vertex1 = Pools.obtain(Vector2.class); Vector2 vertex2 = Pools.obtain(Vector2.class); Vector2 vertex2toVertex1 = Pools.obtain(Vector2.class); vertex1.set(pol.getVertices()[i], pol.getVertices()[i + 1]); for (int j = 0; j < pol.getVertices().length - 1; j += 2) { if (i == j) { continue; } vertex2.set(pol.getVertices()[j], pol.getVertices()[j + 1]); vertex2toVertex1.set(vertex1); float distance = vertex2toVertex1.sub(vertex2).len(); if (distance > maxDistance) { maxDistance = distance; center.set(vertex2).add(vertex2toVertex1.scl(0.5f)); } } Pools.free(vertex1); Pools.free(vertex2); Pools.free(vertex2toVertex1); } Circle circle = Pools.obtain(Circle.class); circle.set(center.x, center.y, maxDistance / 2.0F); Pools.free(center); CircleWrapper circleWrapper = Pools.obtain(CircleWrapper.class); circleWrapper.set(circle); return circleWrapper; } }
BoundingAreaBuilder { public static CircleWrapper getBoundingCircle(EngineEntity entity) { Polygon pol = getBoundingPolygon(entity); Vector2 center = Pools.obtain(Vector2.class); float maxDistance = Float.NEGATIVE_INFINITY; for (int i = 0; i < pol.getVertices().length; i += 2) { Vector2 vertex1 = Pools.obtain(Vector2.class); Vector2 vertex2 = Pools.obtain(Vector2.class); Vector2 vertex2toVertex1 = Pools.obtain(Vector2.class); vertex1.set(pol.getVertices()[i], pol.getVertices()[i + 1]); for (int j = 0; j < pol.getVertices().length - 1; j += 2) { if (i == j) { continue; } vertex2.set(pol.getVertices()[j], pol.getVertices()[j + 1]); vertex2toVertex1.set(vertex1); float distance = vertex2toVertex1.sub(vertex2).len(); if (distance > maxDistance) { maxDistance = distance; center.set(vertex2).add(vertex2toVertex1.scl(0.5f)); } } Pools.free(vertex1); Pools.free(vertex2); Pools.free(vertex2toVertex1); } Circle circle = Pools.obtain(Circle.class); circle.set(center.x, center.y, maxDistance / 2.0F); Pools.free(center); CircleWrapper circleWrapper = Pools.obtain(CircleWrapper.class); circleWrapper.set(circle); return circleWrapper; }  }
BoundingAreaBuilder { public static CircleWrapper getBoundingCircle(EngineEntity entity) { Polygon pol = getBoundingPolygon(entity); Vector2 center = Pools.obtain(Vector2.class); float maxDistance = Float.NEGATIVE_INFINITY; for (int i = 0; i < pol.getVertices().length; i += 2) { Vector2 vertex1 = Pools.obtain(Vector2.class); Vector2 vertex2 = Pools.obtain(Vector2.class); Vector2 vertex2toVertex1 = Pools.obtain(Vector2.class); vertex1.set(pol.getVertices()[i], pol.getVertices()[i + 1]); for (int j = 0; j < pol.getVertices().length - 1; j += 2) { if (i == j) { continue; } vertex2.set(pol.getVertices()[j], pol.getVertices()[j + 1]); vertex2toVertex1.set(vertex1); float distance = vertex2toVertex1.sub(vertex2).len(); if (distance > maxDistance) { maxDistance = distance; center.set(vertex2).add(vertex2toVertex1.scl(0.5f)); } } Pools.free(vertex1); Pools.free(vertex2); Pools.free(vertex2toVertex1); } Circle circle = Pools.obtain(Circle.class); circle.set(center.x, center.y, maxDistance / 2.0F); Pools.free(center); CircleWrapper circleWrapper = Pools.obtain(CircleWrapper.class); circleWrapper.set(circle); return circleWrapper; }  static Array<Polygon> getColliders(EngineEntity entity); static void collectColliders(EntityGroup rendererActor,
			Array<Polygon> colliders); static RectangleWrapper getBoundingRectangle(EngineEntity entity); static Polygon getBoundingPolygon(EngineEntity entity); static CircleWrapper getBoundingCircle(EngineEntity entity); }
BoundingAreaBuilder { public static CircleWrapper getBoundingCircle(EngineEntity entity) { Polygon pol = getBoundingPolygon(entity); Vector2 center = Pools.obtain(Vector2.class); float maxDistance = Float.NEGATIVE_INFINITY; for (int i = 0; i < pol.getVertices().length; i += 2) { Vector2 vertex1 = Pools.obtain(Vector2.class); Vector2 vertex2 = Pools.obtain(Vector2.class); Vector2 vertex2toVertex1 = Pools.obtain(Vector2.class); vertex1.set(pol.getVertices()[i], pol.getVertices()[i + 1]); for (int j = 0; j < pol.getVertices().length - 1; j += 2) { if (i == j) { continue; } vertex2.set(pol.getVertices()[j], pol.getVertices()[j + 1]); vertex2toVertex1.set(vertex1); float distance = vertex2toVertex1.sub(vertex2).len(); if (distance > maxDistance) { maxDistance = distance; center.set(vertex2).add(vertex2toVertex1.scl(0.5f)); } } Pools.free(vertex1); Pools.free(vertex2); Pools.free(vertex2toVertex1); } Circle circle = Pools.obtain(Circle.class); circle.set(center.x, center.y, maxDistance / 2.0F); Pools.free(center); CircleWrapper circleWrapper = Pools.obtain(CircleWrapper.class); circleWrapper.set(circle); return circleWrapper; }  static Array<Polygon> getColliders(EngineEntity entity); static void collectColliders(EntityGroup rendererActor,
			Array<Polygon> colliders); static RectangleWrapper getBoundingRectangle(EngineEntity entity); static Polygon getBoundingPolygon(EngineEntity entity); static CircleWrapper getBoundingCircle(EngineEntity entity);  }