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
worldb2WorldThe world, which keeps the bodies.
initAngledoubleThe hull's and wheels' angle in radians.
initXdoubleThe hull's x in metres.
initYdoubleThe hull's y in metres.
Exceptions
- ArgumentNullException
Thrown when
worldis 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
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
MudColor
Gymnasium's MUD_COLOR: the marks left on grass.
public static readonly Color MudColor
Field Value
Size
Gymnasium's SIZE: metres per car-geometry unit.
public const double Size = 0.02
Field Value
WheelColor
Gymnasium's WHEEL_COLOR: the wheels' and the road marks' colour.
public static readonly Color WheelColor
Field Value
WheelMomentOfInertia
Gymnasium's WHEEL_MOMENT_OF_INERTIA.
public const double WheelMomentOfInertia = 1.6
Field Value
WheelR
Gymnasium's WHEEL_R: a wheel's half-length in car units.
public const int WheelR = 27
Field Value
WheelW
Gymnasium's WHEEL_W: a wheel's half-width in car units.
public const int WheelW = 14
Field Value
WheelWhite
Gymnasium's WHEEL_WHITE: the wheels' stripe.
public static readonly Color WheelWhite
Field Value
Properties
Drawlist
Python's drawlist: the wheels' bodies, then the hull's, in drawing order.
public IReadOnlyList<b2Body> Drawlist { get; }
Property Value
FuelSpent
Python's fuel_spent: the engine work since CarRacing last cleared it (every step).
public double FuelSpent { get; set; }
Property Value
Hull
public b2Body? Hull { get; }
Property Value
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
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
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
World
Python's world: the world the car's bodies live in.
public b2World World { get; }
Property Value
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
bdoubleThe 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
surfaceSurfaceThe surface to draw on.
zoomdoublePixels per metre.
translation(double From, double To)The screen offset added after rotating and scaling.
angledoubleThe rotation in radians (the negated hull angle, so the car points up).
drawParticlesboolWhether to draw the skid marks (CarRacing leaves them out of the observation).
Exceptions
- ArgumentNullException
Thrown when
surfaceis 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
gasdoubleThe 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
sdoubleThe 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
dtdoubleThe time step in seconds (1/50 in CarRacing).