Skip to content

Vehicles

Anastacio builds the vehicle for you from the Physics > Vehicle panel. You only configure the wheels and attach a controller.

You need a chassis (the body) and one object per wheel.

  1. Select the chassis. Under Properties > Physics, use Rigid Body and turn on the vehicle in the Vehicle panel.
  2. In the wheel list, add one entry per wheel and pick its object.
  3. For each wheel, set radius, suspension, whether it steers (front wheels) and whether it is driven.

Attach the VehiclePlayerComponent component (the demo’s vehicle_player_component.py file) to the chassis.

  • W accelerates, S reverses, A/D steer;
  • Space brakes, Left Shift is the handbrake;
  • with gears registered in the Vehicle panel, shifting is automatic (or E/Q for manual).

The component publishes the vehicle_speed_kmh, vehicle_rpm and vehicle_gear properties on the chassis, useful for a speedometer.

getVehicle() returns the chassis’ vehicle (KX_VehicleWrapper). A minimal controller:

import Range
from Range import events, logic
class DirigirSimples(Range.types.KX_PythonComponent):
args = {}
def start(self, args):
self.carro = self.object.getVehicle()
def update(self):
if self.carro is None:
return
tecla = logic.keyboard.inputs
forca = 0.0
if tecla[events.WKEY].active:
forca = -1500.0
elif tecla[events.SKEY].active:
forca = 800.0
volante = 0.0
if tecla[events.AKEY].active:
volante = 0.4
elif tecla[events.DKEY].active:
volante = -0.4
for roda in range(self.carro.getNumWheels()):
self.carro.applyEngineForce(forca, roda)
self.carro.applyBraking(0.0, roda)
self.carro.setSteeringValue(volante, 0)
self.carro.setSteeringValue(volante, 1)

Attach it to the chassis. Wheels 0 and 1 are the first two in the panel’s list (the steering ones).

vehicle.savePreset("//acerto.json") writes mass, suspension, friction and wheels to a file. vehicle.loadPreset(...) reapplies the setup to the same car, for example to switch between “asphalt” and “dirt”.