Table of Contents

Class Car

Namespace
Gym.Environments.Envs.Box2D
Assembly
Gym.Environments.dll

Gymnasium's car_dynamics.Car: a top-down car of a four-polygon hull and four wheels on limited revolute joints, rear-wheel drive. Each Step(double) turns the front wheels toward their steering target, spins the wheels from gas and brakes, and applies each wheel's friction force (more on road tiles than on grass), leaving skid marks when a wheel slides.

public sealed class Car
Inheritance
Car
Inherited Members
Extension Methods

Remarks

The arithmetic is Gymnasium's: float64 Python arithmetic over the bodies' float32 state, forces rounded to float32 where pybox2d converts them. Draw(Surface, double, (double X, double Y), double, bool) renders onto a pygame-compatible Surface through RotateRad(double) and Draw, as Gymnasium's draws through pygame.

Not thread-safe; it changes its world's bodies. Reference: gymnasium/envs/box2d/car_dynamics.py.

Constructors

Car(b2World, double, double, double)

Builds the car in a world (Gymnasium's Car(world, init_angle, init_x, init_y)): the hull's four polygons (density 1), then each wheel (density 0.1, colliding only with category 1) on a motorized revolute joint limited to ±0.4 rad.

public Car(b2World world, double initAngle, double initX, double initY)

Parameters

world b2World

The world, which keeps the bodies.

initAngle double

The hull's and wheels' angle in radians.

initX double

The hull's x in metres.

initY double

The hull's y in metres.

Exceptions

ArgumentNullException

Thrown when world is null.

AssertionError

Thrown when the world is stepping.

Fields

EnginePower

Gymnasium's ENGINE_POWER: the drive's power factor.

public const double EnginePower = 40000

Field Value

double

FrictionLimit

Gymnasium's FRICTION_LIMIT: a wheel's friction force cap on road (0.6 of it on grass).

public const double FrictionLimit = 400

Field Value

double

MudColor

Gymnasium's MUD_COLOR: the marks left on grass.

public static readonly Color MudColor

Field Value

Color

Size

Gymnasium's SIZE: metres per car-geometry unit.

public const double Size = 0.02

Field Value

double

WheelColor

Gymnasium's WHEEL_COLOR: the wheels' and the road marks' colour.

public static readonly Color WheelColor

Field Value

Color

WheelMomentOfInertia

Gymnasium's WHEEL_MOMENT_OF_INERTIA.

public const double WheelMomentOfInertia = 1.6

Field Value

double

WheelR

Gymnasium's WHEEL_R: a wheel's half-length in car units.

public const int WheelR = 27

Field Value

int

WheelW

Gymnasium's WHEEL_W: a wheel's half-width in car units.

public const int WheelW = 14

Field Value

int

WheelWhite

Gymnasium's WHEEL_WHITE: the wheels' stripe.

public static readonly Color WheelWhite

Field Value

Color

Properties

Drawlist

Python's drawlist: the wheels' bodies, then the hull's, in drawing order.

public IReadOnlyList<b2Body> Drawlist { get; }

Property Value

IReadOnlyList<b2Body>

FuelSpent

Python's fuel_spent: the engine work since CarRacing last cleared it (every step).

public double FuelSpent { get; set; }

Property Value

double

Hull

Python's hull: the car's body, null after Destroy().

public b2Body? Hull { get; }

Property Value

b2Body

HullColor

Python's hull.color: the hull's colour as fractions of 255 (dark red).

public (double R, double G, double B) HullColor { get; set; }

Property Value

(double R, double G, double B)

HullPoly1

Gymnasium's HULL_POLY1: the front spoiler.

public static IReadOnlyList<(int X, int Y)> HullPoly1 { get; }

Property Value

IReadOnlyList<(int X, int Y)>

HullPoly2

Gymnasium's HULL_POLY2: the nose.

public static IReadOnlyList<(int X, int Y)> HullPoly2 { get; }

Property Value

IReadOnlyList<(int X, int Y)>

HullPoly3

Gymnasium's HULL_POLY3: the body.

public static IReadOnlyList<(int X, int Y)> HullPoly3 { get; }

Property Value

IReadOnlyList<(int X, int Y)>

HullPoly4

Gymnasium's HULL_POLY4: the rear spoiler.

public static IReadOnlyList<(int X, int Y)> HullPoly4 { get; }

Property Value

IReadOnlyList<(int X, int Y)>

Particles

Python's particles: the skid marks, oldest first, at most 30; a new list on each read.

public IReadOnlyList<Particle> Particles { get; }

Property Value

IReadOnlyList<Particle>

WheelPos

Gymnasium's WHEELPOS: the wheels' anchors in car units: front left, front right, rear left, rear right.

public static IReadOnlyList<(int X, int Y)> WheelPos { get; }

Property Value

IReadOnlyList<(int X, int Y)>

Wheels

Python's wheels: front left, front right, rear left, rear right; empty after Destroy().

public IReadOnlyList<CarWheel> Wheels { get; }

Property Value

IReadOnlyList<CarWheel>

World

Python's world: the world the car's bodies live in.

public b2World World { get; }

Property Value

b2World

Methods

Brake(double)

Gymnasium's brake(b): every wheel's brake, unclipped; 0.9 and above locks the wheels.

public void Brake(double b)

Parameters

b double

The brake, normally in [0, 1].

Destroy()

Gymnasium's destroy(): destroys the hull, then each wheel (their joints go with the hull).

public void Destroy()

Exceptions

AssertionError

Thrown when the world is stepping.

Draw(Surface, double, (double X, double Y), double, bool)

Gymnasium's draw(surface, zoom, translation, angle, draw_particles): the skid marks as 2-pixel polylines (when drawParticles), then each wheel and the hull fixture by fixture as filled polygons, and on each wheel its white stripe where the wheel's phase shows it, all rotated by angle, scaled by zoom and moved by translation (in a y-up frame flipped later).

public void Draw(Surface surface, double zoom, (double X, double Y) translation, double angle, bool drawParticles = true)

Parameters

surface Surface

The surface to draw on.

zoom double

Pixels per metre.

translation (double From, double To)

The screen offset added after rotating and scaling.

angle double

The rotation in radians (the negated hull angle, so the car points up).

drawParticles bool

Whether to draw the skid marks (CarRacing leaves them out of the observation).

Exceptions

ArgumentNullException

Thrown when surface is null.

NotSupportedException

Thrown for a surface with per-pixel alpha (not modelled).

Gas(double)

Gymnasium's gas(gas): rear-wheel drive. The throttle is clipped to [0, 1]; each rear wheel's gas rises toward it by at most 0.1 per call and drops at once (NaN propagates, as NumPy's clip keeps it).

public void Gas(double gas)

Parameters

gas double

The throttle.

Steer(double)

Gymnasium's steer(s): the front wheels' steering target; the joints' motors turn toward it over time.

public void Steer(double s)

Parameters

s double

The target, -1 (left) to 1 (right) in Gymnasium's use.

Step(double)

Gymnasium's step(dt): for each wheel, its steering motor's speed, the friction limit of the ground under it, the spin from gas and brake, the slip force capped at the friction limit (a skid mark when it exceeds twice the limit), the spin the friction takes back, and the force applied to the wheel's body. The world is not stepped.

public void Step(double dt)

Parameters

dt double

The time step in seconds (1/50 in CarRacing).