Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Godot Game Engine Simulation

Author: Lawrence Chan

Overview

Virtual simulation utilizing Godot Game Engine for testing vehicle characteristics / autonomous driving.


Vehicle Controller Script (Godot – VehicleBody3D)

Overview

This script implements a basic vehicle controller for a Godot VehicleBody3D node.

It handles:

  • Acceleration and engine force
  • Braking
  • Steering control
  • Natural deceleration
  • Speed limiting
  • Debug speed output

The script reads player inputs and updates the physics behavior of the vehicle’s VehicleWheel3D nodes accordingly.


Node Requirements

The script must be attached to a VehicleBody3D node.

The vehicle should contain child nodes of type:

  • VehicleWheel3D with use_as_traction = true (drive wheels)
  • VehicleWheel3D with use_as_steering = true (steering wheels)

Input Controls

KeyAction
WAccelerate
ATurn left
SBrake
DTurn right
CReset steering to center

These must be mapped in Project Settings → Input Map:

  • accelerate
  • decelerate
  • steer_left
  • steer_right
  • steer_center

Configuration Constants

Debug

const DISPLAY_DEBUG_SPEED = true

Prints vehicle speed and steering data to the console.


Engine / Acceleration

ConstantDescription
MAX_POWERMaximum engine force
ACCELERATION_STEPIncrement applied to engine force when accelerating
USE_DYNAMIC_THROTTLEEnables dynamic throttle based on speed
MOTOR_RAMP_DOWN_STEPRate engine force decreases when not accelerating

Braking

ConstantDescription
BRAKING_STEPBrake force increment
BASE_DECELPassive braking when no throttle is applied
MAX_BRAKINGMaximum brake force

Steering

ConstantDescription
STEER_STEPSteering change per frame
MAX_STEERING_ANGLEMaximum wheel steering angle in degrees

Speed Limit

const MAX_VEHICLE_SPEED_MPH = 90.0

Maximum allowed vehicle speed.

Internally the script uses m/s and km/h, converting to mph only for debug display.


Core Variables

VariablePurpose
previous_displayed_speedPrevents duplicate debug prints
steer_normalizedNormalized steering value for debugging

Utility Functions

Degree / Radian Conversion

func deg_to_rad(deg):
    return deg * PI / 180

func rad_to_deg(rad):
    return rad * 180 / PI

Used for steering calculations.


Speed Conversion

func kph_to_mph(kph) -> float:
    return kph / 1.609

Converts kilometers per hour to miles per hour.


Throttle Function

func throttle_function(current_speed) -> float:
    # Connect engine power data here
    return 1000.0

Returns engine force when dynamic throttle is enabled.

This is currently a placeholder and can be replaced with a realistic engine torque curve.


Normalized Steering Angle

func norm_steering_angle(steer: float) -> float:
    return steer * (90/PI)

Converts steering radians into a normalized value for debugging.


Physics Update Loop

Main vehicle logic runs inside:

func _physics_process(delta: float) -> void:

This function performs:

  1. Input processing
  2. Debug speed calculation
  3. Wheel physics updates

Speed Calculation

Vehicle speed is calculated from the body’s velocity:

var velocity_mps = linear_velocity.length()

Conversions:

  • m/s → km/h: mps * 3.6
  • km/h → mph: kph / 1.609

Example debug output:

VEHICLE SPEED | 72 km/h [20 m/s] (45 mph) | STEER: 0.4

Wheel Processing

The script loops through all child nodes:

for object in get_children():

If the node is a VehicleWheel3D, it is processed depending on its role.


Traction Wheels

If:

wheel.use_as_traction

The script applies engine and braking forces.

Acceleration

Engine force increases while:

  • Below MAX_POWER
  • Below MAX_VEHICLE_SPEED

Braking

Brake force increases up to:

MAX_BRAKING

Engine force is set to zero when braking.

Natural Deceleration

When neither accelerating nor braking:

  • Engine force gradually decreases
  • Base braking (BASE_DECEL) is applied

Steering Wheels

If:

wheel.use_as_steering

The script adjusts the wheel steering angle.

Steering changes are converted from degrees to radians because Godot steering uses radians.


Steering Limits

Maximum steering range:

± MAX_STEERING_ANGLE

If exceeded, the value is clamped.


Steering Reset

Pressing C resets steering:

wheel.steering = 0.0

Vehicle Behavior Summary

BehaviorDescription
AccelerationApplies engine force to traction wheels
BrakingApplies brake force and disables engine force
SteeringRotates steering wheels within limits
Natural slowdownEngine force gradually decreases
Speed limitingPrevents vehicle exceeding maximum speed
Debug outputDisplays speed and steering data

Possible Improvements

  • Implement a realistic engine torque curve
  • Add gear ratios and transmission
  • Implement traction control
  • Add drift or slip simulation
  • Display speed using a UI element instead of console output

Example Node Structure

VehicleBody3D
│
├── VehicleWheel3D (FrontLeft)
├── VehicleWheel3D (FrontRight)
├── VehicleWheel3D (RearLeft)
└── VehicleWheel3D (RearRight)

Typical configuration:

WheelTractionSteering
Front LeftNoYes
Front RightNoYes
Rear LeftYesNo
Rear RightYesNo

Summary

This script provides a basic but functional vehicle physics controller using Godot’s built-in vehicle system.

It demonstrates:

  • Player input handling
  • Vehicle physics updates
  • Wheel-based acceleration and steering
  • Debug speed monitoring

The system is designed to be simple, modular, and easily extendable for more advanced driving simulations.