Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other...
-
Upload
elwin-johnston -
Category
Documents
-
view
215 -
download
2
Transcript of Chapter 1 Introduction to Game AI 2012/03/22 1. Defining AI “The ability of a computer or other...
Chapter 1Introduction to Game AI
2012/03/22
1
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
Established Game AI
• Cheating
• Finite state machines
• Fuzzy login
• Effective and efficient pathfinding
• Scripting, rules-based system
3
The future
• The game should evolve, learn, and adapt the more it's played
• Decision trees, neural networks, genetic algorithms, and probabilistic methods
4
Chapter 2Chasing and Evading
5
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
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
Basic Chasing
if (predatorX > preyX) predatorX--; else if (predatorX < preyX)
predatorX++;
if (predatorY > preyY) predatorY--;
else if (predatorY < preyY) predatorY++;
8
Basic Evading
if (predatorX > preyX) predatorX++; else if (predatorX < preyX)
predatorX--;
if (predatorY > preyY) predatorY++;
else if (predatorY < preyY) predatorY--;
9
Basic tile-based chase example
if (predatorCol > preyCol) predatorCol--;
else if (predatorCol < preyCol) predatorCol++;
if (predatorRow> preyRow) predatorRow--;
else if (predatorRow<preyRow) predatorRow++;
10
Basic tile-based chase
11
Line-of-Sight Chasing
12
Bresenham's algorithm
• One of the more efficient methods for drawing a line in a pixel-based environment
13
14
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
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
Global & Local Coordinate Systems
17
Steering force test
18
Intercepting
19
DoIntercept
Vr = Prey.vVelocity - Predator.vVelocity;
Sr = Prey.vPosition - Predator.vPosition;
tc = Sr.Magnitude() / Vr.Magnitude();
St = Prey.vPosition + (Prey.vVelocity * tc);
20
21
22
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
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
3. Pattern Movement
25
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
Control instructions
ControlData { double turnRight;
double turnLeft;
double stepForward;
double stepBackward;
};
27
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
Pattern Movement in Tiled Environments
• Paths will be made up of line segments. Each new segment will begin where the previous one ended.
29
Rectangular pattern movement
30
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
Complex tile pattern movement
32
Track pattern movement(add random factor)
33
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
Control Structures
struct ControlData { bool PThrusterActive;
bool SThrusterActive; double dHeadingLimit; double dPositionLimit; bool LimitHeadingChange; bool LimitPositionChange;
};
35
Square path
36
Zigzag path
37
4. Flocking
38
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
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
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
Neighbors
42
Wide versus narrow field-of-view flock formations
43
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
Cohesion
45
Alignment
• each unit should steer so as to try to assume a heading equal to the average heading of its neighbors
46
Separation
47
Obstacle Avoidance
• The units to see ahead of them and then apply appropriate steering forces to avoid obstacles in their paths
48
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
5. Potential Function-Based Movement
50
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
Potential Function
52
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
Potential chase and evade
• (A)
• (B) A• (C) A• (D) B
54
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
Obstacle Avoidance
56
Swarming
• calculate the Lenard-Jones force between each unit in the swarm
57
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
6. Basic Pathfinding and Waypoints
59
Basic Pathfinding
• Chapter 2
60
Obstacle Avoidance
• Random Movement Obstacle Avoidance– works particularly well in an environment with
relatively few obstacles 61
6.2.2 Tracing Around Obstacles
62
63
6.2 Breadcrumb Pathfinding
64
65
66
6.3 Path Following
67
68
69
6.4 Wall Tracing
70
71
6.5 Waypoint Navigation
72
73
74
75