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.
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.
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.
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.