public float[] getMeshVertices(Polygon poly) { // Top verticesMesh[0] = poly.getTransformedVertices()[0]; // go left X verticesMesh[1] = poly.getTransformedVertices()[1]; // go up Y // Right verticesMesh[4] = poly.getTransformedVertices()[2]; // go right X verticesMesh[5] = poly.getTransformedVertices()[3]; // go up Y // Bottom Right verticesMesh[8] = poly.getTransformedVertices()[4]; // go right X verticesMesh[9] = poly.getTransformedVertices()[5]; // go down Y return verticesMesh; }
/** * Clamp the point to the nearest polygon segment * * @param poly The polygon * @param x The original point X * @param y The original point Y * @param dest The clamped point * @return The segment where the clamped point belongs */ public static int getClampedPoint(Polygon poly, float x, float y, Vector2 dest) { float verts[] = poly.getTransformedVertices(); float dTmp; Intersector.nearestSegmentPoint(verts[0], verts[1], verts[2], verts[3], x, y, dest); int nearest = 0; float d = Vector2.dst(x, y, dest.x, dest.y); for (int i = 2; i < verts.length; i += 2) { Intersector.nearestSegmentPoint( verts[i], verts[i + 1], verts[(i + 2) % verts.length], verts[(i + 3) % verts.length], x, y, tmp); dTmp = Vector2.dst(x, y, tmp.x, tmp.y); if (dTmp < d) { d = dTmp; nearest = i; dest.set(tmp); } } return nearest; }
public static boolean inLineOfSight(Vector2 p1, Vector2 p2, Polygon polygon, boolean obstacle) { tmp.set(p1); tmp2.set(p2); float verts[] = polygon.getTransformedVertices(); for (int i = 0; i < verts.length; i += 2) { if (lineSegmentsCross( tmp.x, tmp.y, tmp2.x, tmp2.y, verts[i], verts[i + 1], verts[(i + 2) % verts.length], verts[(i + 3) % verts.length])) return false; } tmp.add(tmp2); tmp.x /= 2; tmp.y /= 2; boolean result = PolygonUtils.isPointInside(polygon, tmp.x, tmp.y, !obstacle); return obstacle ? !result : result; }
public static boolean deletePoint(Polygon poly, float x, float y, float tolerance) { float verts[] = poly.getTransformedVertices(); for (int i = 0; i < verts.length; i += 2) { if (Vector2.dst(x, y, verts[i], verts[i + 1]) < tolerance) { deletePoint(poly, i); return true; } } return false; }
public static boolean isPointInside( Polygon polygon, float x, float y, boolean toleranceOnOutside) { float verts[] = polygon.getTransformedVertices(); boolean inside = false; float oldX = verts[verts.length - 2]; float oldY = verts[verts.length - 1]; float oldSqDist = Vector2.dst2(oldX, oldY, x, y); for (int i = 0; i < verts.length; i += 2) { float newX = verts[i]; float newY = verts[i + 1]; float newSqDist = Vector2.dst2(newX, newY, x, y); if (oldSqDist + newSqDist + 2.0f * Math.sqrt(oldSqDist * newSqDist) - Vector2.dst2(newX, newY, oldX, oldY) < TOLERANCE_IS_POINT_INSIDE) return toleranceOnOutside; float leftX = newX; float leftY = newY; float rightX = oldX; float rightY = oldY; if (newX > oldX) { leftX = oldX; leftY = oldY; rightX = newX; rightY = newY; } if (leftX < x && x <= rightX && (y - leftY) * (rightX - leftX) < (rightY - leftY) * (x - leftX)) inside = !inside; oldX = newX; oldY = newY; oldSqDist = newSqDist; } return inside; }
public static boolean isVertexConcave(Polygon poly, int index) { float verts[] = poly.getTransformedVertices(); float currentX = verts[index]; float currentY = verts[index + 1]; float nextX = verts[(index + 2) % verts.length]; float nextY = verts[(index + 3) % verts.length]; float previousX = verts[index == 0 ? verts.length - 2 : index - 2]; float previousY = verts[index == 0 ? verts.length - 1 : index - 1]; float leftX = currentX - previousX; float leftY = currentY - previousY; float rightX = nextX - currentX; float rightY = nextY - currentY; float cross = (leftX * rightY) - (leftY * rightX); return cross < 0; }
public void drawBBoxLines(ShapeRenderer renderer) { // renderer.begin(ShapeType.Rectangle); renderer.begin(ShapeType.Line); for (BaseActor a : actors.values()) { Polygon p = a.getBBox(); if (p == null) { EngineLogger.error("ERROR DRAWING BBOX FOR: " + a.getId()); } if (a instanceof ObstacleActor) renderer.setColor(OBSTACLE_COLOR); else renderer.setColor(ACTOR_BBOX_COLOR); renderer.polygon(p.getTransformedVertices()); // Rectangle r = a.getBBox().getBoundingRectangle(); // renderer.rect(r.getX(), r.getY(), r.getWidth(), r.getHeight()); } if (polygonalNavGraph != null) { renderer.setColor(WALKZONE_COLOR); renderer.polygon(polygonalNavGraph.getWalkZone().getTransformedVertices()); // DRAW LINEs OF SIGHT renderer.setColor(Color.WHITE); ArrayList<NavNodePolygonal> nodes = polygonalNavGraph.getGraphNodes(); for (NavNodePolygonal n : nodes) { for (NavNode n2 : n.neighbors) { renderer.line(n.x, n.y, ((NavNodePolygonal) n2).x, ((NavNodePolygonal) n2).y); } } } renderer.end(); }