Class VehicleUtility
Static utility class for vehicle setup: CoM calculation, wheel dimension detection, suspension configuration, and inertia tensor calculation. All SI units.
Namespace: NWH.VehiclePhysics2
Assembly: NWH.VehiclePhysics2.dll
Syntax
public static class VehicleUtility
Methods
CalculateCenterOfMass(VehicleController, float, float)
Calculates an approximate center of mass based on wheel positions and weight distribution. Returns local coordinates suitable for direct assignment to Rigidbody.centerOfMass.
Declaration
public static Vector3 CalculateCenterOfMass(VehicleController vehicle, float heightOffset = -0.2, float frontBias = 0.52)
Parameters
| Type | Name | Description |
|---|---|---|
| VehicleController | vehicle | The vehicle controller to calculate COM for |
| float | heightOffset | Vertical offset from lowest wheel point in meters. Negative values lower the COM. Typical: -0.2 to -0.4 for cars |
| float | frontBias | Front weight distribution (0.5 = 50/50, 0.6 = 60% front, 0.4 = 40% front). Sports cars: 0.45-0.5, Sedans: 0.52-0.55, FWD: 0.6+ |
Returns
| Type | Description |
|---|---|
| Vector3 | Center of mass position in vehicle local space |
Remarks
Front/rear axles are auto-detected from wheel positions. Height is set relative to the lowest wheel minus the offset. Multi-axle vehicles group axles by Z position.
CalculateInertiaTensor(float, Vector3)
Calculates realistic inertia tensor values for vehicle dynamics. Accounts for typical vehicle mass distribution patterns.
Declaration
public static Vector3 CalculateInertiaTensor(float mass, Vector3 dimensions)
Parameters
| Type | Name | Description |
|---|---|---|
| float | mass | Vehicle mass in kilograms |
| Vector3 | dimensions | Vehicle dimensions (Width x Height x Length) in meters |
Returns
| Type | Description |
|---|---|
| Vector3 | Inertia tensor suitable for Rigidbody.inertiaTensor |
Remarks
Applies non-uniform multipliers: roll 0.8x (responsive cornering), pitch 1.2x (brake/accel stability), yaw 1.0x (balanced rotation). Adds 15% front-bias for typical engine placement.
ConfigureSuspension(VehicleController, float, float)
Configures suspension springs to reach the target compression at rest and sets damping for the chosen ride characteristics.
Declaration
public static void ConfigureSuspension(VehicleController vehicleController, float targetCompressionRatio = 0.33, float damperRatio = 0.26)
Parameters
| Type | Name | Description |
|---|---|---|
| VehicleController | vehicleController | The vehicle to configure suspension for |
| float | targetCompressionRatio | Target spring compression when stationary (0.33 = 33% compressed at rest). Range: 0.2-0.4 |
| float | damperRatio | Damping as ratio of critical damping (0.26 = slightly underdamped). Range: 0.2-0.4 for comfort, 0.5-0.8 for sport |
Remarks
Spring force is sized so the per-corner static load compresses the spring by targetCompressionRatio under the wheel's own spring.progressiveness, and the damper is sized against the tangent rate at that compression. Call after mass, CoM, and wheel positions are set.
DetectWheelDimensions(GameObject)
Automatically detects wheel dimensions (radius and width) from a wheel GameObject's mesh bounds.
Declaration
public static (float radius, float width) DetectWheelDimensions(GameObject wheelObject)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | wheelObject | GameObject containing the wheel mesh (visual representation) |
Returns
| Type | Description |
|---|---|
| (float radius, float width) | Tuple containing (radius, width) in meters |
Remarks
Combines bounds of all MeshRenderers in the wheel object. Radius = Y extent, width = smaller of the X/Z sizes. Assumes Y-up orientation. With no renderers it falls back to a 0.3 m box, which is radius 0.15 m and width 0.3 m. For non-standard orientations, set dimensions manually.