Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other...

75
Chapter 1 Introduction to Game AI 2012/03/22 1

Transcript of Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other...

Page 1: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Chapter 1Introduction to Game AI

2012/03/22

1

Page 2: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Defining AI

• “The ability of a computer or other machine to perform those activities that are normally thought to require intelligence”.

• Game AI techniques : deterministic and nondeterministic – Deterministic behavior or performance is specified

and predictable – Nondeterministic behavior is the opposite of

deterministic behavior. Behavior has a degree of uncertainty and is somewhat unpredictable

2

Page 3: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Established Game AI

• Cheating

• Finite state machines

• Fuzzy login

• Effective and efficient pathfinding

• Scripting, rules-based system

3

Page 4: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

The future

• The game should evolve, learn, and adapt the more it's played

• Decision trees, neural networks, genetic algorithms, and probabilistic methods

4

Page 5: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Chapter 2Chasing and Evading

5

Page 6: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Application

• A spaceship shooter, a strategy simulation, or a role-playing game, video.

• Make your game's non-player characters (NPC) either chase down or run from your player character

6

Page 7: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Outline

• Basic Chasing and Evading

• Line-of-Sight Chasing

• Line-of-Sight Chasing in Tiled Environments

• Line-of-Sight Chasing in Continuous Environments

• Intercepting

7

Page 8: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Basic Chasing

if (predatorX > preyX) predatorX--; else if (predatorX < preyX)

predatorX++;

if (predatorY > preyY) predatorY--;

else if (predatorY < preyY) predatorY++;

8

Page 9: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Basic Evading

if (predatorX > preyX) predatorX++; else if (predatorX < preyX)

predatorX--;

if (predatorY > preyY) predatorY++;

else if (predatorY < preyY) predatorY--;

9

Page 10: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Basic tile-based chase example

if (predatorCol > preyCol) predatorCol--;

else if (predatorCol < preyCol) predatorCol++;

if (predatorRow> preyRow) predatorRow--;

else if (predatorRow<preyRow) predatorRow++;

10

Page 11: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Basic tile-based chase

11

Page 12: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Line-of-Sight Chasing

12

Page 13: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Bresenham's algorithm

• One of the more efficient methods for drawing a line in a pixel-based environment

13

Page 14: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

14

Page 15: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Line-of-Sight Chasing in Continuous Environments

• game entities—airplanes, spaceships, hovercraft, etc.—are driven by applied forces and torques.

• Physics for Game Developers (O'Reilly).

• The player controls his vehicle by applying thrust for forward motion and steering forces for turning.

15

Page 16: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

u = VRotate2D(-Predator.fOrientation, (Prey.vPosition - Predator.vPosition));

u.Normalize();

if (u.x < -_TOL) left = true;

else if (u.x > _TOL) right = true;

Predator.SetThrusters(left, right); 16

Page 17: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Global & Local Coordinate Systems

17

Page 18: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Steering force test

18

Page 19: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Intercepting

19

Page 20: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

DoIntercept

Vr = Prey.vVelocity - Predator.vVelocity;

Sr = Prey.vPosition - Predator.vPosition;

tc = Sr.Magnitude() / Vr.Magnitude();

St = Prey.vPosition + (Prey.vVelocity * tc);

20

Page 21: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

21

Page 22: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

22

Page 23: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

General Considerations

• This function should be called every time through the game loop or physics engine loop

• so that the predator constantly updates the predicted interception point and its own trajectory

23

Page 24: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

General Considerations

• Sometimes interceptions are not possible

• For example, if the predator is slower than the prey and if the predator somehow ends up behind the prey

24

Page 25: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

3. Pattern Movement

25

Page 26: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Pattern Movement

• The NPCs move according to some predefined pattern that makes it appear as though they are performing complex

• Takes the desired pattern and encodes the control data into an array or set of arrays

26

Page 27: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Control instructions

ControlData { double turnRight;

double turnLeft;

double stepForward;

double stepBackward;

};

27

Page 28: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Pattern initialization• Pattern[0].turnRight = 0; Pattern[0].turnLeft = 0;

Pattern[0].stepForward = 2; Pattern[0].stepBackward = 0; • Pattern[1].turnRight = 0; Pattern[1].turnLeft = 0;

Pattern[1].stepForward = 2; Pattern[1].stepBackward = 0; • Pattern[2].turnRight = 10; Pattern[2].turnLeft = 0;

Pattern[2].stepForward = 0; Pattern[2].stepBackward = 0; • Pattern[3].turnRight = 10; Pattern[3].turnLeft = 0;

Pattern[3].stepForward = 0; Pattern[3].stepBackward = 0; • Pattern[4].turnRight = 0; Pattern[4].turnLeft = 0;

Pattern[4].stepForward = 2; Pattern[4].stepBackward = 0; • Pattern[5].turnRight = 0; Pattern[5].turnLeft = 0;

Pattern[5].stepForward = 2; Pattern[5].stepBackward = 0; • Pattern[6].turnRight = 0; Pattern[6].turnLeft = 10;

Pattern[6].stepForward = 0; Pattern[6].stepBackward = 0;• …

28

Page 29: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Pattern Movement in Tiled Environments

• Paths will be made up of line segments. Each new segment will begin where the previous one ended.

29

Page 30: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Rectangular pattern movement

30

Page 31: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

entityList[1].InitializePathArrays(); entityList[1].BuildPathSegment(10, 3, 18, 3);

entityList[1].BuildPathSegment(18, 3, 18, 12); entityList[1].BuildPathSegment(18, 12, 10, 12); entityList[1].BuildPathSegment(10, 12, 10, 3); entityList[1].NormalizePattern(); entityList[1].patternRowOffset = 5; entityList[1].patternColOffset = 2;

31

Page 32: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Complex tile pattern movement

32

Page 33: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Track pattern movement(add random factor)

33

Page 34: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Pattern Movement in Physically Simulated Environments

• Isn't conducive to forcing a physically simulated object to follow a specific pattern of movement

• Apply appropriate control forces to the object to coax it

• Your input in the form of steering forces and thrust modulation, for example, is processed by the physics engine

34

Page 35: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Control Structures

struct ControlData { bool PThrusterActive;

bool SThrusterActive; double dHeadingLimit; double dPositionLimit; bool LimitHeadingChange; bool LimitPositionChange;

};

35

Page 36: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Square path

36

Page 37: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Zigzag path

37

Page 38: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

4. Flocking

38

Page 39: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Flocking

• Often in video games, NPC must move in cohesive groups rather than independently.

• Craig Reynolds,1987, "Flocks, Herds, and Schools: A Distributed Behavioral Model."

• Collective Intelligence

39

Page 40: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Classic Flocking

• Cohesion – Have each unit steer toward the average

position of its neighbors.

• Alignment – Have each unit steer so as to align itself to the

average heading of its neighbors.

• Separation – Have each unit steer to avoid hitting its

neighbors.

40

Page 41: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Aware of neighbors

• Each unit is aware of its local surroundings• It knows the average location, heading, and

separation between it and the other units in the group in its immediate vicinity.

41

Page 42: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Neighbors

42

Page 43: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Wide versus narrow field-of-view flock formations

43

Page 44: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Flocking Example

• we're going to treat each unit as a rigid body and apply a net steering force at the front end of the unit.

• This net steering force will point in either the starboard or port direction relative to the unit

• Will be the accumulation of steering forces determined by application of each flocking rule

44

Page 45: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Cohesion

45

Page 46: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Alignment

• each unit should steer so as to try to assume a heading equal to the average heading of its neighbors

46

Page 47: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Separation

47

Page 48: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Obstacle Avoidance

• The units to see ahead of them and then apply appropriate steering forces to avoid obstacles in their paths

48

Page 49: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Follow the Leader

• Follow-the-leader rule

• Let some simple rules sort out who should be or could be a leader

• Not leaving the flock leaderless in the event the leader gets destroyed or somehow separated from his flock

• Add other AI to the leaders to make their leading even more intelligent

49

Page 50: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

5. Potential Function-Based Movement

50

Page 51: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

How Can You Use Potential Functions for Game AI?

• Calculate the force between the two units—the computer-controlled unit and the player in this case

• Then apply that force to the front end of the computer-controlled unit, where it essentially acts as a steering force

51

Page 52: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Potential Function

52

Page 53: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Chasing/Evading

• Use the potential function to calculate the force of attraction (or repulsion) between the two units, applying the result as a steering force to the computer-controlled unit

53

Page 54: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Potential chase and evade

• (A)

• (B) A• (C) A• (D) B

54

Page 55: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Obstacle Avoidance

• we set the A parameter, the attraction strength, to 0 to leave only the repulsion component.

• We then can play with the B parameter to adjust the strength of the repulsive force and the m exponent to adjust the attenuation

55

Page 56: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Obstacle Avoidance

56

Page 57: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Swarming

• calculate the Lenard-Jones force between each unit in the swarm

57

Page 58: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Optimization Suggestions

• Complexity: N 2

• not perform the force calculation for objects that are too far away from the unit to have any influence on it

• divide your game domain into a grid containing cells of some prescribed size. – perform calculations only between the unit

and those obstacles contained within that cell and the immediately adjacent cells

58

Page 59: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

6. Basic Pathfinding and Waypoints

59

Page 60: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Basic Pathfinding

• Chapter 2

60

Page 61: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

Obstacle Avoidance

• Random Movement Obstacle Avoidance– works particularly well in an environment with

relatively few obstacles 61

Page 62: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

6.2.2 Tracing Around Obstacles

62

Page 63: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

63

Page 64: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

6.2 Breadcrumb Pathfinding

64

Page 65: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

65

Page 66: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

66

Page 67: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

6.3 Path Following

67

Page 68: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

68

Page 69: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

69

Page 70: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

6.4 Wall Tracing

70

Page 71: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

71

Page 72: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

6.5 Waypoint Navigation

72

Page 73: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

73

Page 74: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

74

Page 75: Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other machine to perform those activities that are normally.

75