games programming iii (tgp2281) – t1, 2010/2011 movement ai john see 19, 26 nov 2010
Post on 22-Dec-2015
215 views
TRANSCRIPT
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.
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
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
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
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)
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
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
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
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
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
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.
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
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
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
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)
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
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)
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
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?
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
Games Programming III (TGP2281) – T1, 2010/2011
Evade
• Simply the opposite behavior to Pursue• Instead of delegating to Seek, delegate it to Flee
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
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
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
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
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
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.
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.
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
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=?
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!
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
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
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?
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
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
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?
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
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
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
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.
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!
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
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
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