com.badlogic.gdx.math.Polygon Java Examples
The following examples show how to use
com.badlogic.gdx.math.Polygon.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PolygonUtils.java From bladecoder-adventure-engine with Apache License 2.0 | 6 votes |
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; }
Example #2
Source File: PolygonUtils.java From bladecoder-adventure-engine with Apache License 2.0 | 6 votes |
public static void addPoint(Polygon poly, float x, float y, int index) { float verts[] = poly.getVertices(); x -= poly.getX(); y -= poly.getY(); int length = verts.length; float destination[] = new float[length + 2]; System.arraycopy(verts, 0, destination, 0, index); destination[index] = x; destination[index + 1] = y; System.arraycopy(verts, index, destination, index + 2, length - index); poly.setVertices(destination); }
Example #3
Source File: PolygonUtils.java From bladecoder-adventure-engine with Apache License 2.0 | 6 votes |
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; }
Example #4
Source File: PolygonalNavGraph.java From bladecoder-adventure-engine with Apache License 2.0 | 6 votes |
private boolean inLineOfSight(float p1X, float p1Y, float p2X, float p2Y) { tmp.set(p1X, p1Y); tmp2.set(p2X, p2Y); if (!PolygonUtils.inLineOfSight(tmp, tmp2, walkZone, false)) { return false; } for (Polygon o : obstacles) { if (!PolygonUtils.inLineOfSight(tmp, tmp2, o, true)) { return false; } } return true; }
Example #5
Source File: PolygonalNavGraph.java From bladecoder-adventure-engine with Apache License 2.0 | 6 votes |
private void addObstacleToGrapth(Polygon poly) { float verts[] = poly.getTransformedVertices(); for (int i = 0; i < verts.length; i += 2) { if (PolygonUtils.isVertexConcave(poly, i) && PolygonUtils.isPointInside(walkZone, verts[i], verts[i + 1], false)) { NavNodePolygonal n1 = new NavNodePolygonal(verts[i], verts[i + 1]); for (int j = 0; j < graphNodes.size(); j++) { NavNodePolygonal n2 = graphNodes.get(j); if (inLineOfSight(n1.x, n1.y, n2.x, n2.y)) { n1.neighbors.add(n2); n2.neighbors.add(n1); } } graphNodes.add(n1); } } }
Example #6
Source File: CanvasDrawer.java From bladecoder-adventure-engine with Apache License 2.0 | 5 votes |
public void drawPolygonVertices(Polygon p, Color c) { float verts[] = p.getTransformedVertices(); drawer.setProjectionMatrix(camera.combined); drawer.setTransformMatrix(new Matrix4()); drawer.begin(ShapeRenderer.ShapeType.Line); drawer.setColor(c); for (int i = 0; i < verts.length; i += 2) drawer.rect(verts[i] - CORNER_DIST / 2, verts[i + 1] - CORNER_DIST / 2, CORNER_DIST, CORNER_DIST); drawer.end(); }
Example #7
Source File: PolygonUtils.java From bladecoder-adventure-engine with Apache License 2.0 | 5 votes |
public static boolean addClampPointIfTolerance(Polygon poly, float x, float y, float tolerance) { boolean added = false; int i = getClampedPoint(poly, x, y, tmp2); if (tmp2.dst(x, y) < tolerance) { added = true; addPoint(poly, tmp2.x, tmp2.y, i + 2); } return added; }
Example #8
Source File: PolygonUtils.java From bladecoder-adventure-engine with Apache License 2.0 | 5 votes |
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; }
Example #9
Source File: Param.java From bladecoder-adventure-engine with Apache License 2.0 | 5 votes |
public static String toStringParam(Polygon p) { StringBuilder sb = new StringBuilder(); float[] verts = p.getVertices(); sb.append(verts[0]); for (int i = 1; i < verts.length; i++) { sb.append(NUMBER_PARAM_SEPARATOR); sb.append(verts[i]); } return sb.toString(); }
Example #10
Source File: PolygonalNavGraph.java From bladecoder-adventure-engine with Apache License 2.0 | 5 votes |
/** * Search the first polygon vertex inside the walkzone. * * @param p the polygon * @param target the vertex found */ private void getFirstVertexInsideWalkzone(Polygon p, Vector2 target) { float verts[] = p.getTransformedVertices(); for (int i = 0; i < verts.length; i += 2) { if (PolygonUtils.isPointInside(walkZone, verts[i], verts[i + 1], true)) { target.x = verts[i]; target.y = verts[i + 1]; return; } } }
Example #11
Source File: PolygonalNavGraph.java From bladecoder-adventure-engine with Apache License 2.0 | 5 votes |
public void addDinamicObstacle(Polygon poly) { int idx = obstacles.indexOf(poly); // CHECK TO AVOID ADDING THE ACTOR SEVERAL TIMES if (idx == -1 && walkZone != null) { obstacles.add(poly); addObstacleToGrapth(poly); } }
Example #12
Source File: PolygonalNavGraph.java From bladecoder-adventure-engine with Apache License 2.0 | 5 votes |
public boolean removeDinamicObstacle(Polygon poly) { boolean exists = obstacles.remove(poly); if (!exists) return false; float verts[] = poly.getTransformedVertices(); for (int i = 0; i < verts.length; i += 2) { if (PolygonUtils.isVertexConcave(poly, i) && PolygonUtils.isPointInside(walkZone, verts[i], verts[i + 1], false)) { for (int j = 0; j < graphNodes.size(); j++) { NavNodePolygonal n = graphNodes.get(j); if (n.x == verts[i] && n.y == verts[i + 1]) { graphNodes.remove(n); j--; for (NavNodePolygonal n2 : graphNodes) { n2.neighbors.removeValue(n, true); } } } } } return true; }
Example #13
Source File: GameTrack.java From uracer-kotd with Apache License 2.0 | 5 votes |
public TrackSector (Polygon poly, float length, float relativeTotalLength, Vector2 leading, Vector2 trailing) { this.poly = poly; this.length = length; this.relativeTotal = relativeTotalLength; this.leading = leading; this.trailing = trailing; this.nLeading = new Vector2(); this.nLeading.set(leading).sub(trailing).nor(); this.nTrailing = new Vector2(); this.nTrailing.set(trailing).sub(leading).nor(); }
Example #14
Source File: GameTrack.java From uracer-kotd with Apache License 2.0 | 5 votes |
private int findSector (Vector2 a) { for (int i = 0; i < sectors.length; i++) { TrackSector s = sectors[i]; Polygon p = s.poly; if (p.contains(a.x, a.y)) { return i; } } return -1; }
Example #15
Source File: GameTrack.java From uracer-kotd with Apache License 2.0 | 5 votes |
private int findPolygon (Vector2 a, Vector2 b) { for (int i = 0; i < polys.size(); i++) { Polygon p = polys.get(i); // mitigate errors for small differences p.scale(0.1f); if (p.contains(a.x, a.y) && p.contains(b.x, b.y)) { p.scale(-0.1f); // restored return i; } p.scale(-0.1f); // restored } return -1; }
Example #16
Source File: GameTrack.java From uracer-kotd with Apache License 2.0 | 5 votes |
public GameTrack (final List<Vector2> route, final List<Polygon> trackPoly) { this.route = route; this.polys = trackPoly; this.sectors = new TrackSector[route.size()]; totalLength = sectorize(); oneOnTotalLength = 1f / totalLength; Gdx.app.log("GameTrack", "total length = " + totalLength); }
Example #17
Source File: GameTrackDebugRenderer.java From uracer-kotd with Apache License 2.0 | 5 votes |
private void drawSector (TrackSector sector) { Polygon p = sector.poly; float[] vertices = p.getTransformedVertices(); shape.line(vertices[0], vertices[1], vertices[2], vertices[3]); shape.line(vertices[2], vertices[3], vertices[4], vertices[5]); shape.line(vertices[4], vertices[5], vertices[6], vertices[7]); shape.line(vertices[6], vertices[7], vertices[0], vertices[1]); }
Example #18
Source File: ScnWidgetInputListener.java From bladecoder-adventure-engine with Apache License 2.0 | 5 votes |
@Override public void clicked(InputEvent event, float x, float y) { Scene scn = scnWidget.getScene(); if (scn == null) return; Vector2 p = new Vector2(Gdx.input.getX(), Gdx.input.getY()); scnWidget.screenToWorldCoords(p); // DOUBLE CLICK TO CREATE OR DELETE POINTS if (getTapCount() == 2 && scnWidget.getSelectedActor() != null) { Polygon poly = scnWidget.getSelectedActor().getBBox(); if ((!(scnWidget.getSelectedActor() instanceof SpriteActor) || !((SpriteActor) scnWidget.getSelectedActor()).isBboxFromRenderer()) && !(scnWidget.getSelectedActor() instanceof AnchorActor)) { if (UIUtils.ctrl()) { // Delete the point if selected boolean deleted = PolygonUtils.deletePoint(poly, p.x, p.y, CanvasDrawer.CORNER_DIST); if (deleted) { Ctx.project.setModified(); return; } } else { boolean created = PolygonUtils.addClampPointIfTolerance(poly, p.x, p.y, CanvasDrawer.CORNER_DIST); if (created) { Ctx.project.setModified(); return; } } } } }
Example #19
Source File: PolygonalNavGraph.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
public void setWalkZone(Polygon walkZone) { this.walkZone = walkZone; }
Example #20
Source File: PolygonalNavGraph.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
public Polygon getWalkZone() { return walkZone; }
Example #21
Source File: PolygonalNavGraph.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
public void createInitialGraph(BaseActor wz, Collection<BaseActor> actors) { graphNodes.clear(); if (wz == null) { walkZone = null; return; } walkZone = wz.getBBox(); // 1.- Add WalkZone convex nodes float verts[] = walkZone.getTransformedVertices(); for (int i = 0; i < verts.length; i += 2) { if (!PolygonUtils.isVertexConcave(walkZone, i)) { graphNodes.add(new NavNodePolygonal(verts[i], verts[i + 1])); } } // 2.- Add obstacles concave nodes obstacles.clear(); for (BaseActor a : actors) { if (a instanceof ObstacleActor && a.isVisible()) obstacles.add(a.getBBox()); } for (Polygon o : obstacles) { verts = o.getTransformedVertices(); for (int i = 0; i < verts.length; i += 2) { if (PolygonUtils.isVertexConcave(o, i) && PolygonUtils.isPointInside(walkZone, verts[i], verts[i + 1], false)) { graphNodes.add(new NavNodePolygonal(verts[i], verts[i + 1])); } } } // 3.- CALC LINE OF SIGHTs for (int i = 0; i < graphNodes.size() - 1; i++) { NavNodePolygonal n1 = graphNodes.get(i); for (int j = i + 1; j < graphNodes.size(); j++) { NavNodePolygonal n2 = graphNodes.get(j); if (inLineOfSight(n1.x, n1.y, n2.x, n2.y)) { n1.neighbors.add(n2); n2.neighbors.add(n1); } } } }
Example #22
Source File: PolygonalNavGraph.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
public ArrayList<Vector2> findPath(float sx, float sy, float tx, float ty) { final NavPathPolygonal resultPath = new NavPathPolygonal(); Vector2 source = new Vector2(sx, sy); Vector2 target = new Vector2(tx, ty); // 1. First verify if both the start and target points of the path are // inside the polygon. If the end point is outside the polygon clamp it // back inside. if (!PolygonUtils.isPointInside(walkZone, sx, sy, true)) { EngineLogger.debug("PolygonalPathFinder: Source not in polygon!"); PolygonUtils.getClampedPoint(walkZone, sx, sy, source); if (!PolygonUtils.isPointInside(walkZone, source.x, source.y, true)) { EngineLogger.debug("PolygonalPathFinder: CLAMPED FAILED!!"); return resultPath.getPath(); } } if (!PolygonUtils.isPointInside(walkZone, tx, ty, true)) { PolygonUtils.getClampedPoint(walkZone, tx, ty, target); if (!PolygonUtils.isPointInside(walkZone, target.x, target.y, true)) { EngineLogger.debug("PolygonalPathFinder: CLAMPED FAILED!!"); return resultPath.getPath(); } } for (Polygon o : obstacles) { if (PolygonUtils.isPointInside(o, target.x, target.y, false)) { PolygonUtils.getClampedPoint(o, target.x, target.y, target); // If the clamped point is not in the walkzone // we search for the first vertex inside if (!PolygonUtils.isPointInside(walkZone, target.x, target.y, true)) { getFirstVertexInsideWalkzone(o, target); // We exit after processing the first polygon with the point // inside. // Overlaped obstacles are not supported break; } } } // 2. Then start by checking if both points are in line-of-sight. If // they are, there’s no need for pathfinding, just walk there! if (inLineOfSight(source.x, source.y, target.x, target.y)) { EngineLogger.debug("PolygonalPathFinder: Direct path found"); resultPath.getPath().add(source); resultPath.getPath().add(target); return resultPath.getPath(); } // 3. Otherwise, add the start and end points of your path as new // temporary nodes to the graph. // AND Connect them to every other node that they can see on the graph. addStartEndNodes(source.x, source.y, target.x, target.y); // 5. Run your A* implementation on the graph to get your path. This // path is guaranteed to be as direct as possible! pathfinder.findPath(null, startNode, targetNode, resultPath); return resultPath.getPath(); }
Example #23
Source File: PolygonUtils.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
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; }
Example #24
Source File: ScnWidgetInputListener.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
@Override public boolean keyDown(InputEvent event, int keycode) { super.keyDown(event, keycode); Polygon p = null; if (scnWidget.getStage() == null || scnWidget.getStage().getKeyboardFocus() != scnWidget) return false; switch (keycode) { case Keys.ENTER: break; case Keys.BACKSPACE: break; case Keys.Z: if (UIUtils.ctrl()) { Ctx.project.getUndoStack().undo(); } break; case Keys.FORWARD_DEL: BaseActor a = Ctx.project.getSelectedActor(); if (a == null) return false; Ctx.project.getUndoStack().add(new UndoDeleteActor(Ctx.project.getSelectedScene(), a)); Ctx.project.getSelectedScene().removeActor(a); Ctx.project.setModified(this, Project.NOTIFY_ELEMENT_DELETED, null, a); break; case Keys.S: if (UIUtils.ctrl()) { try { Ctx.project.saveProject(); } catch (IOException e1) { String msg = "Something went wrong while saving the actor.\n\n" + e1.getClass().getSimpleName() + " - " + e1.getMessage(); Message.showMsgDialog(scnWidget.getStage(), "Error", msg); EditorLogger.printStackTrace(e1); } } break; case Keys.UP: case Keys.DOWN: case Keys.LEFT: case Keys.RIGHT: selActor = scnWidget.getSelectedActor(); p = selActor.getBBox(); undoOrg.set(p.getX(), p.getY()); Ctx.project.setModified(this, Project.POSITION_PROPERTY, null, selActor); break; case Keys.PLUS: case 70: scnWidget.zoom(-1); break; case Keys.MINUS: scnWidget.zoom(1); break; } return false; }
Example #25
Source File: CanvasDrawer.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
public void drawBBoxActors(Scene scn, boolean showSpriteBounds) { drawer.setProjectionMatrix(camera.combined); drawer.setTransformMatrix(new Matrix4()); drawer.begin(ShapeType.Line); for (BaseActor a : scn.getActors().values()) { Polygon p = a.getBBox(); if (p == null) { EditorLogger.error("ERROR DRAWING BBOX FOR: " + a.getId()); } // Rectangle r = a.getBBox().getBoundingRectangle(); if (a instanceof ObstacleActor) { drawer.setColor(Scene.OBSTACLE_COLOR); drawer.polygon(p.getTransformedVertices()); } else if (a instanceof InteractiveActor) { InteractiveActor iActor = (InteractiveActor) a; if(a instanceof SpriteActor && !showSpriteBounds) continue; if (!scn.getLayer(iActor.getLayer()).isVisible()) continue; drawer.setColor(Scene.ACTOR_BBOX_COLOR); if (p.getTransformedVertices().length > 2) drawer.polygon(p.getTransformedVertices()); } else if (a instanceof WalkZoneActor) { drawer.setColor(Scene.WALKZONE_COLOR); if (p.getTransformedVertices().length > 2) drawer.polygon(p.getTransformedVertices()); } else if (a instanceof AnchorActor) { drawer.setColor(Scene.ANCHOR_COLOR); drawer.line(p.getX() - Scene.ANCHOR_RADIUS, p.getY(), p.getX() + Scene.ANCHOR_RADIUS, p.getY()); drawer.line(p.getX(), p.getY() - Scene.ANCHOR_RADIUS, p.getX(), p.getY() + Scene.ANCHOR_RADIUS); } // drawer.rect(r.getX(), r.getY(), r.getWidth(), r.getHeight()); } drawer.end(); }
Example #26
Source File: RetroSceneScreen.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
private void drawHotspots(SpriteBatch batch) { final World world = ui.getWorld(); for (BaseActor a : world.getCurrentScene().getActors().values()) { if (!(a instanceof InteractiveActor) || !a.isVisible() || a == world.getCurrentScene().getPlayer()) continue; InteractiveActor ia = (InteractiveActor) a; if (!ia.canInteract()) continue; Polygon p = a.getBBox(); if (p == null) { EngineLogger.error("ERROR DRAWING HOTSPOT FOR: " + a.getId()); } Rectangle r = a.getBBox().getBoundingRectangle(); unprojectTmp.set(r.getX() + r.getWidth() / 2, r.getY() + r.getHeight() / 2, 0); world.getSceneCamera().scene2screen(worldViewport, unprojectTmp); if (world.getInventory().isVisible()) { // unprojectTmp.y += verbUI.getHeight(); } if (ia.getDesc() == null) { float size = DPIUtils.ICON_SIZE * DPIUtils.getSizeMultiplier(); Drawable drawable = ((TextureRegionDrawable) getUI().getSkin().getDrawable("circle")).tint(Color.RED); drawable.draw(batch, unprojectTmp.x - size / 2, unprojectTmp.y - size / 2, size, size); } else { BitmapFont font = getUI().getSkin().getFont("desc"); String desc = ia.getDesc(); if (desc.charAt(0) == I18N.PREFIX) desc = getWorld().getI18N().getString(desc.substring(1)); textLayout.setText(font, desc); float textX = unprojectTmp.x - textLayout.width / 2; float textY = unprojectTmp.y + textLayout.height; RectangleRenderer.draw(batch, textX - 8, textY - textLayout.height - 8, textLayout.width + 16, textLayout.height + 16, Color.BLACK); font.draw(batch, textLayout, textX, textY); } } }
Example #27
Source File: Param.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
public static void parsePolygon(Polygon p, String v, String pos) { parsePolygon(p, v); Vector2 v2 = parseVector2(pos); p.setPosition(v2.x, v2.y); }
Example #28
Source File: Param.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
public static void parsePolygon(Polygon p, String s) { String[] vs = s.split(NUMBER_PARAM_SEPARATOR); if (vs.length < 6) return; float verts[] = new float[vs.length]; for (int i = 0; i < vs.length; i++) { verts[i] = Float.parseFloat(vs[i]); } p.setVertices(verts); }
Example #29
Source File: ParticleRenderer.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
@Override public void updateBboxFromRenderer(Polygon bbox) { this.bbox = bbox; computeBbox(); }
Example #30
Source File: AnimationRenderer.java From bladecoder-adventure-engine with Apache License 2.0 | 4 votes |
@Override public void updateBboxFromRenderer(Polygon bbox) { this.bbox = bbox; computeBbox(); }