+ All Categories
Home > Documents > 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path...

1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path...

Date post: 18-Dec-2015
Category:
Upload: reynold-horn
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
57
1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding
Transcript
Page 1: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

1

AI for Computer Game Developers

Finite state machinesPath finding and waypointsA-Star path finding

Page 2: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Th

e p

layer

eats a

pow

er

pill

Page 3: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 4: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

4

Generic Finite State Machine Diagram

S1 S2

S3Si

t1

t2

t3t4

t5

Four possible states: {Si, S1, S2, S3}

Transition functions: {t1, t2, t3, t4, t5}

Page 5: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

5

Ghost finite state machine diagram

Three possible states: roam, evade, chase

Four transition functions: see true, see false, blue true, blue false

Roam(Initial state)

EvadeChase

seetrue

seefalse

seefalseblue

true

seetrue

blue true

seefalse

seetrue

blue true

See true:

Ghost see the player

Blue true:

The player eats a power pill

Page 6: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

6

Model Ghost Behavior (1)

Roam

blue = true?

YesEvade

No

seePlayer?Yes

Chase

No

Page 7: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

7

Model Ghost Behavior (2&3)

Current state: Chase

Chase

blue = true?

Yes Evade

No

seePlayer?

Yes

Roam

NoChase

blue = true?

Yes

Evade

No

seePlayer?Yes

Roam

No

Current state: Roam

Page 8: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

8

Finite State Machine to describe Avatar behaviors

Page 9: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

9

Network or Hierarchical of FSM to describe the logic of the whole game

Uncertainty by random numbers

Attack orRetreat?

Page 10: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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?

Page 11: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

11

Pathfinding

Problem Dependent Destination: moving or stationary? Obstacles? Terrain shape Shortest path Aim: move around or reach a

destination?

Page 12: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

12

Outline

Basic Pathfinding Random Movement and Obstacle

Avoidance Tracing Around Obstacles Breadcrumb Pathfinding Path Following Wall Tracing Waypoint Navigation

Page 13: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

13

Basic Pathfinding

Simple path movement

Unnatural-looking path

Line-of-Sight path movement

More natural-looking path

Starting point

Destination

Starting point

Destination

Page 14: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

14

Basic Pathfinding-Problems with Obstacles

Obstacles

The function of obstacle avoidance is necessary element in pathfinding

Page 15: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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?

YesPlayer’s position

Page 16: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 17: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

17

Basic Tracing - when to stop tracing?

Follow the edge of the obstacle in an attempt to work way around it

Destination

Page 18: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 19: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

19

Tracing with Line of Sight Algorithm

Page 20: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 21: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

21

Breadcrumb Trail

Recording

the player positions

Dropping breadcrumbs

Finding and Following

the breadcrumbs

Record 15 steps

Page 22: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

22

Detect and Follow Breadcrumb Trail

Are neighbors are breadcrumbs?

YesFollow 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.

Page 23: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

23

Path Following-Containment Problem

Starting point

Destination

Pathfinding

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

Page 24: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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.

Page 25: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

12

3

4

56

7

8

Page 26: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

26

Weighting Directions

12

3

4

56

7

8

Current direction

(+2)

(+1) (+

0)

(+0)

(-1)(+0)

(+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

Page 27: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 28: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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,…

Page 29: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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.

Page 30: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

12

3

4

56

7

8

Left

Back

Straight

Right

Directions in view of players

Page 31: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

31

Left-handed Strategy

Straight?

Possible Next Step

Impossible

Left?Possible Next Step

Impossible

Right?possible Next Step

Impossible

Back? Next Step

Page 32: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 33: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

33

Waypoint Navigation

Pre-calculate Paths Reduce Pathfinding Time

and CPU Load Method

Placing some nodes in the game environment

Finding paths between nodes

Page 34: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 35: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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?

Page 36: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

36

Building a Path

Node Table Establish the

connections between nodes

Find a Shortest Path between Two Nodes

- B B B B B B

- -

-

-

-

-

A B C D E F G

A

B

C

D

E

F

G

Page 37: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

37

Completed Node Table

- B B B B B B

A - C C C C C

B B - D E E E

C C C - C C C

C C C C - F F

E E E E E - G

F F F F F F -

A B C D E F GA

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

Page 38: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

38

Finding the Path

Goal B G

B -> G Table intersection ->C Move ->CC -> G Table intersection ->E Move ->EE -> G Table intersection ->F Move ->FF -> G Table intersection ->G Move ->G

B -> C -> E -> F -> G

Page 39: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 40: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 41: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 42: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 43: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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;

Page 44: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

44

Search Scenario

A tiled search area

? ? ?

? ?

? ? ?

Adjacent tiles to check

Obstacle area

Starting point Destination

Page 45: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

45

After Checking all adjacent tiles Add to closed list

Building a link

open

open

openopen openopenopen

open

open

open

openopen

open

open openopen

Searching Process

Page 46: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

46

Scoring Each Tile

Starting point

Given tile

Destination

cost1 cost2

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

Page 47: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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.

openopenopen

open

open

open

openopenc = 1 + 4 = 5

Minimum {c} = 4

Page 48: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

48

Examining the Tiles with Minimum Cost

openopenclosed

open

open

open

openopen

open

open

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.

Page 49: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

49

Spread to Adjacent Tiles

closed

closed

closed

closed

closed

closed

closed

closed

closed

open

open

openopen

open

open

Current tile

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

open

Current tile

open open

open

Page 50: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

50

Completed Path

closedclosedclosed

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

closed

open

open

closed

closed

closed

closed

closed

The path is found when the destination is added in the open list.

Page 51: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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?

Page 52: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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

Page 53: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

53

Types of Terrains and Costs

Open Terrain

Cost = 1

Grassland Cost = 3

Swampland

Cost = 5

Other terrains: hills, rivers, …

Previously, the cost of moving from one node to another was always 1.

Page 54: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

54

Finding Lowest-Cost Path

Orig

inal

path

The lowest-cost path is not necessarily the shortest path.

Lowest cost

Shortest

path

Page 55: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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.

Page 56: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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.

Page 57: 1 AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding.

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).


Recommended