I have been working in the past weeks on a multiplayer HTML5 game, using nodejs
and websockets
.
I've been stuck in this problem for a little while. Imagine that I have this tilesheet map implemented with an array (as shown below).
1 or brown tiles - there is an obstacle in the way and the player can not pass through it.
0 or green tiles - are free paths where the player is allowed to move.
Access any tile on the map by calling:
array[x][y]
I would like to create the fastest algorithm possible to find out the shortest route (if there is one) between two points of the map. How would you approach this problem? I know this is common problem.
Example:
Player at the position (1,7) fires a bullet with some AI that will toward the enemy player at the position (6,0). Bullet has to calculate the shortest route between the 2 players and if there aren´t any it would just explode against a wall.
Question:
How to efficiently find the shortest route between two points?
This is a common graph theory problem algorithm
In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized.
The problem of finding the shortest path between two intersections on a road map (the graph's vertices correspond to intersections and the edges correspond to road segments, each weighted by the length of its road segment) may be modeled by a special case of the shortest path problem in graphs.
For now exists lot of implementations of this algorithm. More simpler in implementation is a Dijkstra's algorithm with worst case performance as O(|E|+|V|log|V|)
where
Algorithm will assign some initial distance values and will try to improve them step by step:
Assign to every node a tentative distance value: set it to 0 for our initial node and to ∞ for all other nodes.
Set the initial node as current. Mark all other nodes unvisited. Create a set of all the unvisited nodes called the unvisited set.
For the current node, consider all of its unvisited neighbors and calculate their tentative distances. Compare the newly calculated tentative distance to the current assigned value and assign the smaller one.
When we are done considering all of the neighbors of the current node, mark the current node as visited and remove it from the unvisited set. A visited node will never be checked again.
If the destination node has been marked visited (when planning a route between two specific nodes) or if the smallest tentative distance among the nodes in the unvisited set is ∞ (when planning a complete traversal; occurs when there is no connection between the initial node and remaining unvisited nodes), then stop. The algorithm has finished.
Otherwise, select the unvisited node that is marked with the smallest tentative distance, set it as the new "current node", and go back to step 3.
More implementations of Dijkstra algorithm you can find on github repository mburst/dijkstras-algorithm.
For example here is JavaScript implementation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With