Expand description
Cars and trucks: weight, speed, engine, gears, steering, brakes, fuel and sounds.
A vehicle is an optional part of a physics Body. Add one with
Body::car, Body::truck or
Body::with_vehicle; every other body is not
affected. The physics world drives it at the fixed physics rate: the pedals and the
steering wheel are plain fields you set every frame (or let
Context::drive set them from the arrow keys).
let car = ctx.physics.add(Body::car(vec3(0.0, 0.0, 0.0)).with_vehicle(
Vehicle::car().with_engine_sound("engine").with_horn_sound("horn"),
));
ctx.follow(car);
// every frame:
ctx.drive(car); // Up = gas, Down = brake / reverse, Left / Right = steer, Space = handbrake
if let Some(v) = ctx.physics.vehicle(car) {
let kmh = v.speed_kmh();
}§The model
- The weight is the body’s
massplus the vehicle’sload, in kilograms. The engine force and the brake force are fixed, so a heavier vehicle speeds up more slowly and needs a longer way to stop. - The speed is a signed number along the facing of the body (negative when
reversing), limited by
top_speedandreverse_speed. Engine force fades out towards the top speed. - Steering turns the body’s facing. The turn rate grows with speed (a parked car
cannot turn) up to
max_turn_rate; the tightest circle has the radiusturn_radius. - The vehicle moves with
World::move_by, so it stops at walls, slides along them, pushes loose objects and triggers sensors (checkpoints). Running into something loses the speed going into it and reportsPhysicsEvent::Crashed. - Vehicles drive level (they keep their height) and are not pulled by gravity.
- An automatic gearbox picks gears from the speed; the engine speed (rpm) follows.
Vehicle::engine_pitchturns it into a pitch for a looping engine sound.
Structs§
- Vehicle
- The vehicle part of a body: settings, pedals and the driving state.