List of usage examples for com.badlogic.gdx.utils FloatArray removeRange
public void removeRange(int start, int end)
From source file:es.eucm.ead.engine.collision.BoundingAreaBuilder.java
License:Open Source License
/** * Creates a minimum convex polygon that contains the given entity. The * algorithm takes into account renderers' colliders (if available), and * children. The algorithm gets different polygons coming from renderers, * children, etc. and uses {@link ConvexHull} to determine the minimum * convex polygon that contains all of them. * /* w w w . j a va 2 s. c o m*/ * @param entity * The entity to build a bounding polygon for. * @return The bounding convex polygon, in coordinates of the * {@link Layer#SCENE_CONTENT} layer. May return {@code null} if * this entity is not a descendant of {@link Layer#SCENE_CONTENT}. */ static Polygon getBoundingPolygon(EngineEntity entity) { Array<Vector2> points = new Array<Vector2>(); int count = 0; Group sceneContentGroup = getSceneContentAncestor(entity).getGroup(); if (sceneContentGroup == null) { return null; } Polygon polygon = getBoundingPolygon(entity, sceneContentGroup, entity.getGroup()); if (polygon != null) { toVector2Array(polygon.getVertices(), points); count++; } for (Actor actor : entity.getGroup().getChildren()) { if (actor.getUserObject() != null && actor.getUserObject() instanceof EngineEntity) { EngineEntity child = (EngineEntity) actor.getUserObject(); Polygon childPolygon = getBoundingPolygon(child); toVector2Array(childPolygon.getVertices(), points); count++; } } polygon = new Polygon(); if (count > 1) { FloatArray polygonPoints = convexHull.computePolygon(toSimpleArray(points), false); // Remove last point (duplicate) polygonPoints.removeRange(polygonPoints.size - 2, polygonPoints.size - 1); polygon.setVertices(polygonPoints.toArray()); } else { polygon.setVertices(toSimpleArray(points)); } for (Vector2 point : points) { Pools.free(point); } return polygon; }
From source file:es.eucm.ead.engine.collision.BoundingAreaBuilder.java
License:Open Source License
private static Polygon getBoundingPolygon(EngineEntity entity, Group sceneContentGroup, Group group) { SnapshotArray<Vector2> allPoints = new SnapshotArray<Vector2>(); for (Polygon polygon : getColliders(entity)) { for (int i = 0; i < polygon.getVertices().length; i += 2) { Vector2 tmp = Pools.obtain(Vector2.class); tmp.set(polygon.getVertices()[i], polygon.getVertices()[i + 1]); group.localToAscendantCoordinates(sceneContentGroup, tmp); allPoints.add(tmp);//from ww w. j a v a2 s .c om } } if (allPoints.size == 0) { return null; } // Remove duplicates, if any. Algorithm works better this way Object[] pointsToIterate = allPoints.begin(); int size = allPoints.size; for (int i = 0; i < size; i++) { Vector2 pointA = (Vector2) pointsToIterate[i]; for (int j = 0; j < size; j++) { Vector2 pointB = (Vector2) pointsToIterate[j]; if (j != i && pointA.equals(pointB)) { allPoints.removeValue(pointB, true); } } } allPoints.end(); // To array float[] points = toSimpleArray(allPoints); FloatArray floatArray = convexHull.computePolygon(points, false); // Remove the last point, since its the first one (duplicate) floatArray.removeRange(floatArray.size - 2, floatArray.size - 1); Polygon polygon = new Polygon(); polygon.setVertices(floatArray.toArray()); return polygon; }