Java Code Examples for com.badlogic.gdx.math.Polygon#getTransformedVertices()

The following examples show how to use com.badlogic.gdx.math.Polygon#getTransformedVertices() . 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 vote down vote up
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 vote down vote up
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 3
Source File: PolygonalNavGraph.java    From bladecoder-adventure-engine with Apache License 2.0 6 votes vote down vote up
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 4
Source File: GameTrackDebugRenderer.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
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 5
Source File: PolygonUtils.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
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 6
Source File: PolygonalNavGraph.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
/**
 * 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 7
Source File: PolygonalNavGraph.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
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 8
Source File: CanvasDrawer.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
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 9
Source File: PolygonUtils.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
		}
	}
	
	// ERROR CONTROL:
	// If the clamped point is not in the walkzone 
	// we search for the nearest walkzone vertex
	if (!PolygonUtils.isPointInside(poly, dest.x, dest.y, true)) {
		EngineLogger.debug("> PolygonalPathFinder: CLAMPED FAILED!!");
		
		tmp.set(verts[0], verts[1]);
		d = Vector2.dst(x, y, tmp.x, tmp.y);
		nearest = 0;
		dest.set(tmp);
		
		for (int i = 2; i < verts.length; i += 2) {
			tmp.set(verts[i], verts[i + 1]);
			dTmp = Vector2.dst(x, y, tmp.x, tmp.y);

			if (dTmp < d) {
				d = dTmp;
				nearest = i;
				dest.set(tmp);
			}
		}
	}

	return nearest;
}
 
Example 10
Source File: PolygonUtils.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
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 11
Source File: PolygonalNavGraph.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
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 12
Source File: CanvasDrawer.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
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();
}