refactor and navmesh color change

This commit is contained in:
Priyatham Sai Chand 2022-11-07 12:38:40 -08:00
parent ce71745c1d
commit c72f9bc768
No known key found for this signature in database
GPG Key ID: 5DF163EB5A38175B
3 changed files with 80 additions and 79 deletions

26
Map.pde
View File

@ -286,7 +286,7 @@ class Map
} }
} }
boolean isReachable(PVector point) public boolean isReachable(PVector point)
{ {
if (!isPointInPolygon(point, outline)) return false; if (!isPointInPolygon(point, outline)) return false;
for (Obstacle o: obstacles) for (Obstacle o: obstacles)
@ -295,29 +295,5 @@ class Map
} }
return true; return true;
} }
PVector percentFromPoint(PVector from, PVector to, float percent)
{
//p1 + ((p2 - p1) * percent)
return PVector.add(from, PVector.mult(PVector.sub(to, from),percent));
}
boolean intersectsWall(PVector from, PVector to)
{
//5% of the way from the start
PVector start = percentFromPoint(from, to, 0.01);
//95% of the way from the start
PVector end = percentFromPoint(from, to, 0.99);
if (!isReachable(start)) return true;
//println("Start: " + start);
//println("End: " + end);
for (Wall w : walls)
{
if (w.crosses(start, end)) return true;
}
return false;
}
} }

View File

@ -25,8 +25,8 @@ class Node
x_avg += w.start.x; x_avg += w.start.x;
y_avg += w.start.y; y_avg += w.start.y;
} }
x_avg /=polygon.size(); x_avg /= polygon.size();
y_avg /=polygon.size(); y_avg /= polygon.size();
return new PVector(x_avg, y_avg); return new PVector(x_avg, y_avg);
} }
@ -35,7 +35,7 @@ class Node
int prev = indices.get(indices.size()-1); int prev = indices.get(indices.size()-1);
for(Integer i: indices) for(Integer i: indices)
{ {
if (n.indices.contains(prev) && n.indices.contains(i)) return true; if (n.indices.contains(i) && n.indices.contains(prev)) return true;
prev = i; prev = i;
} }
@ -74,11 +74,11 @@ class NavMesh
{ {
ArrayList<Node> nodes = new ArrayList<Node>(); ArrayList<Node> nodes = new ArrayList<Node>();
int rec_stack_count = 0; int rec_stack_count = 0;
int maxDepth = 1000; int max_depth = 1000;
int pointAmount = 0;
HashMap<PVector, Integer> vert_lookup_map = new HashMap<PVector, Integer>(); HashMap<PVector, Integer> vert_lookup_map = new HashMap<PVector, Integer>();
ArrayList<PVector> mapVectors = new ArrayList<PVector>(); ArrayList<PVector> map_vecs = new ArrayList<PVector>();
PVector midpoint(Node a, Node b) PVector midpoint(Node a, Node b)
{ {
@ -88,19 +88,19 @@ class NavMesh
int prev_index = a.indices.get(a.indices.size()-1); int prev_index = a.indices.get(a.indices.size()-1);
for(Integer i: a.indices) for(Integer i: a.indices)
{ {
if (b.indices.contains(prev_index) && b.indices.contains(i)) { if (b.indices.contains(i) && b.indices.contains(prev_index)) {
start = prev_index; start = prev_index;
end = i; end = i;
break; break;
} }
prev_index = i; prev_index = i;
} }
println(a.id + " and " + b.id + " share indices " + start + " and " + end);
PVector start_vect, end_vect; PVector start_vect, end_vect;
start_vect = mapVectors.get(start); start_vect = map_vecs.get(start);
end_vect = mapVectors.get(end); end_vect = map_vecs.get(end);
return new PVector(start_vect.x + (end_vect.x - start_vect.x)/2, return new PVector(start_vect.x + (end_vect.x - start_vect.x)/2,
start_vect.y + (end_vect.y - start_vect.y)/2); start_vect.y + (end_vect.y - start_vect.y)/2);
@ -134,7 +134,7 @@ class NavMesh
} }
} }
} }
void setIndices(Node node) void setNodeIndices(Node node)
{ {
for(Wall w: node.polygon) for(Wall w: node.polygon)
{ {
@ -142,7 +142,7 @@ class NavMesh
} }
} }
//assume index_1 < index_2
void splitMap(Node node, int index_1, int index_2) void splitMap(Node node, int index_1, int index_2)
{ {
@ -189,17 +189,17 @@ class NavMesh
} }
Node nodeA = new Node(rec_stack_count+"A", polygon_1); Node nodeA = new Node(rec_stack_count+"A", polygon_1);
setIndices(nodeA); setNodeIndices(nodeA);
nodes.add(nodeA); nodes.add(nodeA);
Node nodeB = new Node(rec_stack_count+"B", polygon_2); Node nodeB = new Node(rec_stack_count+"B", polygon_2);
setIndices(nodeB); setNodeIndices(nodeB);
nodes.add(nodeB); nodes.add(nodeB);
rec_stack_count++; rec_stack_count++;
if (rec_stack_count == maxDepth) return; if (rec_stack_count == max_depth) return;
if (findReflexVertex(polygon_1) != -1) { if (findReflexVertex(polygon_1) != -1) {
nodes.remove(nodeA); nodes.remove(nodeA);
@ -228,7 +228,32 @@ class NavMesh
return -1; return -1;
} }
PVector percentFromPoint(PVector from, PVector to, float percent)
{
//p1 + ((p2 - p1) * percent)
return PVector.add(from, PVector.mult(PVector.sub(to, from),percent));
}
boolean intersectsWall(PVector from, PVector to)
{
//threshold to see if wall intersects with 1% margin.
PVector start = percentFromPoint(from, to, 0.01);
//95% of the way from the start
PVector end = percentFromPoint(from, to, 0.99);
if (!map.isReachable(start)) return true;
//println("Start: " + start);
//println("End: " + end);
for (Wall w : map.walls)
{
if (w.crosses(start, end)) return true;
}
return false;
}
int joiningVertex(ArrayList<Wall> polygon, int convex_index) int joiningVertex(ArrayList<Wall> polygon, int convex_index)
{ {
@ -248,16 +273,16 @@ class NavMesh
int lastIndex = convex_index - 1; int lastIndex = convex_index - 1;
if (lastIndex < 0) lastIndex = vertices.size() - 1; if (lastIndex < 0) lastIndex = vertices.size() - 1;
for (int potentialConnecting = vertices.size()-1; potentialConnecting>=0; potentialConnecting--) for (int conn_point = vertices.size()-1; conn_point>=0; conn_point--)
{ {
if (potentialConnecting == next_index || potentialConnecting == convex_index || potentialConnecting == lastIndex) continue; if (conn_point == next_index || conn_point == convex_index || conn_point == lastIndex) continue;
PVector potentialConnectingPoint = vertices.get(potentialConnecting); PVector conn_pointPoint = vertices.get(conn_point);
if (!map.intersectsWall(pointAtIndex, potentialConnectingPoint)) if (!intersectsWall(pointAtIndex, conn_pointPoint))
{ {
return potentialConnecting; return conn_point;
} }
} }
@ -282,13 +307,13 @@ class NavMesh
void setVertexMap(Map map) void setVertexMap(Map map)
{ {
//clear all lookups and map vectors //clear all lookups and map vectors
mapVectors.clear(); map_vecs.clear();
vert_lookup_map.clear(); vert_lookup_map.clear();
for (int i = 0; i < map.walls.size(); i++) for (int i = 0; i < map.walls.size(); i++)
{ {
vert_lookup_map.put(map.walls.get(i).start, i); vert_lookup_map.put(map.walls.get(i).start, i);
mapVectors.add(map.walls.get(i).start); map_vecs.add(map.walls.get(i).start);
} }
} }
@ -300,18 +325,18 @@ class NavMesh
// to keep track of recursive calls // to keep track of recursive calls
rec_stack_count = 0; rec_stack_count = 0;
nodes.clear(); nodes.clear();
pointAmount = map.walls.size();
origin_map = map; origin_map = map;
vert_lookup_map.clear(); vert_lookup_map.clear();
mapVectors.clear(); map_vecs.clear();
//make hashmap of vertices //make hashmap of vertices
setVertexMap(map); setVertexMap(map);
//create a node with the whole map walls //create a node with the whole map walls
Node m = new Node("Map", map.outline); Node m = new Node("Map", map.outline);
setIndices(m); setNodeIndices(m);
convexDecomposition(m); convexDecomposition(m);
@ -333,26 +358,19 @@ class NavMesh
//Uses A* to find a path from start to dest //Uses A* to find a path from start to dest
ArrayList<PVector> findPath(PVector start, PVector dest) ArrayList<PVector> findPath(PVector start, PVector dest)
{ {
println("dest vec " + dest);
ArrayList<SearchFrontier> frontier = new ArrayList<SearchFrontier>(); ArrayList<SearchFrontier> frontier = new ArrayList<SearchFrontier>();
ArrayList<Node> visited_nodes = new ArrayList<Node>(); ArrayList<Node> visited_nodes = new ArrayList<Node>();
Node node_start = nodeFromPoint(start); Node node_start = nodeFromPoint(start);
Node node_dest = nodeFromPoint(dest); Node node_dest = nodeFromPoint(dest);
println("dest node " + node_dest);
//for (Node n: nodes)
//{
// if (isPointInPolygon(start,n.polygon)) node_start = n;
// else if (isPointInPolygon(dest,n.polygon)) node_dest = n;
// //else println("node_dest is null");
//}
SearchFrontier s = new SearchFrontier(node_start, null, node_dest.findCenter()); SearchFrontier s = new SearchFrontier(node_start, null, node_dest.findCenter());
frontier.add(s); frontier.add(s);
visited_nodes.add(frontier.get(0).node); visited_nodes.add(frontier.get(0).node);
println("frontier " + frontier);
//till the end of of frontier //till the end of of frontier
while (frontier.get(0).node != node_dest) while (frontier.get(0).node != node_dest)
{ {
@ -362,7 +380,7 @@ class NavMesh
for (Node neighbours: first_frontier.node.neighbours) for (Node neighbours: first_frontier.node.neighbours)
{ {
println("loop");
if (!visited_nodes.contains(neighbours)) if (!visited_nodes.contains(neighbours))
{ {
frontier.add(new SearchFrontier(neighbours, first_frontier, node_dest.findCenter())); frontier.add(new SearchFrontier(neighbours, first_frontier, node_dest.findCenter()));
@ -405,7 +423,7 @@ class NavMesh
Collections.reverse(res); Collections.reverse(res);
println("result " + res);
return res; return res;
} }
@ -425,14 +443,21 @@ class NavMesh
{ {
for (Wall w: n.polygon) for (Wall w: n.polygon)
{ {
stroke(0,255,255); stroke(255);
strokeWeight(3); strokeWeight(1);
line(w.start.x, w.start.y, w.end.x, w.end.y); line(w.start.x, w.start.y, w.end.x, w.end.y);
//w.draw(); //w.draw();
} }
} }
for( Wall w: map.outline){
stroke(255,0,0);
strokeWeight(3);
line(w.start.x, w.start.y, w.end.x, w.end.y);
}
} }
} }

View File

@ -37,26 +37,26 @@ void mousePressed() {
if (mouseButton == LEFT) if (mouseButton == LEFT)
{ {
if (!entering_path) //if you haven't made a path yet if (!entering_path)
{ {
//println("node size " + nm.nodes.size());
if (nm.nodes.size() > 0) //if you're on a map if (nm.nodes.size() > 0) // if map is pressed and nodes are still left
{ {
println("Pathfinding to single target");
waypoints = nm.findPath(billy.kinematic.position, target); waypoints = nm.findPath(billy.kinematic.position, target);
println("waypoints " + waypoints);
//Collections.reverse(waypoints);
billy.follow(waypoints); billy.follow(waypoints);
} }
else { //if you're not on a map else { //target seeking on plain space
println("Simply seeking target");
billy.seek(target); billy.seek(target);
} }
} }
else //if you have a path else
{ {
//finish the path //finish the path
if (nm.nodes.size() > 0) //if you're on a map if (nm.nodes.size() > 0) //remaining map if left
{ {
PVector start_vectoint = waypoints.get(waypoints.size() -1); PVector start_vectoint = waypoints.get(waypoints.size() -1);
ArrayList<PVector> finalRoute = nm.findPath(start_vectoint, target); ArrayList<PVector> finalRoute = nm.findPath(start_vectoint, target);
@ -65,13 +65,13 @@ void mousePressed() {
waypoints.add(p); waypoints.add(p);
} }
} }
else //if you're not on a map else //waypoints to be added
{ {
waypoints.add(target); waypoints.add(target);
} }
println("Finishing Path");
entering_path = false; entering_path = false;
println(waypoints);
billy.follow(waypoints); billy.follow(waypoints);
} }
} }