Course Outline Video Game AI: Lecture 7 Heuristics & Smoothing Nathan Sturtevant COMP 3705 http://aigamedev.com/ now has free interviews! Miscellaneous details Heuristics How better heuristics can be built Path smoothing Simple smoothing Funnel algorithm Variable size units / special movement? Not necessary in all games Artists can make sure all areas are passable If planning is done in an abstract state space, build a different abstraction for every unit type Add the radius of the unit to all constrained edges Doesn t quite handle all cases If units can jump, need different connectivity Finding the best location out of several A* uses a heuristic to guide search toward a single target If there are multiple targets, you would need to take the minimum of the heuristic distance to each In practice, it is much better to search in reverse
Search with two goals Search with two goals G S G G S G Speeding search There are many ways to speed search Most common is abstraction Build a more abstract graph and search on it (Last Wednesday s lecture) An alternative Build a better heuristic Building better heuristics The book discusses several methods We will discuss a few quickly Read the details in the book One method is not covered in the book Will discuss in more detail
Heuristics Fundamentally: Describe the constraints of the problem A heuristic is the exact solution to the problem with one or more constraints removed Grid example: Move from the start to goal through adjacent cells, avoiding blocked cells Relax adjacency or blocked cells Heuristic - Take advantage of the state space Suppose map is divided by a bridge Every optimal path between the top and bottom of the map will cross that bridge Option 1: Plan two separate paths, one from the bridge and one away from it Option 2: Pre-compute distances to the bridge
Building a heuristic - memory Heuristics In this case, just store the distance from every point to the point in the center Advantage of this approach: O(N) memory, with N states in graph/map Any pathfinding between the four regions will be nearly immediate Pathfinding within a region will require a full search Long paths become fast Not very much memory required Disadvantage of the approach: Only works on certain types of maps Could precompute: Approx: 4 (N/4)2 + N = N2/4 + N Factor of 4 saving over APSP Alternate approach: differential heuristics Simple idea: Perform a Dijkstra search from any one state in the map For each state reached, store the distance to s1 Estimate the distance between any two states using the triangle inequality h(x, y) = d(x, s1) - d(y, s1)! d(x, y) Prefer to pick states at the end of the state space Easier to visualize as a 1-d projection of the state space
Differential Heuristic Advantages: Works for more types of state spaces Can build several heuristics and take the max Disadvantages May require more storage Won t work well on 2d spaces But, we tend to have good default heuristics for 2d spaces The best results use compression & specialized A* Simple smoothing Any path can be represented by line segments What algorithms can be performed on line segments to shorten paths that are too long Attempt 1: Straight-line checks Begin at the first point in the path Perform a straight line check to every other vertex Replace previous vertices with the straight line
Attempt 1 Analysis How many line tests/raycasts required? Guaranteed to produce optimal path? Not all optimal smoothing points are on endpoints of path segments Order matters Paths still a bit blocky Attempt 2: Limited straight-line checks Try to smooth optimal octile segments Only if underlying representation is a grid Try to connect the midpoints of the line segments Maintain a minimum line segment length Don t smooth too close to obstacles
Attempt 2 analysis How many line tests/raycasts required? Smoothed path likely to be homotopic to previous path Not checking far enough ahead to make big cuts in path length Attempt 3: Path following Instead of trying to generate a smooth path, follow the path in a smooth way Pick a point slightly ahead of the character Walk towards that point Update each frame Subtle change in DA:O Limit the character turn rate Possibly take several frames to turn
Attempt 3 analysis Pros Cost is minimal Can be very effective in practice Cons May bump into walls/traps Attempt 4 Find closest point on polygon boundary Generally used when not convex Otherwise there is a better approach
More complex smoothing Funnel Algorithm When a path is defined by a sequence of polygons, the funnel algorithm finds the optimal path through For each additional segment, optionally joint the new points with the previous candidate line segments Definitions Channel Bounded area in which path is being produced Funnel Two sets of line segments which define potential area for future segments of the path Path Existing path which has been produced Apex Intersection between the end of the path and start of the funnel Funnel Algorithm Walk down each new edge Extend the existing left/right line segment This extends the funnel If the left/right lines cross, a new apex is defined Start over with the new apex
Funnel Algorithm Pros: Produces high-quality paths No more difficult than many other approaches Cons: Need representation amenable to funnels Length of path may be significantly different than when planning Conclusions Finding a path is only part of the problem Making it look good is just as important Wednesday: Flocking, group movement, etc