Download presentation
Presentation is loading. Please wait.
Published byReynold Horn Modified over 9 years ago
1
1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding
2
2 Finite State Machine An abstract machine that can exist in one of several different and predefined states. Define a set of conditions that determine when the state should change. Characteristics Easy to understand Easy to implement and debug An Example Ghosts in Pac Man Finite states roaming freely, chasing the player, or evading the player Chasing Evading The player eats a power pill
3
3 Outline Basic State Machine Model Generic finite state machine Example- Ghost finite state machine Finite State Machine Design Structures and classes Behavior and transition functions Ant Example
4
4 Generic Finite State Machine Diagram S1 S2 S3 Si t1 t2 t3 t4 t5 Four possible states: {Si, S1, S2, S3} Transition functions: {t1, t2, t3, t4, t5}
5
5 Ghost finite state machine diagram Three possible states: roam, evade, chase Four transition functions: see true, see false, blue true, blue false See true: Ghost see the player Blue true: The player eats a power pill
6
6 Model Ghost Behavior (1) Roam blue = true? Yes Evade No seePlayer? Yes Chase No
7
7 Model Ghost Behavior (2&3) Current state: Chase Chase blue = true? Yes Evade No seePlayer? Yes Roam No Chase blue = true? Yes Evade No seePlayer? Yes Roam No Current state: Roam
8
8 Finite State Machine to describe Avatar behaviors
9
9 Network or Hierarchical of FSM to describe the logic of the whole game Uncertainty by random numbers Attack or Retreat?
10
10 Finite State Machines Advantages Fast data structure, dynamic memory or current state Non-deterministic FSM can make behavior unpredictable (by random numbers) Dis-advantages Number of states can grow very fast Number of arcs can grow even faster How may states can I go from here?
11
11 Pathfinding Problem Dependent Destination: moving or stationary? Obstacles? Terrain shape Shortest path Aim: move around or reach a destination?
12
12 Outline Basic Pathfinding Random Movement and Obstacle Avoidance Tracing Around Obstacles Breadcrumb Pathfinding Path Following Wall Tracing Waypoint Navigation
13
13 Basic Pathfinding Simple path movement Unnatural-looking path Line-of-Sight path movement More natural-looking path Starting point Destination Starting point Destination
14
14 Basic Pathfinding- Problems with Obstacles Obstacles The function of obstacle avoidance is necessary element in pathfinding
15
15 Random Movement and Obstacle Avoidance Simple and Effective Method Suitable Environment Few obstacles Example: A game environment with sparsely placed trees No Move in Random Direction Follow Straight Path to Player Player in Line of Sight? Yes Player’s position
16
16 Tracing Around Obstacles Find a Path around Large Obstacles Suitable Environment Large obstacles Example: a mountain range in a strategy or role-playing game Methods Basic Tracing Improved Tracing Tracing with Line of Sight
17
17 Basic Tracing - when to stop tracing? Follow the edge of the obstacle in an attempt to work way around it Destination
18
18 Improved Tracing Avoid looping back to the starting point Calculate a line from the tracing starts and the desired destination. Continue tracing until the line is crossed Switch to simple path finding state Destination
19
19 Tracing with Line of Sight Algorithm
20
20 Breadcrumb Pathfinding Memorize the previous play’s steps Each member follow the leader’s breadcrumb Suitable Environment Move groups of computer-controlled characters Advantages Seemed more intelligent More effective and efficient
21
21 Breadcrumb Trail Recording the player positions Dropping breadcrumbs Finding and Following the breadcrumbs Record 15 steps
22
22 Detect and Follow Breadcrumb Trail Are neighbors are breadcrumbs? Yes Follow the breadcrumb No Randomly move one step Current position Find neighbors: Eight possible directions Detect Breadcrumbs Follow shortest path Following the exact footsteps of the player would be rather unnatural. It is more intelligent to follow the detected shortest path.
23
23 Path Following-Containment Problem Starting point Destination Pathfinding Moving from a starting point to a desired destination Path following No obvious destination More of a containment problem: containing the computer-controlled role to the road area E.g., a car-racing game requires the computer- controlled cars to navigate a roadway Path Following
24
24 Path Following-Direction Analysis Terrain Analysis Detect Possible Directions Weighting Directions Update Position It would look unnatural if the computer-controlled object simply moves randomly on the road. We need to analyze the surrounding terrain and decide on the best move.
25
25 Terrain Analysis and Direction Analysis (x-1, y+1) (x, y+1)(x+1, y+1) (x-1, y)(x, y)(x+1, y) (x-1, y- 1) (x, y-1)(x+1, y-1) Are the eight neighbors contained in the terrain? Terrain analysis Consider tile-based environment Determine possible directions 1 2 3 4 5 6 7 8
26
26 Weighting Directions 1 2 3 4 5 6 7 8 Current direction (+2) (+1) (+0) (-1) (+0) (+1) Current direction Up and left: direction 1 Weighting directions Direction 1: +2 Continuing current direction is most preferred Directions 2 and 8: +1 The next two best possibilities Directions 3 and 7: 0 Direction 5: -1 Complete opposite direction is the most undesirable
27
27 Direction Determination Among possible directions Choose one with maximum weight Move in the selected direction Update position Improve robustness Examining more than just the adjacent tiles Road Path
28
28 Wall Tracing Exploration Technique Environment Many small rooms Maze-like game Example: computer-controlled characters explore the environment to search of the player, weapons, treasures,…
29
29 Example Environment Randomly moving about the environment is one often used solution which offers a certain level of unpredictability. However, this also can result in getting stuck in small rooms for long periods of time.
30
30 Wall Tracing Methods Move in Random Directions Systematically Explore the Entire Environment Left-handed approach If possible, always move left Completely explore the environment Conceptually easy More effective 1 2 3 4 5 6 7 8 Left BackStraight Right Directions in view of players
31
31 Left-handed Strategy Straight? Possible Next Step Impossible Left? Possible Next Step Impossible Right? possible Next Step Impossible Back? Next Step
32
32 Wall-tracing Path Left-handed movement method The character enters every room in the game environment The method is prevented from allowing the character to reach every single room
33
33 Waypoint Navigation Pre-calculate Paths Reduce Pathfinding Time and CPU Load Method Placing some nodes in the game environment Finding paths between nodes
34
34 Placing Nodes Rules of placing nodes Every point is in the line of sight of at least one node Every node is in the line of sight of at least one other node Labeling nodes
35
35 Finding Path From A to E: A -> B -> C -> E A path can be found from any room to any other room. Step 1. Calculate which node is nearest and in the line of sight Step 2. Follow the connections How does the computer know the connections among the nodes?
36
36 Building a Path Node Table Establish the connections between nodes Find a Shortest Path between Two Nodes - BBBBBB - - - - - - ABCDEFG A B C D E F G
37
37 Completed Node Table -BBBBBB A -CCCCC BB -DEEE CCC -CCC CCCC -FF EEEEE -G FFFFFF - ABCDEFG A B C D E F G The first step to take from node C to node F. Filling in the table is to determine the first node to visit when moving from any starting node to any ending node. Start End
38
38 Finding the Path Goal B G B -> G Table intersection ->C Move ->C C -> G Table intersection ->E Move ->E E -> G Table intersection ->F Move ->F F -> G Table intersection ->G Move ->G B -> C -> E -> F -> G
39
39 A * Algorithm One of the most widely used pathfinding algorithms It is guaranteed to find the best path between any two points Relatively efficient algorithm Can be used whenever possible unless dealing with some special-case scenario E.g., if a clear line of sight exists with no obstacles between the starting point and ending point, a faster line-of-sight movement algorithm would be better. Difficult to be understood by new game developers
40
40 Steps in A * Algorithm Defining and Simplifying the Search Area Starting the Search among a reasonable number of nodes Path Scoring to Determine the Best Path Finding a Dead End Incorporating the Terrain Cost Influence Cost Mapping
41
41 Defining the Search Area Simplifying the Search Area Placing nodes Reduce the nodes Maintain a list of connections A* Algorithm is more suitable to tiled environment Each tile serves as a node Simplifying the search area Tiled search area
42
42 Starting the Search Goal: Find the shortest path between any two nodes Suitable Environment A small tiled environment Each tile is a node Some nodes contain obstacles Search Method Begin at the starting point and spread to the adjacent tiles
43
43 Search Strategy open list – keep track of the tiled needed to be checked closed list – the tiles that already were checked and no longer to be examined Link – from neighbors to the currently considered tile open list <- starting point; For each element in open list Check each adjacent node of this element; If it is valid, add it to open list and calculate cost; Add the current element in open list to closed list; Build a link from current element to valid adjacent nodes;
44
44 Search Scenario A tiled search area ??? ?? ??? Adjacent tiles to check Obstacle areaStarting point Destination
45
45 After Checking all adjacent tiles Add to closed list Building a link open Searching Process
46
46 Scoring Each Tile Starting point Given tileDestination cost1cost2 Determined by heuristic Score = Cost from Start + Heuristic Cost to move from the starting tile to any given tile Cost to move from the given tile to the destination tile
47
47 Calculating Path Score c (cost) = s + h s – the number of steps from the starting tile to the given tile; h – the number of steps from given tile to the destination. open c = 1 + 4 = 5 Minimum {c} = 4
48
48 Examining the Tiles with Minimum Cost open closed open Currently considered tile The starting point is added in the closed list. The tile with minimum cost is examined currently. There are three valid tiles in neighbors of current tile, which are added to open list.
49
49 Spread to Adjacent Tiles closed open Current tile closed open Current tile open
50
50 Completed Path closed open closed The path is found when the destination is added in the open list.
51
51 Finding a Dead Path Monitor the open list – if no member in the open list to examine, then reach a dead path. It is always that no valid path exists between any two given points. How do we know when we have reached a dead end?
52
52 Terrain Cost Shortest Path = Fastest Path? Which is faster? A long walk along a road A shorter walk through a swamp Consider Terrain Cost Total Cost from Start = Cost from Start + Terrain Cost Score = Total Cost from Start + Heuristic
53
53 Types of Terrains and Costs Open Terrain Cost = 1 GrasslandCost = 3 SwamplandCost = 5 Other terrains: hills, rivers, … Previously, the cost of moving from one node to another was always 1.
54
54 Finding Lowest-Cost Path Original path The lowest-cost path is not necessarily the shortest path. Lowest cost Shortest path
55
55 Influence cost Mapping Influence cost mapping – a way to vary the cost of the A * nodes depending on what is happening in the game, such as enemy… Building a Influence Map Position Orientation The influence map cost will be added to each node’s value when calculating possible paths.
56
56 Building a Influence Map Influenced by the enemy firing zone The tiles in the line of fire still are passable, just at a high cost. If no other path is possible, or if the alternate paths have a higher cost, the game character will pass through the line of fire.
57
57 Influenced by What the Character Does Influenced by the number of kills Each time the player makes a kill, that node increases in cost (e.g., possible places of NPC appearance).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.