Racing Overview
Drive your car to finish two laps faster than everyone else. Our engine attempts to recreate F1 racing physics, but all controls are simplified to throttle, braking, and steering with no special driving modes. Read more about the physics here.
To ensure games don’t take too long, cars get disqualified and removed from the track if they don’t complete their laps fast enough. Additionally, cars immediately get removed from the track once they have finished the race.
Ranking
TODO
Details
Your algo’s on_update() function gets called every tick, 30 times a second, with the new racing gamestate: where your car is, how it is moving, and what it can see within 300 m. Call RacingInterface.set_controls() with a throttle from -1 to 1, a brake from 0 to 1 and a steering from -1 to 1 to drive your car. Positive steering turns right, and a throttle below 0 drives the car backwards. Everything about your car in its own frame goes the same way as the steering: positive to its right and negative to its left. Each control stays as it was until you set it again, so if the algo does not call RacingInterface.set_controls() before the engine moves on to the next tick, the car drives on with the controls it already had.
Your algo’s setup() function is given track, the circuit: its centerline, how wide it is at every point of it, and where the start line is.
See RacingGameState and RacingInterface for API reference.
Sample Algo
This algo uses every control: the throttle and brake hold it to a slow speed, and the steering weaves it left and right. It doesn’t follow the track, so it won’t finish a lap. For one that drives a whole lap, start from the starter algo the local app suggests for a seat.
import math
from algogames import RacingInterface
# The speed to hold, in m/s
SPEED = 8.0
# setup is handed the track once, before the race starts
def setup(track):
RacingInterface.log(f'Racing on {track.name}, {track.lap_length:.0f} m a lap')
# This gets called with a new gamestate on each tick
def on_update(state):
speed = state.car.velocity[0] # forward speed, in m/s
# Weave left and right, a full swing every two seconds.
# Positive steers right, negative steers left.
steering = 0.3 * math.sin(state.elapsed_seconds * math.pi)
if speed < SPEED:
# Too slow: press the throttle and let off the brake
RacingInterface.set_controls(throttle=0.3, brake=0.0, steering=steering)
else:
# Fast enough: lift off the throttle and brake gently
RacingInterface.set_controls(throttle=0.0, brake=0.2, steering=steering)