Download - AI on the Racetrack
AI on the RacetrackBy Emily Cohen
Hit the Track!The first video racing game is said to be by
Atari back in 1974 called Grand Track 10
Then:Now:
Bad Racing AIWhen competing against AI you should feel
challengedNot so challenging
We are going to talk about the general steps to make a good AI
Start at the BottomIn order to create a ‘good’ AI we must start
from the bottomStore as much information as possible in the
road
Two types of roads:Race TracksOpen Street Conditions
Divide it upSectors:
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 DotsInterfaces are the basic building blocks of the
race trackDefine the left and rightmost boundariesDefine possible driving linesThe Sector linked lists point to Leading and
Trailing edge Interfaces
Drive on the Line!Driving lines used to define optimal paths
between interfacesComprised of:
Forward vector Right Vector – used to determine how far the car is
from the racing linePlanes used to mark
boundaries of sectorUse to check if car
is in particular sector
Approximating Racing Lines
Approach #1Record the path taken by a vehicle while
under the control of human playerGood: Fast and efficient way of making
accurate approx to racings linesBad: can not be used for randomly generated
or player created tracks
Approach #2Have predefined segments with precompiled
racing linesGood: not to much…Bad:
Tracks are less randomRacing lines change depending on what comes
before and after the current segment
Approach #3Lines of Minimum CurvatureRepeatedly apply small forces to every set of
three contiguous pointsGradually reduces the curvature
Lines of Minimum CurvatureGood:
Can be approximated quickly, efficiently , and reliably.Follows true racing lines accurately enough focuses on finding an approx to the path of minimum
curvatureFinds the path the vehicle can travel at max speed
Bad:Ignores details of the physics of the vehicleNo sense of direction.Can produce errors in the way approx trades curvature
in one part of track off against curvature further along
Where am I?It is easy to determine where you are on the
track when using sectors.
Floast DistAlongSector(const Csector& sector, const vector3& pos){
vector3 delta = pos – sector.drivingLinePos;delta.y = 0.0f;float dist =
DotPorduct(sector.drivingLineForward, delta);return (dist * lengthScale);
}
More Info PleaseOther relevant data about the environment
should be stored in each sectorPath Type
Short cut, winding road, weapon pick-up rout etc.Terrain Type
Rugged terrainWallsHairpin turnBreak/Throttle
-1.0 to +1.0
Now for the AI…Goal: have the AI produce an output that
emulates human inputBasic Framework:
Finite-State MachineA Fixed Time-Step
Changing the Time-Step can cause issues because AI might miss breaking points, or react to late to obstacles if the time-step is to long
Now for the AI… - con’tControlling the car
Simple structure: Steering: x range -1.0 to +1.0 Acceleration: y <0.0 = breaking, >0.0 =
accelerationSimplifying with 2D
Project 3D coordinates onto XZ plane by zeroing Y element and then normalizing
This simplifies many calculations
State = STARTING_GRIDThe AI must initialize the car
Pass in the starting grid locationSector pointer should be stored internally as
the current and last-valid sectorsLast-valid sector helps to return the car if it is
no longer in a sectorWhen the state is set to STATE_RACING, give
little delay before actually starting… more realistic
State = RACINGUse linked list of sectors to traverse the trackAlso 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
Anticipating the Road AheadKnowing your come up to a sharp bendTraverse 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 WayOvertaking:
As we say before there is an overtaking lineJust use the index of the alternate driving line
to followImprove 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 StatesSTATE_AIRBORN
Some tracks have jumpsNeed to prepare the car for a controlled
landingSet steering straight ahead and put
acceleration to fullSTATE_OFF_TRACK
When car is no longer on in any of the sectorsKeeping 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 lead
Break earlier for corners and accelerate slowerIf weapons involved, AI could only target other
AIWhat NOT to do
Around TownHandling Open Street Conditions
Random network of roads and intersectionsFirst need to create a map that the AI can
handle
RoadsRoads represent space between two
intersectionsStraight or CurvedMight include sidewalks with obstacles
Any two roads at an intersection define a sharp turn with an angle between them > 45˚
RoadsRoads represent space between two
intersectionsStraight or CurvedMight include sidewalks with obstacles
Any two roads at an intersection define a sharp turn with an angle between them > 45˚
Navigating the CityFigure 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
Update Road CacheIncludes current and next two road segments
defined by primary route
Navigating the City… con’tEnumerate all possible routes for traversing
road segmentRoute planning distance
Longer provide better choices, see potentially blocked
routes Uses up more CPU time
Watch OutObstacle map:
Bucket system for organizing obstacles on the road
Breaks the road into smaller subsectionsIntersections are defined as an obstacle bucket
When a collision happens:Vehicle is completely under control of physics
engineMust backup (if visible)… manually reposition
Buckets
Getting around Obstacle
Handling an IntersectionThe arc of a circle defines the path through a
sharp turn
How Fast Can I Go?Usually AI tries to go as fast as possibleFind fastest way to go through sharp turn:
Attain max speed without losing friction
v = √μgRVelocit
yCoefficient of friction
Radius
Gravity
BreakingOnce you get the target velocity figure out
how long before you reach the turn:Time to Reach the Turn (T) = Distance to
Turn/SpeedThen calculate breaking force:
Breaking Force = (Current Speed – Target
Speed)/(T*μ*g)
Good ol’ PhysicsIf 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 StabilityTesting for a StabilityCorrecting the Car
Detecting StabilityThe 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 CarCase Velocity
SignsCorrection
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
SummarySectors, Interfaces and the information
stored in the trackTwo types of tracks: Race tracks and Open
Street conditionsDriving lines, and some ways to generate
themAccounting for a physics engine to correct
over and under turning
Questions?