# AI on the Racetrack

```By Emily Cohen
Hit the Track!
 The first video racing game is said to be by Atari back
in 1974 called Grand Track 10
Now:
Then:
 When competing against AI you should feel
challenged
 Not so challenging
 We are going to talk about the general steps to make a
good AI
Start at the Bottom
 In order to create a ‘good’ AI we must start from the
bottom
 Store as much information as possible in the road
 Race Tracks
 Open Street Conditions
Divide it up
 Sectors:
 The track is divided up into multiple sectors


Defined by a doubly linked list that allows for forward and
backward traversal
Connected by Interfaces
Connect the Dots
 Interfaces are the basic building blocks of the race
track
 Define the left and rightmost boundaries
 Define possible driving lines
edge Interfaces
Drive on the Line!
 Driving lines used to define optimal paths between
interfaces
 Comprised of:


Forward vector
Right Vector – used to determine how far the car is from the
racing line
 Planes used to mark
boundaries of sector
 Use to check if car
is in particular sector
Approximating Racing Lines
Approach #1
 Record the path taken by a vehicle while under the
control of human player
 Good: Fast and efficient way of making accurate
approx to racings lines
 Bad: can not be used for randomly generated or player
created tracks
Approach #2
 Have predefined segments with precompiled racing
lines
 Good: not to much…
 Tracks are less random
 Racing lines change depending on what comes before
and after the current segment
Approach #3
 Lines of Minimum Curvature
 Repeatedly apply small forces to every set of three
contiguous points
Lines of Minimum Curvature
 Good:
 Can be approximated quickly, efficiently , and reliably.
 Follows true racing lines accurately enough
 focuses on finding an approx to the path of minimum
curvature
 Finds the path the vehicle can travel at max speed
 Ignores details of the physics of the vehicle
 No sense of direction.
 Can produce errors in the way approx trades curvature
in one part of track off against curvature further along
Just For Fun
Where am I?
 It is easy to determine where you are on the track when
using sectors.
Floast DistAlongSector(const Csector&amp; sector, const vector3&amp; pos)
{
vector3 delta = pos – sector.drivingLinePos;
delta.y = 0.0f;
float dist = DotPorduct(sector.drivingLineForward, delta);
return (dist * lengthScale);
}
 Other relevant data about the environment should be
stored in each sector
 Path Type

Short cut, winding road, weapon pick-up rout etc.
 Terrain Type

Rugged terrain
 Walls
 Hairpin turn
 Break/Throttle

-1.0 to +1.0
Now for the AI…
 Goal: have the AI produce an output that emulates
human input
 Basic Framework:
 Finite-State Machine
 A Fixed Time-Step

Changing the Time-Step can cause issues because AI might
miss breaking points, or react to late to obstacles if the timestep is to long
Now for the AI… - con’t
 Controlling the car
 Simple structure:


Steering: x  range -1.0 to +1.0
Acceleration: y  &lt;0.0 = breaking, &gt;0.0 = acceleration
 Simplifying with 2D
 Project 3D coordinates onto XZ plane by zeroing Y
element and then normalizing
 This simplifies many calculations
State = STARTING_GRID
 The AI must initialize the car
 Pass in the starting grid location
 Sector pointer should be stored internally as the current
and last-valid sectors
 Last-valid sector helps to return the car if it is no longer
in a sector
 When the state is set to STATE_RACING, give little delay
before actually starting… more realistic
Weeeee
State = RACING
 Use linked list of sectors to traverse the track
 Also helps keep track of the car…
 If the car can’t be found on a sector that means it is
probably lost somewhere
 For that you switch to STATE_OFF_TRACK
What to do?
 Split Decisions
 Deciding what way to go when you have two paths
 Combination of AI’s current needs and path info
 Knowing your come up to a sharp bend
 Traverse sectors a certain distance ahead from car’s
current position
Turn Quick!
 Hairpin turns:
 Looking ahead can cause issues with hairpin turns
 Mark sectors as “hairpin left” or “hairpin right”

Cut the search short
 The AI will continue to target the start of the first sector.
Once the minimum search distance is reached, the AI
will then target the driving line ahead by this distance.

Result: The AI targets the first marked sector while breaking
to a reasonable speed, and then following the corner when
close enough
Turn Quick!
 Hairpin turns:
 Looking ahead can cause issues with hairpin turns
 Mark sectors as “hairpin left” or “hairpin right”

Cut the search short
 The AI will continue to target the start of the first sector.
Once the minimum search distance is reached, the AI
will then target the driving line ahead by this distance.

Result: The AI targets the first marked sector while breaking
to a reasonable speed, and then following the corner when
close enough
Get Out of My Way
 Overtaking:
 As we say before there is an overtaking line
 Just use the index of the alternate driving line to follow
 Improve upon the AI’s over taking ability by creating
multiple overtaking lines.
 Chose appropriate line based on other car’s relative
position and what line is on the inside corner
Some Other States
 STATE_AIRBORN
 Some tracks have jumps
 Need to prepare the car for a controlled landing
 Set steering straight ahead and put acceleration to full
 STATE_OFF_TRACK
 When car is no longer on in any of the sectors
 Keeping track of last valid sector
Catch up!
 If the AI is doing “too well”
 Limit AI’s top speed to proportion to how far it is in the
 Break earlier for corners and accelerate slower
 If weapons involved, AI could only target other AI
 What NOT to do
Around Town
 Handling Open Street Conditions
 Random network of roads and intersections
 First need to create a map that the AI can handle
 Roads represent space between two intersections
 Straight or Curved
 Might include sidewalks with obstacles
 Any two roads at an intersection define a sharp turn
with an angle between them &gt; 45˚
 Roads represent space between two intersections
 Straight or Curved
 Might include sidewalks with obstacles
 Any two roads at an intersection define a sharp turn
with an angle between them &gt; 45˚
Navigating the City
 Figure out what road or intersection car is on
 Can then determine the relative location in the primary
route
 Plan immediate rout to navigate up coming obstacles
 Includes current and next two road segments defined
by primary route
Navigating the City… con’t
 Enumerate all possible routes for traversing road
segment
 Route planning distance
 Longer


provide better choices, see potentially blocked routes
Uses up more CPU time
Watch Out
 Obstacle map:
 Bucket system for organizing obstacles on the road
 Breaks the road into smaller subsections
 Intersections are defined as an obstacle bucket
 When a collision happens:
 Vehicle is completely under control of physics engine
 Must backup (if visible)… manually reposition
Buckets
Getting around Obstacle
Handling an Intersection
 The arc of a circle defines the path through a sharp
turn
How Fast Can I Go?
 Usually AI tries to go as fast as possible
 Find fastest way to go through sharp turn:
 Attain max speed without losing friction
Velocity
v = √μgR
Coefficient of friction
Gravity
Breaking
 Once you get the target velocity figure out how long
before you reach the turn:
 Time to Reach the Turn (T) = Distance to Turn/Speed
 Then calculate breaking force:
 Breaking Force =
(Current Speed – Target Speed)/(T*μ*g)
Good ol’ Physics
 If the car is modeled with even a basic physics
simulator the car will be subjected to under-steering
and over-steering.
 This causes spinning and missing a turn
 3 Steps:
 Detect the Car’s Stability
 Testing for a Stability
 Correcting the Car
Detecting Stability
 The stability can be determined by a few comparisons
of the sideways velocities of each wheel
 Find the dot product of the velocity of the wheel and
the unit-length right direction of its orientation
Testing for Stability
Correcting the Car
Case
Velocity Signs Correction
Under-Steer
Same
Abs(FrontVel + RearVel)/UndersteerRange
Over-Steer 1
Same
Abs(FrontVel - RearVel)/OversteerRange
Over-Steer 2 Different
(Abs(RearVel)-Abs(FrontVel))/OversteerRange
 Summed velocities divided by range factor – this
reduces the correction amount to a value between 0
and 1
 Correction value should be added or subtracted from
the current steering positions depending on sign on dx
Summary
 Sectors, Interfaces and the information stored in the
track
 Two types of tracks: Race tracks and Open Street
conditions
 Driving lines, and some ways to generate them
 Accounting for a physics engine to correct over and
under turning
Questions?
```