Bullet comes with a simple vehicle controller, which can be used for arcade style vehicle simulations. Instead of simulation of each wheel and chassis as separate rigid bodies connected by joints, it simply uses a single rigid body for the chassis. Collision detection for the wheels is approximated by ray casts, and the tire friction is a basic anisotropic friction model. This approach to vehicle modelling is called "raycast vehicle", and it is used widely in commercial and non-commercial driving games.
In order to create a vehicle we first have to create an ordinary dynamic rigid body. This rigid body will serve as the vehicle chassis. Then we can create a new instance of
BulletVehicle. We have to pass the
BulletWorld and the
BulletRigidBodyNode as arguments to the vehicle constructor.
The following code snippet shows how this could be done.
from panda3d.bullet import BulletVehicle
# Chassis body
shape = BulletBoxShape(Vec3(0.7, 1.5, 0.5))
ts = TransformState.makePos(Point3(0, 0, 0.5))
chassisNP = render.attachNewNode(BulletRigidBodyNode('Vehicle'))
chassisNP.setPos(0, 0, 1)
# Chassis geometry
vehicle = BulletVehicle(world, chassisNP.node())
Once we have created the chassis and the vehicle we can add wheels to the vehicle. We can create a new wheel using the
createWheel factory method of the previously created vehicle. Once created we still have to configure the wheel, that is set friction parameters, offset of the wheel hub with respect to the chassis, axle direction and so on.
The following sample shows how to create and configure a wheel. In this case a front wheel is created. Front wheels are steerable.
wheelNP = loader.loadModel('path/to/model')
wheel = vehicle.createWheel()
wheel.setChassisConnectionPointCs(Point3(0.8, 1.1, 0.3))
wheel.setWheelDirectionCs(Vec3(0, 0, -1))
wheel.setWheelAxleCs(Vec3(1, 0, 0))
Steering and Engine/Brake
Finally we need to control steering and engine/brakes. This is best done using a task, and keeping the current steering angle around somewhere in a variable.
Here we use a very simple model of controlling the steering angle. If 'turnLeft' or 'turnRight' keys are pressed the sterring angle will increase/decrease at a constant rate, until a maximum steering angle is achieved. No relaxation is applied. Therefor we also define constants for the maximum steering angle (here: steeringClamp) and the rate at which the steering angle increases/decreases (here: steeringIncrement).
The engine force and brake model shown is very simple too. If 'forward' is pressed then the engine force will be the maximum engine force, otherwise engine force will be zero. Likewise for the brakes.
Once the steering angle and engine/brake forces are determined they will be applied to the wheels. Each wheel - addressed by it's index, i. e. 0 to 3 for a four-wheel car - can be individually assigned values for steering and engine/brake force. This way front/rear drives or four-wheel-drives can be simulated.
The following code snippet shows pseudocode for controlling steering and engine/brakes.
# Steering info
steering = 0.0 # degree
steeringClamp = 45.0 # degree
steeringIncrement = 120.0 # degree per second
# Process input
engineForce = 0.0
brakeForce = 0.0
engineForce = 1000.0
brakeForce = 0.0
engineForce = 0.0
brakeForce = 100.0
steering += dt * steeringIncrement
steering = min(steering, steeringClamp)
steering -= dt * steeringIncrement
steering = max(steering, -steeringClamp)
# Apply steering to front wheels
# Apply engine and brake to rear wheels
More realistic control models can be invented, in order to meet the control requirements of individual driving games. For example:
- Relaxing the steering angle to zero if the user does no hold down the left or right keys.
- Reducing the maximum steering angle with increasing vehicle speed.
- Setting engine force based on an analogue input, or alternatively based on the duration of the forward key being pressed down.
However, it is up to you do invent such controls. What Bullet requires is that you provide the steering angle and the engine and brake force.