# Exam Guides
# Game
Key while developing AI for games is making it realistic and behave more human-like. In some games (like Angry bird), it is possible for the AI to compute projectiles so it always hits the target perfectly using equations, but that would not make an interesting game. So the AI needs to use a iterative approach where it mimics a human like behavior, learning from its earlier failures.
For First person shooter games, your code can access the entire gamespace and know where the objects are and also target objects perfectly. But human players don't function the same way (e.g. they can't see behind walls). So, you want the AI system to take this into account.
# Basic Agent Movement
# Types of movement
- Discrete
- Continuous
When it comes to planning the movements, think of it in 2D because most games are inherently 2d because even 3d games are bound by gravity. For exceptions within the game when the agent is falling/flying, we can handle it using special cases.
# Getting to target
- Decouple the vector to get magnitude and direction
- To compute new position, don't blindly use constant velocity offset (
velocity * t
) wheretime (t)
is computed by1/(FPS of game)
. This is because FPS is constantly changing in the game depending on how heavy the content being rendered is. Therefore, you might want to:- Use a velocity offset that leverages the
deltaT
provided by Unity instead of thet
that relies on FPS.deltaT
is free from FPS dependency. The only flip side is the multiplication that will happen every update which could've been avoided with constant velocity offset. - Unity provides a fixedUpdate hook as well, but at that point you're leaving it up to unity to make the tradeoffs if the rendering is heavy and certain things have to be deprioritized.
- Use a velocity offset that leverages the
# Statics
Statics are data that are maintained as state information about the agent. E.g. position & orientation
# Coordinate system conventions
- In 3D Games, XZ plane is used for the characeter movement calculations. Y is the vertical orientation.
- In 2D orientation, rotation from +Z axis is used for convenience
- Common convenience function:
Atan2(a, b)
to get angles
- Common convenience function:
# Orientation/Rotation
- Depending on game engine, there may be a high-level method for setting the facing/forward vector of the game object
- Or the appropriate transform can be generated using Homogeneous Affine Transform (4x4 matrix representing translation, rotation and scale)
- A slightly fancier rotation can be by using angular velocity, so the orientation changes gradually as the game object moves towards the destination.
# Kinematic Movement Algorithms
# Kinematic Seek
- A kinematic object knows its statics (position and orientation).
- Updating a kinematic object primarily involves position (orientation for visual quality)
- One consideration for kinematic movements like the seek algorithm is to only calculate velocity and assign that to the agent (similar to storing statics). At update time, the agent then uses the velocity to update position. This works great for movements like steering as well.
# Kinematic Fleeing
- Just flip the relative position vector calculation (B - A), otherwise the same calculations.
# Kinematic Arriving
- Seeking algorithm as is won't work well due to overshooting and wiggling around the goal.
Two approaches
- Large capture radius
- Slow down upon arriving
# Arriving with Modified Seek
- Blend both the approaches mentioned above
- Modify seek algorithm to check for
magnitude of vector BA <= CaptureRadius
, stopping if so - Divide vector BA by a timeToTarget constant (small value such as 0.25s). (note: i.e. the vector is almost like a teleportation velocity if the agent was to get the target in next second)
- If the magnitude of resultant vector is less than
then use the new velocity for - Otherwise, calculate
as before
# Kinematic Wandering
Useful for extras in games
- We can use the orientation stored in the statics for the agent to define velocity direction (multiply by
for new velocity) - Then update the orientation (angle form) with a zero-biased random value [-1, 1] multiplied by a maxRotation
- Assuming random() returns [0, 1], a zero-biased random is
random() - random()
Range [-1, 1]
values around 0 most common
# Kinematic Path Following
- For a path made up of a sequence of waypoints, we can just use seek algorithm for each waypoint (possibly arriving at the last waypoint)
- However, a capture radius will be deseriable to avoid wiggling or backtracking
- A downside here is that the agent will tend to not be exactly on the path.
- Also, if a waypoint is somehow missed, the agent must backtrack.