52
Games Programming III (TGP2281) – T1, 2010/2011 Movement AI John See 19, 26 Nov 2010

Movement AI

  • Upload
    alina

  • View
    44

  • Download
    0

Embed Size (px)

DESCRIPTION

John See 19, 26 Nov 2010. Movement AI. Movement AI in Millington’s Model. Movement AI vs. Animation. Many games rely solely on some movement AI, and very little advanced decision-making Movement AI vs. Animation overlap - PowerPoint PPT Presentation

Citation preview

Page 1: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Movement AI

John See19, 26 Nov 2010

Page 2: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Movement AI in Millington’s Model

Page 3: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Movement AI vs. Animation

• Many games rely solely on some movement AI, and very little advanced decision-making

• Movement AI vs. Animation overlap• Movement AI – movement of characters around the game

level and NOT movement of limbs/faces/parts etc.

Page 4: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Movement algorithm structure

Page 5: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

2D Movement - Statics

• Characters as Points• Static structure

• Position of character (two linear coordinates in a 2D vector)• Orientation (floating point value in radians)

• 2D movement commonly take place on the x-z plane with y axis fixed to zero

• In standard game engines by default, a character is looking down the z-axis at zero orientation

Page 6: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Kinematics

• Movement (velocity) calculated based on position and orientation alone

• No acceleration involved? A little unrealistic, but works fine for many games. Simpler to implement.

• Kinematic structure• Position of character (2D or 3D vector)• Orientation (floating point value indicating degree of facing)• Velocity (2D or 3D vector indicating speed and direction)• Rotation (floating point value indicating rotation speed)

• Steering structure• Return accelerations (for movement and rotation) to change

velocities of character

Page 7: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Updating Position and Orientation

• High-school physics equations for motion: • s = vt + 0.5at2

• If frame rate high, update time is small, the square is even smaller, contribution of acceleration is negligible

• Common to see the 2nd term removed from the update loop for fast movement updates (less computation too)• s = vt

Page 8: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Kinematic Movement Algorithms

• Use static data (position & orientation, no velocities) to output a desired velocity

• No acceleration used• Abrupt changes in velocity can be smoothed over several

frames to give realistic look• Further simplification: Force orientation of character to be in

the direction it is traveling (without any smooth rotation)

Page 9: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Seek (Kinematic)

• Input: Character’s and Target’s static data (position & orientation)

• Calculates: • Velocity direction (vector) from the character to the target by

subtracting position of character from position of target)• No rotation

• Perform normalization on the direction vector to obtain unit vector, which will then be multiplied with the max speed of character

Page 10: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Flee (Kinematic)

• Simply reverse the calculation of the velocity direction vector, to move away from the target• Calculate from target to character

Page 11: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Arrive (Kinematic)

• SEEK is designed for chasing• If the character seeks a particular location in the world at

constant maximum speed, it is likely to overshoot an exact point, wiggle backward and forward trying to get there.

• Arrive introduces• A radius of satisfaction (to check if the character is nearing location)• A time-to-target value (to slow character down if it is within radius of

satisfaction)

• Inside radius, Velocity = Dist. from location / Time-to-target• This reduces as the character is nearing location

Page 12: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Wander (Kinematic)

• Character meanders randomly (like a random walk) in a forward direction

• Always moves in the direction of the current orientation at maximum speed

• Direction of orientation is randomized here • Take a random number between –1 and 1

(where values around zero are more likely), and multiply by a fixed maximum rotation speed), to get new rotation velocity

Page 13: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Steering Behaviors

• Extend kinematic algorithms by adding velocity and rotation as input – thus characters have acceleration

• 2 categories:• Fundamental steering behaviors• Behaviors built from combination of fundamental behaviors

• Input: Kinematic values of a moving character• Target information can be from another moving character,

collision geometry of the world, or specific path geometry

Page 14: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Variable Matching

• Simplest form of steering behaviors involve matching variables from character with variables from target.

• Matching• Position of target (Seek, Arrive)• Orientation of target (Align)• Velocity of target (Velocity Matching)• Delegation or Combination of various kinematic elements

• There could be opposite behaviors that will intend to “unmatch” as much as possible.

Page 15: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Seek & Flee

• Seek – match position of character with position of target.• Direction vector from character to target.• Velocity/speed of character needs to be clipped from

exceeding its maximum value, since acceleration will cause its speed to grow larger and larger.

• Acceleration is applied to the direction to the target, limited by a maximum value

• Introduce additional drag to prevent orbiting of target

• Flee – Direction vector from target to character

Page 16: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Arrive

• Similar to (Kinematic) Arrive, this (Dynamic) Arrive intends to slow the character down as it approaches the target so that it arrives exactly at the right location

Page 17: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Arrive

• Uses 2 radii• Arrival Radius – lets character get near enough to target w/o

letting small errors keep it in motion• Slowdown Radius – slows character down when it passes into

this radius. Ideal speed is calculated using time-to-target method (like before). Upon entering this radius, speed is maximum. Zero speed when arrive successfully.

• The target velocity (speed) is interpolated using distance from target

• When a character is moving too fast to arrive at right time, target velocity < actual character velocity, acceleration will be negative, or acting to slow it down

Page 18: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Leave

• Opposite behavior to Arrive• No point in implementing – Unlikely to want to accelerate

and build up speed if leaving.• Just using Flee (move at maximum velocity)

Page 19: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Align• Match orientation of the character with that of target• Pays no attention to position/velocity of character/target• Idea: Subtract character orientation from target

orientation, and convert result into range (-π, π) radians• Algorithm is similar to Arrive

Page 20: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Velocity Matching

• Idea: Use acceleration to get to the target velocity• Subtract velocity of character from velocity of target to get

velocity difference• Use time-to-target method to find acceleration/deceleration to be

applied to character

• How is matching velocities useful?

• Also becomes much more useful when combined with other behaviors (e.g. flocking steering behavior)

Page 21: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Delegated Behaviors

• More complex behaviors that make use of the basic fundamental steering behaviors

• Seek, Align and Velocity Matching are the fundamental behaviors that can be used

• Programming Tip: Polymorphic style needed to capture these dependencies

Page 22: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Pursue

• When seeking a moving target, constantly moving towards the target’s current position is not sufficient!

• Going in circles? Inefficient? Look unrealistic?

• Instead of aiming at its current position, how about predicting where it will be at some time in the future, and aim towards that point?

Page 23: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Pursue

• Does not need sophisticated algorithms – Overkill!• Assumption: Target will continue moving with same

velocity as it currently is.

• Work out distance between character and target, and how long it takes to get there

• Use this time interval as prediction time

• Calculates position of target based on the assumption

• Use new position as target for Seek

Page 24: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Evade

• Simply the opposite behavior to Pursue• Instead of delegating to Seek, delegate it to Flee

Page 25: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Face

• Makes character look at its target• Delegates to Align behavior to perform rotation, but

calculates target orientation first• Target orientation generated from relative position of

target to character

Page 26: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Looking Where You’re Going

• To enable character to face in the direction it is moving• Using Align, give the character angular acceleration to

make it face the right way while moving – this method causes gradual facing change (more natural)

• Method of implementation is similar to Face behavior except for target orientation which is calculated using current velocity of character

Page 27: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Wander

• In Kinematic version, direction of character is perturbed by a random amount of rotation each time it was run. Result = Erratic rotation

• This can be smoothen by making orientation of the character indirectly reliant on random numbers.

• OR, think of it as a delegated Seek behavior.

• Idea #1: Constrain the target to a circle around the character

Page 28: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Wander

• Idea #2: Improve it by moving the circle out in front of the character and shrink it down

• Face or Look Where You’re Going behaviors can be used to align the character’s orientation to the direction it is moving

• A maximum “wander rate” can be used to constrain the random numbers to an interval within the previous wander direction – to prevent too much erratic rotation

Page 29: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Path Following

• Steering behavior that takes a whole path as a target• Move along path in one direction• A Delegate behavior:

• Calculates position of target based on current character location and shape of path

• Hands over its target to Seek

Page 30: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Path Following

• 2 stages:• Current character position is mapped to nearest point along path.

Curved paths or paths with many line segments can increase computation complexity.

• Target is selected further along the path than the mapped point by a fixed distance. Seek the target.

Page 31: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Predictive Path Following

• Predictive version:• Predict where the character will be in a short time. • Map this to the nearest point on the path. This is the candidate

target for seeking.• If the new candidate target has not been placed farther along the

path than it was at the last frame, then change to new target.

Page 32: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Predictive Path Following

• Upside: Smoother for complex paths with sudden direction change

• Downside: Cutting-corner behavior – Character may miss a whole section of the path if two sections of a path come close together

Page 33: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

How to Construct Path?

• For ease of use in graphic/rendering systems, paths are normally represented using a single parameter (normally floating-point, constraint to a range) that increases monotonically along the path (can be seen as distance along path)

t=0

t=1

t=?

Page 34: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Separation

• Commonly used for crowd simulations (where number of characters are heading roughly same direction)

• Acts to keep characters from getting too close and crowded.

• Does not work when characters move across each others’ path

• Zero output in terms of movement!

Page 35: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Separation

• Idea: If behavior detects another character closer than some threshold, it acts like “evade” to move away

• Strength of the “evade” movement is related to the distance from the target

• 2 common calculations• Strength = maxAcceleration * (threshold – distance) / threshold• Strength = min(k * distance * distance, maxAcceleration)

For each case, distance: distance between character and nearby neighborthreshold: min distance for separation to occurmaxAcceleration: max acceleration of characterk: strength decay constant

Page 36: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Steering “Family Tree”

Page 37: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Other Delegated Steering Behaviors…

• Collision Avoidance• To avoid collision between various moving characters

• Obstacle/Wall Avoidance• To avoid collision between character and unanimated obstacles

or walls

• Read from textbook

Page 38: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Combining Steering Behaviors

• A moving character – usually needs more than one steering behavior to model it more realistically

• E.g. To seek its goal, avoid collision with others, avoid bumping into walls

• Some special behaviors may require more than one steering behavior to be active at once.

• E.g. To steer in a group towards a goal, maintaining a good separation distance from group members, and to match each members’ velocities

• How?

Page 39: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Combining Steering Behaviors

• Blending• Execute all steering behaviors and combining their results using

some set of weights or priorities

• Arbitration• Selects one or more steering behaviors to have complete control

over character. Many schemes available nowadays.

• Many steering systems combine elements of both blending and arbitration to maximize advantages

Page 40: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Weighted Blending

• Use weights to combine steering behaviors• Example: Riot crowd AI

• Character does not just do one thing. It does a “blend” or synthesis of all considered behaviors.

• Idea:• Each steering behavior is asked for its acceleration request• Combine the accelerations using a weighted linear sum,

coefficients specific to each behavior• If final acceleration from sum is too great, trim it accordingly

Page 41: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Flocking

• Original research by Craig Reynolds, to model movement patterns of flocks of simulated birds (“boids”).

• Flocking relies on simple weighted blend of 3 behaviors:• Separation – move away from boids that are too close• Alignment – move in the same direction and at the same

velocity as flock• Cohesion – move towards the center of mass of the flock

• Simple flocking: Equal weights for all• Any of the behaviors seemed more important?

Page 42: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Flocking

• In most implementations, flocking behavior is modified to ignore distant boids for efficiency

• A neighborhood is specified to consider only other boids within the area

• Shape: Radius or angular cut-off

Page 43: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Flocking

Page 44: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Flocking – Equilibria Problems

• Unstable equilibria: Character trying to do more than one thing at a time, resulting in doing nothing (as long as enemy is stationary), then skirts around before making a move

• Stable equilibria: Character could make it out of equilibrium slightly, but heads back into equilibrium within basin of attraction

Page 45: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Flocking – Constrained Environments

• Obstacles vs. Target: Character tries to avoid obstacle while pursuing enemy. Blending causes resulting direction even farther from correct route to capture enemy

• Narrow Doorways: Character tries to move at acute angles through narrow doorways to get to target. Obstacle avoidance causes character to move past the door missing the target

Page 46: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Flocking – Nearsightedness Problem

• Nearsightedness: Due to the behaviors acting locally in their immediate surroundings, a character can avoid a wall, but takes the wrong side of the wall due to method of computing change of orientation.

• Does not realize the wrong path!• Can be addressed by incorporating pathfinding.

Page 47: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Priority-based Blending

• Some steering behaviors do not produce acceleration as output (collision avoidance, separation, arrive, etc.) HOW?

• Example: Seek (always max acceleration) + Collision Avoidance (minimal change of movement to avoid).

• Seek always dominates if blended equally!

Page 48: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Priority-based Blending

• Idea:• Arrange behaviors in groups with regular blending weights• Place groups in order of priority, and consider each group

accordingly• If total result is very small (less than some threshold), ignore it

and consider next group• If total result is reasonable (more than some threshold), use the

result to steer character

• Example: Pursuing character with 3 groups in priority – 1st: Collision avoidance, 2nd: Separation, 3rd: Pursuit

Page 49: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Equilibria Fallback

• Priority-based approach can cope with stable equilibria problem.

• If a group of behaviors in equilibrium, total acceleration will be near zero – drop down to the next group in priority

• Example: Falling back to Wander

Page 50: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Cooperative Arbitration

• In priority blending, a prioritized behavior may have an drastic effect on the character movement (not smooth) when it changes to other behaviors of less priority

• In weighted blending, one of the main behaviors may be diluted by the output of another behavior

• Context-sensitive or cooperation between different behaviors can help create more realistic and less-dramatic movement

Page 51: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Cooperative Arbitration: Steering Pipeline

Page 52: Movement AI

Games Programming III (TGP2281) – T1, 2010/2011

Other Movement Topics

• Physics (Predictive and Non-predictive)• Jumping• Coordinated Movement (to some extent using group AI)

• Formations• Motor Control