You are on page 1of 20

7.2.

AI ENGINE AND STEERING


BEHAVIOUR I
Design of an AI Engine and introduction to steering in game AI

GAME AI ENGINE
Design of a game Artificial Intelligence Engine

In simple games, AI
tends to be bespoke and
individually written for
each character (e.g.
embedded within the
layer/object update
method).

A Game Artificial Intelligence


Engine

In more complex games


there is a need to have a
set of general AI
routines that can be
controlled by level
designers, etc. There is
often a need to manage
Aside: Unless you explicitly
CPU/memory
wish to do so, you do not
constraints.
need to define a separate AI
engine in your relatively

The AI gets some time


to perform / progress
its routines.

Example structure of an AI Engine

The output of the AI


engine is turned into
actions that update the
Aside: Not all games need all
game
state.
types of AI, e.g. Board games
may only need strategic AI,
whilst a scrolling shooter may

AI obtains world information

The AI engine can


query the world to
obtain information.

Execution
Execution
Management
Management

W
W
or
or
ld
ld
In
In
te
te
rfrf
aa
cc
ee

Higher-level AI applies
to groups, whilst lowerlevel AI operates on
individual game
objects.

AI receives processor time

te
SSt tr raa t e
ggyy
u
( (GGr rooI )u
pp AA I )

DDeecci si si o
io
nn
MMaakki n
i ngg
( (CChhaar r
AAI )I )

Animati
Animati
on
on

MMoovveem
meenn
tt
((CChhaarraacctt
eerr AAI I))

Physics
Physics

...
...

AI output is turned into action

There are a range of


movement algorithms,
from very simple, to
very complex.

W
oW
ro
lr
dl
d

The movement
component contains a
range of algorithms
that make decisions
about motion.

I
nI
tn
et
re
fr
af
ca
ec
e

AI Engine (movement)

Execution
Execution
Management
Management

SSt t
r ar a
t et eM o
o
ggy yvM
e

ve
mmee
nnt
t

Ani
Ani Phy ...
mati
Phy
mati sics
...
on
sics
on

I
nI
tn
et
re
fr
af
ca
ec
e

Each game character


will typically have a
range of different
behaviours that can
be performed. The
decision making
process determines
which behaviour is
best at a given point
in time.

AI Engine (decision making)


W
oW
ro
lr
dl
d

Selected behaviours
are then translated
into action (possibly
making use of
movement AI, or
simply triggering an
animation).

Execution
Execution
Management
Management

SSt t
r ar a
t et eM o
o
ggy yvM
e

ve
mmee
nnt
t

Ani
Ani Phy ...
mati
Phy
mati sics
...
on
sics
on

In other words,
strategic AI controls/
influences the
behaviour of a group
of characters, often
devolving the
execution of the
group behaviour to
individual decision
making / movement
algorithms.

W
oW
ro
lr
dl
d

AI Engine (strategy)

I
nI
tn
et
re
fr
af
ca
ec
e

In order to coordinate
the behaviour of
multiple game
objects some form of
strategic AI is often
needed.

Execution
Execution
Management
Management

SSt t
r ar a
t et eM o
o
ggy yvM
e

ve
mmee
nnt
t

Ani
Ani Phy ...
mati
Phy
mati sics
...
on
sics
on

Execution
ExecutionManagement
Management
SSt rt ar at t
eeggy y

W
W
orl
orl
d
d
Int
Int
erf
erf
ac
ac
e
e

MMov
o vem
em
en
t
ent

Animation
Animation

Physics
Physics

...
...

INTRODUCTION TO MOVEMENT
Introduction to the different forms of movement AI

The aim of movement AI is to sensibly


move game objects around the level.

Introduction to movement AI

All movement algorithms take as input


data about the state of the world and
output geometric data about the
desired form of movement.

Some algorithms only require the


objects position and a target position.
Others algorithms require lots of
interaction with objects (e.g. collision
avoidance, etc.).
Some algorithms directly output a new
velocity (termed kinematic
movement), others output an
acceleration/force used to update the
objects velocity (termed dynamic or
steering behaviours)

All game objects can be


defined as having a
position and an
orientation.

Introduction to movement
AI
Steering algorithms
output
an acceleration (or force)
(kinematics)
In
some game types a
movement algorithm can
directly update the
position/orientation (e.g.
tile-based). However, this
will look unrealistic in
other types of game (e.g.
driving).
In order to permit
continuous (2D)
movement it is necessary
Vector
position
tofloat
store:
orientation;
Vector
velocity;
float rotation;

applied to directional or
rotational velocities. Using
the Newton Euler
equations, the variables
can be updated as follows:
velocity += acceleration * time_delta
rotation += angular_acc * time_delta
position += velocity * time_delta
orientation += rotation * time_delta
Aside: In most 3D games,
characters are usually under
the influence of gravity, with
movement effectively

KINEMATIC MOVEMENT
ALGORITHMS
Basic forms of kinematic movement algorithm
Based upon
Artificial Intelligence for Games

Kinematic movement
algorithms operate using
positions and orientations. The
output is a target velocity
(speed + orientation).

Kinematic movement algorithms

The speed may simply vary


between full speed and
stationary, i.e. kinematic
algorithms do not use
acceleration.
This section will explore the
following basic forms of
kinematic
Seek() movement algorithm:
Flee()
Arrive()

To do:
r if
Conside
le
applicab

Seek takes as input a current


and target location. The
algorithm calculates the
direction from the current to
the target location and
defines a matching velocity.

Seek

The velocity can be used to


define the output orientation
if needed.
Seek ( Vector source, Vector target,
float maxSpeed ) { normalise()
normalise() will
will return
return

Target velocity

DetermineOrientation(
Vector velocity, float currentOrientation ) {

vector
vector of
of unit
unit length
length
and
and same
same direction
direction

if( velocity.length() == 0 )
return currentOrientation;
else
return Math.atan2( -velocity.x,
velocity.y)

Vector velocity
= (target source).normalise()
* maxSpeed;
return velocity;
}

Current velocity

See
See common
common on
on next
next slide
slide for
for atan
atan

Flee

Flee is the opposite of


Seek, i.e. the object
moves away from their
target. It can simply be
defined as the opposite
of the velocity returned
by Seek, i.e:
Flee( Vector source, Vector target,
float maxSpeed ) {
Vector velocity
= (source target).normalise()
* maxSpeed;
return velocity;

Aside: Why atan2 on last


slide?
atan2 computes the
arctangent of y/x in a range
of (, ), i.e. it determines
the counter clockwise angle
(radians) between the xaxis and the vector <x,y>
in 2D Euclidean space. The
normal atan function
returns a range of (/2,
/2)

}
This is useful to find the
direction from one point to

A problem with Seek is


that it can keep
overshooting the target,
never reaching it. One
means to overcome this is
to provide a buffer close
enough region around the
target. Another is to
reduce the speed as the
target comes close. Both
approaches can be
combined as
follows:

Arrive

vel= max * 0.4


vel= max * 0.6
vel= max * 0.75
vel= max

Aside: As with seek, etc., the


returned velocity can be used to
provide the objects orientation if
desired.

Arrive ( Vector source, Vector target,


float maxSpeed, float nearRadius ) {
float slowingFactor = 0.2;

slowingFactor
slowingFactor ==
slowing
slowing strength
strength

Vector velocity = [0,0,...];


Vector separation = (target source);

if( separation.length() < nearRadius )


return velocity;Return initial velocity = 0.0

Return initial velocity = 0.0

velocity = separation / slowingFactor;


if( velocity.length() > maxSpeed )
velocity =
velocity.normalise() *
maxSpeed;

Closeness
threshold

return velocity;

vel = max

Determine
Determine velocity,
velocity, and
and
cap
cap at
at max
max speed
speed ifif
needed
needed

STEERING MOVEMENT
ALGORITHMS
Forms of dynamic (or steering) movement algorithm
Based upon
Artificial Intelligence for Games

Steering movement algorithms


Steering behaviours extend
the kinematic movement
algorithms by determining
acceleration (both forward
movement and rotation)
In many game types (e.g.
driving games) steering
algorithms are often used. In
other games, they may not be
useful.
We
will consider
following
Pursue() the
Face()
Seek()
forms
behaviour:
Evade()
Separate()
Flee() of steering
Interpose() PathFollow()
Arrive()
Align()
AvoidObstacle()
Wander()
Jump()

To do:
r if
Conside
le
applicab

Matching a target property

Basic steering algorithms operate by


trying to match some kinematic
property of the target to the source,
e.g. this might be the targets position,
velocity, orientation, etc. Matching
steering algorithms take source and
target kinematic properties as input.
More advanced steering behaviours try
to match a combination of properties,
potentially with additional constraints.
Typically for each matching behaviour
there is a readily defined opposite
behaviour (e.g. Seek vs. Flee, etc.).
Flee path

Seek path

Thinking about movement.


The next lecture will
consider the steering
behaviours in detail.
As part of completing the
Question Clinic for this
week, please do think about
the role of AI (including
movement based AI) in your
game and identify current
areas of uncertainty.

To do:
Th i n k
ab o u t
nt
moveme

Summary
Today we
explored:
The design of

an AI engine
Kinematic

forms of
movement AI
Introduction to

steering forms
of movement
AI

To do:

ion
t
s
e
u
Q
e
t
le
p
Com
Clinic
e
m
a
g
r
u
o
y
e
n
fi
Iterate/re
I
A
e
n
fi
e
d
o
t
n
desig
n e ed s
at
h
w
n
la
p
o
t
e
Continu
or the
f
o
d
o
t
e
p
o
h
you
Alpha handin

You might also like