Waypoints are put there by hand. You can see it in this file:
http://blissfulthinking.nl/levelzero.svg (EDIT: try "save as")
Look for a layer called "Waypoints", it contains rectangular shapes which are parsed into Waypoint object which end up in a ArrayList in my custom World class.
This is the Waypoint class:
Code:
import java.util.ArrayList;
import nl.blissfulthinking.java.gamezero.Main;
import org.jbox2d.common.Vec2;
/**
* Waypoints have a world position and have references to all Waypoints visible from their position.
* @author Michiel
*
*/
public class Waypoint {
/**
* The Waypoint's (static) world position
*/
public final Vec2 position;
/**
* The Waypoint's neighbours, i.e. the Waypoints visible from its location
*/
private final ArrayList<Waypoint> neighbours = new ArrayList<Waypoint>();
/**
* Constructor, makes a new Waypoint object
* @param position The world position of this Waypoint
*/
public Waypoint(Vec2 position) {
this.position = position;
}
/**
* Debug rendering of this Waypoint. It displays the Waypoint and draws a line to all its neighbours.
*/
public void render() {
//Draw a rectangle around the Waypoint
Vec2 screenPos = Main.getMain().getRenderer().worldToScreen(position);
Main.getMain().stroke(0.0f,144.0f,144.0f);
Main.getMain().fill(0);
Main.getMain().rect(screenPos.x-5,screenPos.y-5,10,10);
//Draws lines to all neighbouring Waypoints
Main.getMain().stroke(200.0f,200.0f,0.0f);
Vec2 neighbourPos = new Vec2(0.0f,0.0f);
for(Waypoint w : neighbours) {
neighbourPos = Main.getMain().getRenderer().worldToScreen(w.position);
Main.getMain().line(screenPos.x, screenPos.y, neighbourPos.x, neighbourPos.y);
}
}
/**
* Adds a Waypoint to this Waypoint's list of neighbours.
* @param waypoint The Waypoint you want added.
*/
public void addNeighbour(Waypoint waypoint) {
if(!neighbours.contains(waypoint)) {
neighbours.add(waypoint);
}
}
/**
* Return an ArrayList containing all the neighbouring Waypoints of this Waypoint.
* @return neighbours ArrayList<Waypoint> containing all the neighbouring Waypoints.
*/
public ArrayList<Waypoint> getNeighbours() {
return neighbours;
}
}
Righty, when I finilize the World before playing I link them up with this method:
Code:
/**
* Initializes all static waypoints in this world. SHOULD ONLY BE CALLED ONCE!
*/
public void initWaypoints() {
for(Waypoint w0 : waypoints) {
for(Waypoint w1 : waypoints) {
if(w0.equals(w1)) {
//System.out.println("this is the same waypoint, continueing");
continue;
}
if(isVisible(w0.position,w1.position)) {
//System.out.println("making neighbour");
w0.addNeighbour(w1);
}
}
}
//Remove Waypoints without neighbours
ArrayList<Waypoint> emptyWaypoints = new ArrayList<Waypoint>();
for(Waypoint w : waypoints) {
if(w.getNeighbours().size() == 0) {
System.out.println("There is a useless Waypoint at :"+w.position);
emptyWaypoints.add(w);
}
}
waypoints.removeAll(emptyWaypoints);
}
Ok they are linked up now and empty Waypoints didn't make it into the list.
Now from inside the game we can try to get a Route from point A to point B. This is the Route class, it will become more clear once you see the methods that use it:
Code:
import java.util.ArrayList;
import org.jbox2d.common.Vec2;
import nl.blissfulthinking.java.gamezero.Main;
import nl.blissfulthinking.java.gamezero.tools.CalculationTool;
/**
* Holds route information.
* @author Michiel
*
*/
public class Route {
/**
* The Waypoints that make up this Route
*/
private ArrayList<Waypoint> waypoints = null;
/**
* The starting position of this Route
*/
public Vec2 startPos = new Vec2(0.0f,0.0f);
/**
* The target position of this Route
*/
public Vec2 targetPos = new Vec2(0.0f,0.0f);
/**
* The length of this Route
*/
public float length = 0.0f;
/**
* Add a Waypoint to this Route. Also updates the length of this Route if successful.
* @param waypoint
* @return TRUE if the Waypoint was added, FALSE if it could not be added because the Waypoint was already part of this Route.
*/
public boolean addWaypoint(Waypoint waypoint) {
if(!waypoints.contains(waypoint)) {
float length = CalculationTool.getDistance(waypoints.get(waypoints.size()-1).position,waypoint.position);
this.length += length;
waypoints.add(waypoint);
return true;
}
else {
return false;
}
}
/**
* Create a new Route object with only its starting and target position set.
* @param startPos
* @param targetPos
*/
public Route(Vec2 startPos, Vec2 targetPos) {
this.startPos = startPos;
this.targetPos = targetPos;
}
/**
* Tries to nibble off some Waypoints by checking if the second and second last Waypoint are visible,
* from respectively the starting and target position.
*/
public void prune() {
if(waypoints != null) {
if(waypoints.size() > 1) {
if(Level.getWorld().isVisible(startPos, waypoints.get(1).position)) {
System.out.println("pruned off the first node");
waypoints.remove(0);
}
if(waypoints.size() > 1) {
if(Level.getWorld().isVisible(targetPos, waypoints.get(waypoints.size()-2).position)) {
System.out.println("pruned off the last node");
waypoints.remove(waypoints.size()-1);
}
}
}
}
}
/**
* Clone constructor
* @param r
*/
@SuppressWarnings("unchecked")
public Route(Route r) {
//Copy values from the provided Route
waypoints = (ArrayList<Waypoint>) r.getWaypoints().clone();
length = r.length;
this.startPos.x = r.startPos.x;
this.startPos.y = r.startPos.y;
this.targetPos.x = r.targetPos.x;
this.targetPos.y = r.targetPos.y;
}
/**
* Create a new Route with at least one Waypoint, more can (and should) be added afterwards with addWaypoint()
* @param w The Waypoint you want the Route to start with
* @param startPos The starting position of this Route
* @param targetPos The target position of this Route
*/
public Route(Waypoint w, Vec2 startPos, Vec2 targetPos) {
waypoints = new ArrayList<Waypoint>();
waypoints.add(w);
this.startPos = startPos;
this.targetPos = targetPos;
}
/**
* Returns the ArrayList<Waypoint> containing all Waypoints belonging to this Route
* @return ArrayList<Waypoint> if this Route has Waypoints or NULL if it does not hve any.
*/
public ArrayList<Waypoint> getWaypoints() {
return waypoints;
}
/**
* Debug rendering of Route information
*/
public void render() {
//If the Route has Waypoints attached
if(waypoints != null) {
Vec2 screenPos;
Vec2 neighbourPos;
for(int i=0;i<waypoints.size();i++) {
screenPos = Main.getMain().getRenderer().worldToScreen(waypoints.get(i).position);
Main.getMain().stroke(255.0f,0.0f,0.0f);
Main.getMain().rect(screenPos.x-5,screenPos.y-5,10,10);
if(i<waypoints.size()-1) {
Main.getMain().stroke(255.0f,0.0f,255.0f);
neighbourPos = waypoints.get(i+1).position;
neighbourPos = Main.getMain().getRenderer().worldToScreen(neighbourPos);
Main.getMain().line(screenPos.x, screenPos.y, neighbourPos.x, neighbourPos.y);
}
}
screenPos = Main.getMain().getRenderer().worldToScreen(startPos);
Main.getMain().stroke(255.0f,0.0f,0.0f);
Main.getMain().rect(screenPos.x-5,screenPos.y-5,10,10);
Main.getMain().stroke(255.0f,0.0f,255.0f);
neighbourPos = waypoints.get(0).position;
neighbourPos = Main.getMain().getRenderer().worldToScreen(neighbourPos);
Main.getMain().line(screenPos.x, screenPos.y, neighbourPos.x, neighbourPos.y);
screenPos = Main.getMain().getRenderer().worldToScreen(targetPos);
Main.getMain().stroke(255.0f,0.0f,0.0f);
Main.getMain().rect(screenPos.x-5,screenPos.y-5,10,10);
Main.getMain().stroke(255.0f,0.0f,255.0f);
neighbourPos = waypoints.get(waypoints.size()-1).position;
neighbourPos = Main.getMain().getRenderer().worldToScreen(neighbourPos);
Main.getMain().line(screenPos.x, screenPos.y, neighbourPos.x, neighbourPos.y);
}
//If there are no Waypoints in this route
else {
Vec2 startPosScreen = Main.getMain().getRenderer().worldToScreen(startPos);
Vec2 targetPosScreen = Main.getMain().getRenderer().worldToScreen(targetPos);
Main.getMain().stroke(255.0f,0.0f,0.0f);
Main.getMain().rect(startPosScreen.x-5,startPosScreen.y-5,10,10);
Main.getMain().rect(targetPosScreen.x-5,targetPosScreen.y-5,10,10);
Main.getMain().stroke(255.0f,0.0f,255.0f);
Main.getMain().line(startPosScreen.x, startPosScreen.y, targetPosScreen.x, targetPosScreen.y);
}
}
}
Now, to put it all together one can call the getFastestRoute(Vec2,Vec2) method from the World class:
important: The World class has a field "float shortestRouteLength" which is not visible down here.
The storedRoute thing is something I use to render the last Route created, you can remove that.
Code:
/**
* This method will try to provide you with a Route object containing a path from the provided starting position to the provided target position.
* @param startPos
* @param targetPos
* @return A Route object containing Waypoints between the provided positions,
* or a Route object with no Waypoints when the provided positions are visible from each other,
* or NULL if no Route can be made.
*/
public final Route getFastestRoute(Vec2 startPos, Vec2 targetPos) {
//If the positions are visible from each other
if(isVisible(startPos, targetPos)) {
System.out.println("CustomWorld.getFastestRoute() > TARGETS ARE VISIBLE !!!");
//We make an "empty" Route with only the start and target position
Route route = new Route(startPos,targetPos);
//I render the last Route that has been made, thats why this is here
storedRoute = route;
//Return result
return route;
}
//Since start and target are not visible we need starting and target Waypoints
Waypoint startWaypoint = getClosestWaypoint(startPos);
Waypoint targetWaypoint = getClosestWaypoint(targetPos);
//If either of them returns as NULL it means we can't make a route
if(startWaypoint == null || targetWaypoint == null) {
return null;
}
//If they return the same Waypoint, we can make a Route out of it and return that.
if(startWaypoint.equals(targetWaypoint)) {
Route route = new Route(startWaypoint,startPos,targetPos);
storedRoute = route;
return route;
}
//Make a new Route object with the starting Waypoint and the provided positions
Route route = new Route(startWaypoint,startPos,targetPos);
//Set the length of the shortest Route found to a maximum value
shortestRouteLength = Float.MAX_VALUE;
//Declare a array which can hold all valid Routes found and fill it with the result of the recursive method getRoutes()
ArrayList<Route> routes = getRoutes(route, targetWaypoint);
//If no routes were returned
if(routes.size() < 1) {
System.out.println("CustomWorld.getFastestRoute() > NO VALID ROUTE FOUND !!!");
//Return null
return null;
}
//If one or more Routes returned
else {
System.out.println("CustomWorld.getFastestRoute() > ROUTE FOUND !!!");
//Get the last Route in the array, which is the shortest Route
route = routes.get(routes.size()-1);
//Try to prune it
route.prune();
storedRoute = route;
return route;
}
}
public final ArrayList<Route> getRoutes(Route route, Waypoint target) {
//Make a new array to store found routes
ArrayList<Route> routes = new ArrayList<Route>();
//Get the neighbours of the last Waypoint in the route
ArrayList<Waypoint> neighbours = route.getWaypoints().get(route.getWaypoints().size()-1).getNeighbours();
//If the target is one of its neighbours
if(neighbours.contains(target)) {
System.out.println("Found target in waypoint's neighbours");
//Add the target to the route
route.addWaypoint(target);
System.out.println("Found a valid route over : " + route.getWaypoints().size() + " waypoints and a length of : " + route.length);
//If the route's length is now the shortestRouteLength
if(route.length < shortestRouteLength) {
//Update the shortestRouteLength length
shortestRouteLength = route.length;
System.out.println("It is the new shortestRouteLength path!");
//Add this route to the results
routes.add(route);
//Fastest route has been found from this Waypoint so we return it
return routes;
}
}
//Retrieve a sorted version
ArrayList<Waypoint> sortedNeighbours = getSortedNeighbours(neighbours,target);
//Declare a new Route object
Route r;
//Target was not amongst its neighbours so we search from there
for(Waypoint w : sortedNeighbours) {
//Make a new Route object
r = new Route(route);
//If the neighbour can be added it means it is at least valid, up to here
if(r.addWaypoint(w)) {
//This route is doomed because a faster route has been determined
if(r.length > shortestRouteLength) {
continue;
}
//Try to find more Routes from here
else {
routes.addAll(getRoutes(r,target));
}
}
}
//Return the possible results
return routes;
}
/**
* Sorts and returns a sorted version of the provided ArrayList<Waypoint> on the basis of their distance to the target Waypoint
* @param neighbours
* @param target
* @return
*/
@SuppressWarnings("unchecked")
private ArrayList<Waypoint> getSortedNeighbours(ArrayList<Waypoint> neighbours, Waypoint target) {
//Make a shallow copy
ArrayList<Waypoint> copy = (ArrayList<Waypoint>)neighbours.clone();
ArrayList<Waypoint> sorted = new ArrayList<Waypoint>();
float nearest = Float.MAX_VALUE;
int nearestIndex = 0;
int allWaypoints = copy.size();
float distance;
while(sorted.size() < allWaypoints) {
nearest = Float.MAX_VALUE;
nearestIndex = 0;
for(int i=0;i<copy.size();i++) {
distance = CalculationTool.getDistance(copy.get(i).position,target.position);
if(distance < nearest) {
nearest = distance;
nearestIndex = i;
}
}
sorted.add(copy.get(nearestIndex));
copy.remove(nearestIndex);
}
return sorted;
}
I will post some code showing how an enemy uses a Route shortly.
This method does not return the theoretical shortest path. I could make it that way but it would mean I would have to start searching from EVERY Waypoint visible from the starting position and start looking for EVERY Waypoint visible from the target location...
Any questions, suggestions, please post them. If I have left something out (could happen) please tell me.