← All articles · Dev · 4 min read

Writing your own car physics in s&box

How to write raycast suspension and tire slip curves for s&box as C# components instead of using the built-in WheelJoint, and how to show the values on an in-car dashboard.

An old white sedan with its door open, parked by a log cabin and a wooden house.

You can write your own car physics for s&box as C# components. This article covers raycast suspension, weight transfer, tire slip and an in-car dashboard.

Valve’s Source SDK 2013 includes ready-made vehicle entities, such as prop_vehicle_jeep in the Half-Life 2 game code. They run on the engine’s vehicle physics controller. A vehicle script file sets each car’s handling. For a different driving model, you worked within those script settings or wrote your own vehicle code.

Raycast suspension or WheelJoint

Write the vehicle as C# components on a GameObject that has a Rigidbody and a collider. Each wheel runs a ray or shape trace every fixed physics step to measure how far its spring is compressed. The suspension force comes from that compression and from the damper velocity. Apply it at each wheel’s position. Together with the tire forces at the contact patches, this lets weight shift under braking and cornering.

You can also build each wheel as a separate rigidbody connected by a joint. For that, s&box ships a WheelJoint with suspension, steering and a spin motor. The raycast approach gives up that convenience for direct control over the tire forces. Keep the chassis as one Rigidbody, and run four downward ray or sphere traces in OnFixedUpdate(). That method runs on the fixed physics step, which is 50 times a second by default. The FixedUpdateFrequency physics setting changes that rate. Box3D, the physics engine that s&box uses, simulates the body.

Springs, dampers and weight transfer

Each wheel calculates its suspension force with Hooke’s law plus velocity damping: F_suspension = (restLength - currentLength) * springStiffness + compressionVelocity * damperRate. Here compressionVelocity is positive while the spring gets shorter. Clamp the result to zero or above, because the ground can push a tire up but cannot pull it down. Apply the force along the chassis up axis at each wheel’s mounting point, for example with Rigidbody.ApplyForceAt. Apply the tire grip and brake forces at the contact patches as well. The chassis then pitches under hard braking and rolls through corners without scripted animation.

Tire grip does not rise in proportion to vertical load. Because of that, the load that shifts between the wheels is a large part of how a car responds. Trail braking means braking past the corner entrance. It moves load to the front tires and away from the rear, so the front grips more and the rear grips less. The car understeers less, but too much brake can lock the rear wheels and spin the car.

Tire slip angles

Cornering grip comes from each wheel’s slip angle. Read the chassis velocity at the wheel contact patch with Rigidbody.GetVelocityAtPoint. Project it onto the wheel’s forward and right vectors, and compute the slip angle atan2(v_lateral, |v_forward|). In s&box, +y points left, so check the sign of v_lateral. The atan2 function returns radians. Convert with MathX.RadianToDegree if you key your curve in degrees. Feed the slip angle into a lookup curve, such as a Curve, that rises to a peak and falls off as the tire starts to slide. Tune the peak angle for each car.

Longitudinal grip, for traction and braking, depends on the slip ratio instead. The slip ratio compares the wheel’s rolling surface speed (spin rate times effective radius) with its forward speed. It needs its own curve.

Both slip formulas have the forward speed in the denominator. Near a standstill that speed approaches zero, and an explicit simulation gives unstable results. To keep a car parked on a slope from oscillating, blend the slip solver into a static friction clamp below a small speed. Find that speed by testing. World units in s&box are inches, so convert any value in meters per second with MathX.MeterToInch.

Who simulates the car, and an in-car dashboard

For multiplayer racing or roleplay servers, run the vehicle physics on the machine that simulates the car. The connection that owns a networked object simulates it. The host simulates objects that no connection owns. The s&box networking docs suggest this for cars: when a player gets in, make that player the owner. The owner sends the chassis position, rotation and scale to other clients. Other clients interpolate that transform by default.

Sync only the extra input state, such as steering angle and throttle, as [Sync] properties. Keep the steering angle a float if you want SyncFlags.Interpolate to smooth it on other clients. Remote clients can then derive wheel spin and suspension visuals locally. The owning client controls its object, so check results that matter on the host.

UI panels in s&box render on the screen with a ScreenPanel, or in the world with a WorldPanel. An in-car dashboard built as a Razor panel can read your synced RPM, gear and wheel-slip values. Include those values in BuildHash(), so the panel rebuilds when they change.

Facepunch documents these APIs in its reference at sbox.game/api. Ownership and [Sync] are explained at sbox.game/dev/doc/networking.

Comments

No comments yet.

All news · Guides · Follow @sboxlist on Instagram and TikTok.