Recursive AI

Simulate a wheeled vehicle in C#

OnePhysics is a free, open-source physics library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Attach a wheel-and-suspension model to a dynamic chassis, then drive it over a flat floor. This example runs the physics without a renderer.

Terminal
dotnet add package RecursiveAI.OnePhysics

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMath;
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics3D;
 
var world = new PhysicsWorld3D();
world.AddBox(new(1000,1,1000), new(0,-0.5,0),
    type: BodyType.Static);
var chassis = world.AddBox(new(1.8f,0.5f,4.2f),
    new(0,1,0), density: 400);
var car = world.AddVehicle(chassis, VehicleDef.Car);
car.Drive(throttle: 0.5f);
for (int i = 0; i < 300; i++) world.Step(1f / 60);
Console.WriteLine(FormattableString.Invariant(
    $"Forward speed: {car.ForwardSpeed:F3} m/s"));
Text
Forward speed: 11.736 m/s

Unzip the example, then dotnet run.

The full program

All geometry is created in code: a static floor box and a dynamic chassis box using the car preset. The floor’s top lies at Y = 0. The download prints chassis motion and each wheel’s contact state, saves a snapshot, then compares every world hash over two runs of the same steering inputs. The hashes check that the replay gives identical results (determinism), as in the snapshot and rollback recipe.

C#
using RecursiveAI.OneMath;
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics3D;
 
var world = new PhysicsWorld3D();
world.AddBox(new(1000,1,1000), new(0,-0.5,0),
    type: BodyType.Static);
var chassis = world.AddBox(new(1.8f,0.5f,4.2f),
    new(0,1,0), density: 400);
var car = world.AddVehicle(chassis, VehicleDef.Car);
car.Drive(throttle: 0.5f);
for (int i = 0; i < 300; i++) world.Step(1f / 60);
Console.WriteLine(FormattableString.Invariant(
    $"Forward speed: {car.ForwardSpeed:F3} m/s"));
Console.WriteLine(FormattableString.Invariant(
    $"Chassis Z: {chassis.Position.Z:F3} m"));
for (int wheel = 0; wheel < car.WheelCount; wheel++)
    Console.WriteLine($"Wheel {wheel} contact: {car.GetWheel(wheel).IsInContact}");
var snapshot = world.Snapshot();
var hashes = new ulong[60];
for (int i = 0; i < hashes.Length; i++)
{
    car.Drive(0.5f, steer: 0.1f);
    world.Step(1f / 60);
    hashes[i] = world.ComputeHash();
}
world.Restore(snapshot);
car = world.GetVehicle(car.RawHandle);
bool same = true;
for (int i = 0; i < hashes.Length; i++)
{
    car.Drive(0.5f, steer: 0.1f);
    world.Step(1f / 60);
    same &= hashes[i] == world.ComputeHash();
}
Console.WriteLine($"Replay hashes match: {same}");
Text
Forward speed: 11.736 m/s
Chassis Z: 33.187 m
Wheel 0 contact: True
Wheel 1 contact: True
Wheel 2 contact: True
Wheel 3 contact: True
Replay hashes match: True

Drive throws ArgumentOutOfRangeException for inputs outside their allowed ranges or NaN, leaving the previous inputs unchanged.

Variations

Brake after accelerating. Append these lines to the snippet at the top. The brake is held through the following steps.

C#
car.Drive(throttle: 0, brake: 1);
for (int i = 0; i < 180; i++) world.Step(1f / 60);
Console.WriteLine(FormattableString.Invariant(
    $"After braking: {car.ForwardSpeed:F3} m/s"));
Text
After braking: -0.014 m/s

The short answer

Create a dynamic body for the chassis and pass it to PhysicsWorld3D.AddVehicle with VehicleDef.Car. Call Drive with normalized inputs, then advance the world using a fixed step. Read ForwardSpeed for motion along the vehicle’s forward direction and GetWheel for the state of an individual wheel after the last step.

The example uses OnePhysics directly. It creates no game scene, camera, controller bindings or visual wheel models. Its numerical output lets you check that the chassis moves and the wheels find the floor before adding presentation code. Both the floor and the vehicle are defined in the program; there are no asset downloads or service credentials to supply.

How do I simulate wheel vehicle physics in C#?

A vehicle needs a dynamic chassis body and a vehicle definition. The chassis supplies its collision geometry and mass properties. The definition supplies the wheel, suspension and drivetrain configuration. OnePhysics includes Car, Truck and Kart presets; the program uses Car rather than assembling a wheel definition from scratch.

A vehicle does not add separate wheel rigid bodies. Each wheel sweeps a cylinder, or a sphere when configured that way, along its suspension direction to find ground. Suspension and tire forces act on the chassis and, when applicable, the dynamic ground body. The vehicle specification describes the casting, suspension, tires and drivetrain. This matters when connecting a renderer: visual wheels should follow wheel state, not independent simulated cylinders joined to the chassis.

Call Drive when an input changes. Its values persist through later steps until another call replaces them. Throttle and steering range from −1 to 1; brake and handbrake range from 0 to 1. Positive steering points toward the chassis’s positive X direction in the presets. The full program supplies the same steering command at each replayed step so the input sequence is explicit.

How do I make a physics simulation for a vehicle?

Keep simulation time separate from display updates. Here each call advances by the same 1f / 60 interval. The example first accelerates in a straight line, then saves a snapshot and records a hash after each steering step. Restoring that snapshot and applying the same inputs produces matching hashes in this run.

Compare the whole sequence, not only the final position. A vehicle can finish near its original path while wheel spin, drivetrain state or chassis velocity differs. The world hash captures state beyond the visible transform. Snapshot and restore are useful for repeating a local test, but this small example is not a complete network transport or a multiplayer input protocol.

Read wheel contacts after stepping. IsInContact means the wheel sweep found ground within suspension travel; it is not a generic collision event for the chassis. ContactPoint, SuspensionLength and SuspensionForce provide further information when diagnosing ride height or a wheel that hangs clear of the floor. A chassis touching the floor does not by itself prove that its wheels are supporting it.

The braking variation deliberately prints the measured speed instead of asserting exact rest. Here the final value is slightly negative. Decide what counts as stopped for your game using a suitable tolerance, rather than testing floating-point speed for equality with zero.

Where can I download a free C# vehicle physics simulation example?

Use the example download at the top of this page, unzip it and run dotnet run. It contains the project file and the complete program, with the published package version pinned. OnePhysics is free and open source; the header links to its package, repository and documentation.

The download is a console simulation, not an interactive driving game. Start with its printed speed, contact states and replay result. Then connect your own input loop and renderer while keeping the simulation interval fixed and passing driver inputs through Drive.

All onephysics recipes