INTERACTIVE OBJECTS IN
GAMING APPLICATIONS
Basic principles and practical scenarios in Unity
June 2013 Stavros Vassos Sapienza University of Rome, DIAG, Italy [email protected]
Interactive objects in games 2
Pathfinding
Part 1: A* heuristic search on a grid
Part 2: Examples in Unity
Part 3: Beyond the basics
Action-based decision making
AI Architectures
Pathfinding: Beyond the basics 3
Many neat tricks for efficient/nice pathfinding
Alternative game-world representations
Beyond A*
Better heuristics
Pathfinding: Beyond the basics 4
Many neat tricks for efficient/nice pathfinding
Alternative game-world representations
Beyond A*
Better heuristics
Pathfinding: From grids to graphs 5
A* works the same in a graph as in a grid
Graphs nodes correspond to points in the 2D/3D space
We can still use the same heuristics
Manhattan, Chebyshev, Euclidean
Pathfinding: From grids to graphs 6
A* works the same in a graph as in a grid
Graphs nodes correspond to points in the 2D/3D space
We can still use the same heuristics
Manhattan, Chebyshev, Euclidean
But we need to
generate these
graphs in an
automated way!
Pathfinding: From grids to graphs 7
Many types of graphs have been studied in video-
games as well as in robotics
Waypoint graphs
Circle-based waypoint graphs
Visibility graphs
Navigation mesh graphs
Probabilistic road maps
…
Pathfinding: From grids to graphs 8
Many types of graphs have been studied in video-
games as well as in robotics
Waypoint graphs
Circle-based waypoint graphs
Visibility graphs
Navigation mesh graphs
Probabilistic road maps
…
Nice description on Amit’s A* pages, next
Pathfinding: From grids to graphs 9
Visibility graph
Connected obstacle corners
Navmesh graph
Connected walkable areas
Pathfinding: From grids to graphs 10
Visibility graph
Connected obstacle corners
Navmesh graph
Connected walkable areas
Visibility graph:
Often contains too
many edges
Pathfinding: From grids to graphs 11
Visibility graph
Connected obstacle corners
Navmesh graph
Connected walkable areas
Navmesh graph:
Several ways to
map to a graph
Pathfinding: Navmesh graphs 12
Navmesh graph
Area decomposed in polygons
One or more graph nodes per polygon
Polygon center movement
One node at the
center of each polygon
Nodes connected if
they are in adjacent
polygons
Pathfinding: Navmesh graphs 13
Navmesh graph
Area decomposed in polygons
One or more graph nodes per polygon
Polygon edge movement
One graph node at the
center of each edge
between adjacent
polygons
Don’t have to go to the
center of a polygon
Pathfinding: Navmesh graphs 14
Navmesh graph
Area decomposed in polygons
One or more graph nodes per polygon
Polygon vertex movement
Graph nodes and edges
at each as the polygons
Helpful for moving
around an obstacle,
following its edges
Pathfinding: Navmesh graphs 15
Navmesh graph
Area decomposed in polygons
One or more graph nodes per polygon
Pick your own hybrid
combination!
Here: graph nodes both
at nodes of polygons
and the middle
of edges of polygons
Pathfinding: Navmesh graphs 16
Navmesh graph
Widely used in video games!
Supported natively in Unity Pro after version 4
A lot of passionate supporters :)
Pathfinding: Navmesh graphs 17
“Fixing pathfinding once and forall” by Paul Tozour
(2008) at www.ai-blog.net
Pathfinding on waypoint graphs vs navmesh graphs
1. Some graphs require too many waypoints
Pathfinding: Navmesh graphs 18
“Fixing pathfinding once and forall” by Paul Tozour
(2008) at www.ai-blog.net
Pathfinding on waypoint graphs vs navmesh graphs
2. More difficult to make “smooth” realistic paths
Pathfinding: Navmesh graphs 19
“Fixing pathfinding once and forall” by Paul Tozour
(2008) at www.ai-blog.net
Pathfinding on waypoint graphs vs navmesh graphs
3. More difficult to handle dynamic obstacle avoidance
Pathfinding: Navmesh graphs 20
“Fixing pathfinding once and forall” by Paul Tozour
(2008) at www.ai-blog.net
Pathfinding on waypoint graphs vs navmesh graphs
Other interesting observations in the article
Pathfinding: Beyond the basics 21
Many neat tricks for efficient/nice pathfinding
Alternative game-world representations
Beyond A*
Better heuristics
Pathfinding: Beyond A* 22
Incremental versions of A*
Real-time/Any-time versions of A*
Hierarchical versions of A*
Any-angle pathfinding
Diverse character pathfinding
Cooperative pathfinding
Pathfinding: Beyond A* 23
Incremental versions of A*
Real-time/Any-time versions of A*
Hierarchical versions of A*
Any-angle pathfinding
Diverse character pathfinding
Cooperative pathfinding
Nice article by Alex Nash at AiGameDev.com, next
Pathfinding: Beyond A* 24
Any-angle pathfinding
Theta*
Combining visibility graphs and the power of A* on grids
Simple extension based on A*
Here: 8-connected nodes at the corners of each cell
Pathfinding: Beyond A* 25
Finding better/nicer paths vs post-processing paths
Pathfinding: Beyond A* 26
Finding better/nicer paths vs post-processing paths
Manhattan distance
heuristic
Chebyshev distance
heuristic
Pathfinding: Beyond A* 27
Finding better/nicer paths vs post-processing paths
Manhattan distance
heuristic
Chebyshev distance
heuristic
Slower, but easier
to improve
Faster, but more
difficult to improve
Pathfinding: Beyond A* 28
Any-angle pathfinding with Theta*
Very similar to A*
Search from the start node
Open list of nodes to visit
Closed list of visited nodes
Expand a node by looking
into neighbors
Compute and store f(s’) for each of the neighbors of
expanded node s: f(s’) = g(s’) + h(s’)
Pathfinding: Beyond A* 29
Any-angle pathfinding with Theta*
Very similar to A*
Search from the start node
Open list of nodes to visit
Closed list of visited nodes
Expand a node by looking
into neighbors
Compute and store f(s’) for each of the neighbors of
expanded node s: f(s’) = g(s’) + h(s’)
But, do a trick in computing g(s’) and the parent of s’!
Pathfinding: Beyond A* 30
Any-angle pathfinding with Theta*
Expand node s
Look into neighbors of s, e.g., node s’
Consider two cases when computing g(s’)
Pathfinding: Beyond A* 31
Any-angle pathfinding with Theta*
Expand node s
Look into neighbors of s, e.g., node s’
Consider two cases when computing g(s’)
Option 1: g(s’) = g(s) + c(s,s’)
Pathfinding: Beyond A* 32
Any-angle pathfinding with Theta*
Expand node s
Look into neighbors of s, e.g., node s’
Consider two cases when computing g(s’)
Option 1: g(s’) = g(s) + c(s,s’)
Option 2: g(s’) = g(p) + c(p,s’),
where p is the parent of s!
Pathfinding: Beyond A* 33
Any-angle pathfinding with Theta*
Expand node s
Look into neighbors of s, e.g., node s’
Consider two cases when computing g(s’)
Option 1: g(s’) = g(s) + c(s,s’)
Option 2: g(s’) = g(p) + c(p,s’),
where p is the parent of s!
If the Option 2 is better also
update the parent!
Pathfinding: Beyond A* 34
Any-angle pathfinding with Theta*
Essentially remove unnecessary grid-like steps, using
visibility information
Simplifying and smoothing as you go!
Pathfinding: Beyond A* 35
Any-angle pathfinding with Theta*
Essentially remove unnecessary grid-like steps, using
visibility information
Simplifying and smoothing as you go!
Pathfinding: Beyond A* 36
Incremental versions of A*
Real-time/Any-time versions of A*
Hierarchical versions of A*
Any-angle pathfinding
Diverse character pathfinding
Cooperative pathfinding
Nice resources available!
Pathfinding: beyond A* 37
AAAI 2008 tutorial on path planning
Part 1: Advances in Path Planning by Sven Koenig
http://webdocs.cs.ualberta.ca/~nathanst/aaai_tut1.pdf
Any-angle pathfinding
Field D*, Theta*
Incremental versions of A*
FSA*, AA*, LPA*, MTAA*, D*Lite
Real-time versions of A*
LRTA*, RTAA*
Pathfinding: beyond A* 38
AAAI 2008 tutorial on path planning
Part 2: Abstraction in Pathfinding by Nathan Sturtevant
http://webdocs.cs.ualberta.ca/~nathanst/aaai_tut2.pdf
http://webdocs.cs.ualberta.ca/~nathanst/pathfinding.html
Abstraction for minimizing memory
Used in BioWare’s Dragon Age
Generalized Cooperative pathfinding
CA*, WHCA*, PRA*, CPRA*
Pathfinding: beyond A* 39
Beyond A*: Speeding up pathfinding through
hierarchical abstraction by Daniel Harabor
http://harablog.files.wordpress.com/2009/06/beyonda
star.pdf
Hierarchical pathfinding
HPA*
Dealing with diversity
HAA*
Beyond grids
Probabilistic
Road Maps
Pathfinding: beyond A* 40
AAAI 2008 tutorial on path planning
Part 3: Map Representations & Geometric Path Planning
by Michael Buro
http://webdocs.cs.ualberta.ca/
~nathanst/aaai_tut3.pdf
Polygon-based
Free space decompositions
TA*: Triangulation A*
TRA*: Triangulation reductions A*
Pathfinding: Beyond the basics 41
Many neat tricks for efficient/nice pathfinding
Alternative game-world representations
Beyond A*
Better heuristics
Pathfinding: Better heuristics 42
True distance memory based heuristics (TDHs)
Main idea based on precomputing useful information
Store true distance between selected pairs of nodes
Use these to calculate admissible estimates for any pair
Different variations
How to chose the initial pairs to pre-compute
How to calculate heuristic values from pre-computed
pairs
Recent IJCAI-09 paper and SOCS-10 paper
Pathfinding: Better heuristics 43
Differential heuristic
Pick a set of “canonical” nodes
Compute true distance between every node and the
canonical nodes
For dist(a,b) look into all canonical nodes c and take as
estimate the max value of |dist(a,c)-dist(b,c)|
Pathfinding: Better heuristics 44
Differential heuristic
Pick a set of “canonical” nodes
Compute true distance between every node and the
canonical nodes
For dist(a,b) look into all canonical nodes c and take as
estimate the max value of |dist(a,c)-dist(b,c)|
Admissible!
Intuition: think of just one canonical state c
dist(a,b)+dist(b,c) has to be greater or equal than dist(a,c)
otherwise this would have been stored as a shorter route
The max out of many canonical states is a better lowerbound
a b
c
Pathfinding: Better heuristics 45
Canonical heuristic
Pick a set of “canonical” nodes
Compute true distance between the canonical nodes
Compute true distance between every node and the m
closest canonical nodes
For dist(a,b) look into all pairs of m canonical nodes c1
close to a and m canonical nodes c2 close to b, and
take as estimate the max value of
|dist(c1,c2) - dist(a,c1) - dist(b,c2)|
Pathfinding: Better heuristics 46
Border heuristic and portal-based heuristic
Domain is partitioned in regions
Borders between regions
Portals connect regions
Store true distance
between pairs of portals
Portal-based search
that breaks pathfinding
into sub-problems
Pathfinding: Compress Path Databases 47
Another neat idea in a recent AIIDE-11 paper
Ultra-Fast Optimal Pathfinding without Runtime Search
Pathfinding: Compress Path Databases 48
Another neat idea in a recent AIIDE-11 paper
Ultra-Fast Optimal Pathfinding without Runtime Search
Pre-computing and store all paths!
Compress Path Databases that bring the data to
manageable size
Compared to A* runtime speedup reaches two orders
of magnitude
It has been tested over maps of Baldur’s Gate
Pathfinding: Compress Path Databases 49
“Path coherence”
The shortest paths from a current node to any target in
a remote area share a common first move
Think of going from Rome to a destination in any city in
western Europe
Most probably all shortest paths from a specific
starting point to any of these destinations have to pass
through some fixed landmarks, e.g., a major intersection
CPDs store this information for every node in the graph
Pathfinding: Compress Path Databases 50
CPDs compress this information for every node in
the graph using boxes of destinations that can be
“directed” in the same way
1. Starting from here
2. Going to here
Pathfinding: Compress Path Databases 51
CPDs compress this information for every node in
the graph using boxes of destinations that can be
“directed” in the same way
1. Starting from here
2. Going to here
3. You need to go NE!
Pathfinding: Compress Path Databases 52
CPDs compress this information for every node in
the graph using boxes of destinations that can be
“directed” in the same way
One starting node
For all possible destinations
Compute the next move that you
needs to be performed
Make this map
Store it by compressing
regions