Artificial Intelligence in
Game Design
Movement in Games
• Determine desired direction of movement of
character in some space
– Can include more complex components
• Speed
• Orientation • Acceleration
• Meant to meet some goal
– Pursuing player – Fleeing player
– Moving through obstacles – Wandering/patrolling
Types of Spaces
• Usually two dimensional
– Most games involve “ground”
– Screen inherently 2D
• Can still use angles to appear 3D
• 2 ½ dimensional space
Basic Game Physics
• Representation:
– Position: X, Y
– Orientation: ω
Y X
Basic Game Physics
• Velocity and rotation
– velX
– velY
– velω
• Forces to change velocity
– Steering controlled by player – steerX
– steerY
– steerω
“speed” = sqrt(velX2 + velY2)
Basic Game Physics
• Simple update algorithm:
Update(steerX, steerY, steerω, time) {
X = X + velX * time
• Note that not perfect physics, but “close enough” for fast frame rate
Limitations in Game Physics
• Only accelerate in current direction of steering (facing)
– Direction of drive wheels in car – Direction of engines in rocket
• Single accel component player controls
– Gas pedal, etc.
• steerX = accel * cos(ω) steerY = accel * sin(ω)
• Will need to change ω
to achieve direction
– Target ω = atan(targetY Y targetX X)
targetX X
Limitations in Game Physics
• Maximums on velocity and/or steering
– Max speed at which character can run – Max gas flow/steering wheel turn in car
• Rotation/spin set to maximum if exceed
– If (vel ω > maxvel ω) vel ω = maxvel ω
• Directional maximums usually for total in both X, Y
– speed = sqrt(velX2 + velY2) <= maxspeed
• Must normalize both to maximum
– velX = maxspeed * cos(ω) – velY = maxspeed * sin(ω)
velY speed
velX maxspeed
Limitations in Game Physics
• Drag slows velocities each frame
– Atmospheric drag – Friction with ground – Underwater motion
• Can implement as drag coefficient
– drag between 0 and 1
– speed = speed * (1 – drag)
– Normalize velX, velY based on adjusted speed
velY
velX ω
Limitations in Game Physics
• Limits imposed by animation
– Fixed number of motion animations rendered for “legged” characters
walk stride
Limitations in Game Physics
• May map actual velocities to those allowed by animation
– In worst case define:
Steering Behaviors
• Define steering components for NPC
– Determined in terms of goal of NPC – Usually very simple
– Can combine to create complex emergent behavior
• Examples:
– Seek – Flee – Arrive – Align – Pursue – Evade – Wander – Avoid
Steering Behaviors
Basic form of character motion:
• Use steering behaviors to determine desired steerX, steerY, steerω
• May need to combine multiple behaviors
– Seek food – Flee predator
• Normalize to maxX, maxY, maxω if necessary
• Apply update method to change positions and velocities • Apply other constraints as necessary
– Drag
Seek Behavior
• Goal:
Move to some specific location as fast as possible
– Goal location: (targetX, targetY)
– May be location of character or other objective
steerX = targetX – X
steerY = targetY – Y
– Probably larger than maximum acceleration, will need to normalize
steerX
Flee Behavior
• Goal:
Move away from some location as fast as possible
– Opposite of Seek
steerX = X – targetX
steerY = Y – targetY
steerX
steerY
targetX X
Arrive Behavior
• Problem with using seek to stop at location:
– Usually at maximum velocity when reach location – Will overshoot (possibly several times)
• Must decelerate when near location
accelerate
Arrive Behavior
• Decelerate within some “slow radius” of target
• Idea:
Target velocity should decrease with distance to target
– distance = sqrt((targetX – X)2 + (targetY – Y)2)
targetSpeed = maxSpeed * distance / slowRadius
accelerate
Arrive Behavior
• Normalize to get target velocity in each dimension based on angle Ө to target
Ө = atan( ( targetY – Y) / ( targetX X))
targetVelX = targetSpeed * cos(Ө) targetVelY = targetSpeed * sin(Ө)
• If target moving will need to adjust to match speed
targetVelX = targetVelX – locationVelX
targetVelY = targetVelY – locationVelY
targetSpeed targetVelX
targetVelY
Arrive Behavior
• Problem: will never reach exact target point
– Movements not precise enough – Will just “hover” forever
• Solution: define small “close enough” targetRadius – within targetRadius steerX = 0
steerY = 0
Arrive Behavior
Arrive( targetX, targetY, LocationVelX, LocationVelY) { distance = sqrt((targetX – X)2 + (targetY – Y)2)
if (distance <= targetRadius) { speedX = 0; speedY = 0;
}
else if (distance <= slowRadius) {
targetSpeed = maxSpeed * distance / slowRadius;
compute targetVelX, targetVelY components
adjust targetVelX, targetVelY to locationVelX, locationVelY
MatchVelocity(targetVelX, targetVelY); }
else {
MatchVelocities Behavior
Set steering to achieve desired velocities as fast
as possible
steerX = targetVelX – velX
steerY = targetVelY – velY
May need several steps if more than maximum acceleration!
Align Behavior
• Set orientation to some given targetω
• Like Arrive for orientation
– Define some slowAngle at which to slow rotation – Define some targetAngle at which to stop
– Spin as fast as possible outside slowAngle
targetω targetAngle
Pursue Behavior
•
Seek
does not work well for moving targets
Seek moves to current location of target and must readjust each frame
But target has moved away from target location
Pursue Behavior
• Compute distance to target
distance = sqrt((targetX – X)2 + (targetY – Y)2)
• Estimate time to target based on current speed
timeToTarget = distance / sqrt(velX2 + velY2)
• Estimate location of target after that time based on its current speed
actualTargetX = targetX + velTargetX * timeToTarget actualTargetY = targetY + velTargetY * timeToTarget
• Move to that location as fast as possible
Seek (actualTargetX, actualTargetY) or
Pursue Behavior
• This is quick estimate of complex computation
– Time to intercept is just rough estimate
Evade Behavior
• Same idea as Pursue
– Anticipate where pursuer will be
• Current location • Current speed
– Flee from that location
Wander Behavior
• Goal: Have character “wander around” at “random”
• Problem: Moving in random direction each frame just causes character to ”hover” in same area
Wander Behavior
At each move:
• Place circle at some distance ahead of character
– Good distance = maxSpeed
– Radius of circle = maximum turn in frame