View on GitHub

dragster

Dragster Analysis and Simulation

Dragster Physics

States and Inputs

The evolution of the game can be modeled as a discrete-time dynamical system of the form

where is a global in-game frame counter, is the game state in frame and are the user inputs in frame .

The state of the game is completely characterized by five variables:

Symbol State Domain Comments
Clutch flag  
Gear Gear indicates the ‘neutral’ gear.
Motor speed  
Dragster speed  
Dragster position The game starts at and ends when .

The user input consist of two variables:

Symbol Input Domain
Throttle
Clutch

Note that the clutch flag and the clutch user input are not redundant. The clutch flag denotes the clutch input from the previous frame, i.e. .

Effect of throttle

The throttle affects the motor speed . Motor speed is incremented when and decremented otherwise. To model the effect of the gear’s transmission ratios, the increments are applied only at a subset of time steps.

Gear Increment Frame rule
0 ±3 every frame
1 ±1 every frame
2 ±1 every second frame (when )
3 ±1 every fourth frame (when )
4 ±1 every eighth frame (when )

The frame rule is not applied when the clutch was pressed in the previous time step (indicated by state variable ). In this case, the increment of is applied regardless.

Note that this frame rule makes the function time-inhomogenous, i.e. the function itself depends on the frame counter .

Effect of clutch

Pressing the clutch switches gears. Whenever the clutch is released the gear increases by one. Releasing the clutch in fourth gear has no effect (the gear stays in fourth position).

In addition to switching gears, pressing the clutch modulates the rules for updating the motor speed (see Effect of throttle).

Effect of motor speed

The motor speed determines whether the car accelerates or decelerates, depending on the car’s current speed . A given gear and motor speed determine the car’s target (or reference) speed . To model the effect of a turbo charger, the target speed is boosted when which is indicated by the turbo charger flag in the formulas below.

Gear Target speed
0
1
2
3
4

If the car is slower than the target speed (), it accelerates by 2, if it is faster (), it decelerates by 1.

Additionally, if the target speed exceeds the actual speed by 16 or more, the motor speed is decremented by 1.

Motor speeds are illegal. In the game, such states result in a busted engine and the game is over.

Effect of dragster speed

The speed simply affects the dragster’s position. In each time step, the position increases by the speed of the previous time step.

Python implementation

Since writing down the above rules in a mathematical formula for is tedious, we give a description in the form of Python code which is more concise. The code snippet below implements the function . The inputs are

The output is the next game state (4-tuple). It returns None when the user inputs were infeasible for the current state x.

Note that the function does not calculate the dragster position , since it is straightforward to get the position as the sum of speeds for .

mask = (0x00, 0x00, 0x02, 0x06, 0x0E)
rd = (3, 1, 1, 1, 1)

def f(t, u, x):
    # unpack variables
    c, y, r, v = x
    th, cl = u
    # motor speed (r)
    k = (1-c)*y
    if t & mask[k] == 0:
        r = max(r + (2*th-1)*rd[k], 0)
    if r > 31:
        return None
    # dragster speed (v)
    if y > 0 and c == 0:
        vref = ((1 << y) >> 1)*r + ((1 << y) >> 2)*(r >= 20)
        if vref < v:
            v -= 1
        elif vref > v:
            if vref >= v + 16:
                r -= 1
            v += 2
    # gear (y)
    if cl == 0 and c == 1:
        y = min(y+1, 4)
    # clutch (c)
    c = cl
    return c, y, r, v

References