8.1. STEERING BEHAVIOUR IIISteering behaviours in game AI
STEERING MOVEMENT ALGORITHMSForms of dynamic (or steering) movement algorithm
Execution Management
World
Interface
Movement
S t r a te g y
Animation Physics ...
Separate( Vector source, Array targets,float separateThreshold, float separateDecay,float maxAcceleration ) {
Vector acceleration = [0,...];foreach( Object target in targets ) {
Vector direction = target.position - source;float distance = direction.length();
if( distance < separateThreshold ) {float separateStrength =
min( separateDecay * distance * distance,
maxAcceleration );acceleration +=
direction.normalise() * separateStrength;
}
if( acceleration.length() > maxAcceleration )acceleration =
acceleration.normalise() * maxAcceleration;
return acceleration;
SeparateThe separate behaviour tries to stop game objects from becoming too crowded. An accelerative force is introduced to move away from any object that is too close.
Aside: Separate is also known as the Repulsion
Steering behaviour
Net acceleration
Individual repulsion
Individual repulsion
targets holds all Objects to consider for separation
separateThreshold controls separation distance drop-off
Determine separation strength
Update net acceleration
Path followingFollowPath will attempt to steer a object along some defined path. It is a delegating behaviour, i.e. a target position is calculated on which to Seek.
Aside: Arrive may be used to navigate to the final point (if the
path does not loop).
Seek output
Closest path point
The first step is to determine the object’s nearest location on the path (this may be hard). Next, a target location further along the path is determined and used as the Seek target.
Target path point
Path following (non-predictive)
The basic delegating behaviour can be defined as:
FollowPath( Object source, Path path, float pathOffset ) {
float pathPosition = path.getPathPosition(source.position);
float targetPathPosition = pathPosition
+ pathOffset;Vector target =
path.getPosition( targetPathPosition );
return Seek( source.position, target );}
path holds the Path objectpathOffset controls how far along the path the target position will be setDetermine the current
position on the path
Move along the path and determine the world position of the new path position Aside: It may be useful to
remember and pass the last path position, i.e.
getPathPosition( Vector currentPosition, float lastPathPosition) as this can help to determine the closest
path position.
Path following (predictive)Predictive path following operates by firstly predicting where the object will be in a short time and then mapping this onto the path.
This can result in smoother path following, but can introduce skipping when the path points are close together.
In order to reduce skipping the last path location can be used to help determine the correct position (based on the principle of coherence)
FollowPath( Object source, Path path, float pathOffset, float lookAheadTime,float lastPathPosition )
Vector futurePosition = source.Position + source.velocity * lookAheadTime;
float pathPosition = path.getPathPosition(futurePosition, lastPathPosition);
float targetPathPosition = pathPosition + pathOffset;
Vector target = path.getPosition( targetPathPosition );
return Seek( source.position, target );
lookAheadTime controls how far ahead the position will be predicted
Predict the future location
lastPathPosition holds the last determined path position
Path following (predictive problems)
The example below shows a potential problem with predictive path-following.
By using the last path position and the principle of coherence the skip can be avoided.
Seek output
Predicted location Aside: Enforcing
coherence is not always desirable (e.g. the object
have may ‘teleported’,
etc.).
Last path position
Coherent path locations
Path following (paths)
Different means of defining a path can be used (including parametric curve functions).
A list of points provides a straightforward and effective path representation. Point spacing can be varied using the degree of curvature.
The integer path position is defined as the index of the closest point to the source.
An array of point-N-to-point-N+1 distances can be maintained to test for coherence.
Array<Point> pathPoints;
Collision Avoidance
Where there is a lot of independent game objects moving in the same space it will likely be necessary to provide some form of collision avoidance.
One approach to collision avoidance is to trigger an evade or separate steering behaviour if a predicted collision is detected.
Collision Avoidance (collision detection)
A future collision can be detected by determining the future path of two objects based on current velocities.
The closest distance of approach can be calculated and action triggered if the distance is less than some threshold (e.g. combined bounding radii × comfort factor)
Object 1Object 2
Point of closest approach Point of
closest approachClosest
distance
Note: The point of closest approach may not be where the trajectories cross as different object velocities may mean objects reach an intersection point at different times.
Collision Avoidance (collision detection)The time of closest
approach can be calculated as:
The points of closest approach are then given by:
Object 1Object 2
v1v2p1
p2
dp
dv
Note: If the time of closest approach is negative then the objects are moving away from each other, and no future action is needed.
Collision Avoidance (detection response)
For best results, if a possible collision has been detected, then the predicted future locations are used to derive the evade or separate behaviour (and not the current positions)
For multiple predicted collisions, the object with the closest predicted time of collision should be reacted to first.Note: If the point of collision will occur at the centre of both objects (unlikely, but maybe possible) then evade, etc. will be unable to operate. A special case would be needed to handle this situation, e.g. evading on the current positions, etc.
Collision AvoidanceCollisionAvoidance(
Object source, Array targets) {
Object closestTarget = null;float closestTime = float.max;float closestSep, closestDis;
// Determine closest target
if( closestTarget == null ) return;if( closestSep <= 0 || closestDis <
source.radius + closestTarget.radius )return Evade( source.position,...);
else {return Evade( source.position +
source.velocity * closestTime, ... );
}
targets holds all Objects to consider for avoidance
If already in collision, or exact centre hit then steer away from current position, else use predicted location.
foreach( Object target in targets ) {Vector relPos =
target.position – source.position;Vector relVel =
target.velocity – source.velocity;
float relSpeed = relVel.length();float relDistance = relPos.length();
float timeToCollide = Math.dot(relPos, relVel)
/ (relSpeed * relSpeed );
float minSep = relDistance – relSpeed *
closestTime;if( minSep < source.radius + target.radius )
if( timeToCollide > 0 ANDtimeToCollide <
closestTime ) { // Store closestTarget,
closestTime, ... }}
Determine time to collide using provided formula
If collision detected and closest so far, then store
Collision Avoidance (walls and large objects)
The previous algorithm assumes a circular/spherical bound (applicable to lots of objects).
For large irregular objects or rectangular objects (e.g. walls) a bounding sphere can offer a poor bound (a large volume around the object will be needlessly avoided).
Collision Avoidance (walls and large objects)
A suitable approach for walls, etc. involves casting one or more rays in the direction of travel. The rays will have a defined maximum length.
If the rays collide with an object then a movement target is created to avoid the collision, and a Seek made towards the target.
Cast ray
Point of collisionNormal to collision
Seek target to avoid collision
ObstacleAvoidance( Object source, CollisionDetector detector ) {
float avoidDistance;float lookaheadDistance;
Vector ray = source.velocity;ray.normalize();ray *= lookaheadDistance;
CollisionInfo collision = detector.getCollision(source.position, ray)
if( collision == null ) return;
Vector targetPosition = collision.position + collision.normal * avoidDistance;
return Seek( source.position, targetPosition )
A CollisionDetector is assumed to be an object that has awareness of the level geometry and can test for object/ray intersection – returning at least an intersection point and normal vector.
Collision Avoidance (walls and large objects)
Note: Developing a fast and efficient collision detection system can be a sizeable undertaking. For more information see: http://en.wikipedia.org/wiki/Collision_detection
http://www.realtimerendering.com/intersections.html
CollisionInfo is assumed to hold information on any detected collision(s).
Collision Avoidance (walls and large objects)
A single cast ray may not avoid an obstacle as shown opposite.
Other common configurations include:• Parallel side rays – good
for tight passages, susceptible to the corner trap.
• Central ray with short whiskers – need to cast three rays, not good for very tight passages.
• Whiskers only – ‘blind spot’ close to object, not good for tight passages
Collision not detected
Corner trap – collision avoidance cancels out
Parallel side rays
Central ray, side whiskers
Whiskers
Constrained movement (motor control)
Steering behaviours provide movement requests, i.e. acceleration vector.
Some objects may not be able to immediately realise the request, e.g. a car can only accelerate forward/backward (with different rates) and has a turning rate that varies with movement speed, i.e. different objects can have different behaviours.
In order to realise movement requests, a motor control layer can determine how best to execute a request, a process known as actuation.
Constrained movement (filtering)The simplest actuation approach is to remove all components of the steering output that cannot be currently realised.
Running the filtering algorithm every update tick (the steering output may remain constant) the object will move to match the steering target.
This approach is easily implemented, fast and reasonably effective, however, it can result in much smaller than requested accelerations (which can be detected and avoided if desired).
Requested linear
acceleration
Requested angular
acceleration
Achievable linear and angular acceleration
Constrained movement (cars and bikes)
Vehicular movement can be constrained using decision arcs.
A forward arc in which the vehicle will turn towards the target without braking.
A rear arc (with a maximum range) in which the car will reverse towards the target.
The arcs shrink based on movement speed and amount of grip. At low speeds the forward and rear arc must touch (to prevent breaking deadlock at low speed).
If the target lies between the two arcs, then the vehicle will break and turn towards the target.
Front arc
Maximum reversing distance
Rear arc
Front arc
Rear arc
Breaking arc
Breaking arc
Summary
To do:If applicable to your
game, test and explore movement algorithms.
Work towards your alpha-code hand-in
Today we explored:
More advanced steering behaviours including path traversal and collision avoidance
Constrained movement using actuation