Vehicle

Package: Libraries.Game.Physics.Vehicles · Inherits from: Libraries.Language.Object, Libraries.Game.Physics.PhysicsBehavior3D

A vehicle in a physics game is a body that can move, turn, and react to the ground and other objects. You can use this class to make vehicles that can react in games involving physics. The word wheel and suspension are used here to mean the same thing in this context. This vehicle's design is an example of a raycast vehicle. This means that it does not use physics for wheels and other pieces as you would normally setup. Instead, a a raycast vehicle checks the ground beneath each wheel by sending a short line downward. This is called a raycast. The distance to the ground helps decide how high the vehicle sits and how its wheels push against the ground. This lets it move over uneven ground without needing each wheel to be a full moving physics object. You can add wheels, steer them, apply power to move forward or backward, apply brakes, and check the vehicle's speed. To use a vehicle, give it a chassis with SetChassis, create a Suspension for each wheel and add it with Add, and then add the vehicle to the same Layer3D as the chassis: Vehicle vehicle vehicle:SetChassis(chassis) vehicle:Add(frontLeft) vehicle:Add(frontRight) vehicle:Add(rearLeft) vehicle:Add(rearRight) layer:Add(vehicle) From then on, the layer updates the vehicle once for every physics step and moves the wheels' visual items once per frame. Control the vehicle from the game's Update action with SetEngineForce, SetSteering, and SetBrake. Before removing the chassis from the layer, remove the vehicle with the layer's Remove action, or call RemoveBehaviorsUsing with the chassis.

Actions

Add(integer location, Libraries.Game.Physics.Vehicles.Suspension suspension)

This action adds a wheel setup at a chosen place in the list. Places start at 0. The new wheel is placed before the wheel already at that place.

Parameters

Add(Libraries.Game.Physics.Vehicles.Suspension suspension)

This action adds a wheel setup to the end of the vehicle's list.

Parameters

AddToEnd(Libraries.Game.Physics.Vehicles.Suspension suspension)

This action adds a wheel setup after all the other wheel setups.

Parameters

AddToFront(Libraries.Game.Physics.Vehicles.Suspension suspension)

This action adds a wheel setup at the start of the list. The other wheel setups move one place to make room.

Parameters

Get(integer location)

This action gets the wheel setup at a place in the list. Places start at 0, so place 0 is the first wheel setup.

Parameters

  • location integer: The place of the wheel setup.

Returns

Libraries.Game.Physics.Vehicles.Suspension: The wheel setup at that place.

GetBodyVisual()

Returns the item drawn as the vehicle's body, or undefined if there is none.

Returns

Libraries.Interface.Item3D: The item set with SetBodyVisual.

GetBodyVisualOffset()

Returns where the body's item sits from the chassis's middle.

Returns

Libraries.Compute.Vector3: The offset, along the chassis's own axes.

GetChassis()

Returns the item used as the vehicle's main body. The vehicle reads its movement and applies suspension and tire impulses to this item.

Returns

Libraries.Interface.Item3D

GetItem(integer index)

This action returns the vehicle's chassis for index 0, and undefined for any other index.

Parameters

  • index integer

Returns

Libraries.Interface.Item3D

GetItemsSize()

To use a vehicle, give it a chassis with SetChassis, add a Suspension for each wheel, and then add the vehicle to the same Layer3D as the chassis: Vehicle vehicle vehicle:SetChassis(chassis) vehicle:Add(frontLeft) vehicle:Add(frontRight) vehicle:Add(rearLeft) vehicle:Add(rearRight) layer:Add(vehicle) From then on the layer updates the vehicle once for every physics step, and moves the wheels' visual items once per frame. Control it from the game's Update action with SetEngineForce, SetSteering, and SetBrake. Removing the chassis from the layer also removes the vehicle.

Returns

integer

GetIterator()

This action gives you an iterator for going through the vehicle's wheel setups one at a time.

Returns

Libraries.Containers.Iterator<Libraries.Game.Physics.Vehicles.Suspension>: An iterator over the wheel setups.

GetSpeedKilometersPerHour()

Returns the vehicle's speed in kilometers per hour. The speed is the magnitude of the chassis's movement, so it is always zero or positive, whether the vehicle is moving forward or backward.

Returns

number

GetSpeedMilesPerHour()

Returns the vehicle's speed in miles per hour. The speed is always zero or positive, whether the vehicle is moving forward or backward.

Returns

number

GetSuspensionSize()

This action tells you how many wheel setups are in the vehicle.

Returns

integer: The number of wheel setups in the list.

Remove(Libraries.Game.Physics.Vehicles.Suspension suspension)

This action looks for a wheel setup and removes it from the list.

Parameters

Returns

boolean: True if it was removed, or false if it was not found.

RemoveAt(integer location)

This action removes a wheel setup from a place in the list and returns the removed setup. Places start at 0.

Parameters

  • location integer: The place of the wheel setup to remove.

Returns

Libraries.Game.Physics.Vehicles.Suspension: The wheel setup that was removed.

Set(integer location, Libraries.Game.Physics.Vehicles.Suspension suspension)

This action replaces the wheel setup at a place in the list. Places start at 0. The place must already have a wheel setup.

Parameters

SetBodyVisual(Libraries.Interface.Item3D visual, Libraries.Compute.Vector3 offset)

Sets the item drawn as the vehicle's body, such as a car model, so the chassis can be a plain physics item that isn't drawn. Each frame, after physics, the vehicle places the item where the chassis is drawn. Before calling this action, turn the item as it should look on a chassis with no rotation, with its front along the chassis's +Z. The item gets no physics and isn't the chassis's child, so a model can be used as it is, whichever way its file faces and wherever its origin is: the offset says where its origin sits.

Parameters

Example

use Libraries.Game.Physics.Vehicles.Vehicle
use Libraries.Game.Graphics.Model
use Libraries.Compute.Vector3

Vehicle vehicle
Model car
car:Load("Car/ToyCar.gltf")
car:SetYawPitchRoll(180, 0, 0)
Vector3 offset
offset:Set(0, -0.2, 0)
vehicle:SetBodyVisual(car, offset)

SetBrake(number force, integer wheelIndex)

Sets the braking force for this wheel, in newtons. The brake slows the wheel along the ground with at most this force, so the vehicle stops without the wheel being pushed backward. The brake is only used while the wheel has no engine force. Negative values are treated as 0. Set it to 0 to release the brake. As a starting point, divide the vehicle's mass times the slowing you want by the number of braked wheels. For example, to slow an 800 kg vehicle at 7.5 meters per second each second (about three quarters of gravity) with four braked wheels, use 800 * 7.5 / 4 = 1500. Very strong brakes on a tall vehicle can tip it forward, and grippy tires make that more likely.

Parameters

  • force number: The braking force for this wheel, in newtons.
  • wheelIndex integer: The index of the wheel to brake.

SetChassis(Libraries.Interface.Item3D chassis)

Sets the vehicle's main body. The vehicle reads its movement and applies suspension and tire impulses to this item.

Parameters

SetEngineForce(number force, integer wheelIndex)

Sets the driving force for this wheel. Positive and negative values drive in opposite directions. Set it to 0 to apply no engine force. The vehicle uses this value during its next physics update.

Parameters

  • force number: The driving force for this wheel
  • wheelIndex integer: The index of the wheel

SetSteering(number value, integer wheelIndex)

Sets how sharply this wheel turns the vehicle. The value is in radians. A value of 0 points the wheel straight ahead. Positive and negative values turn it in opposite directions. The angle is used as given, so keep it to a sensible range. Most cars steer no more than about 0.5 radians (about 30 degrees) to each side. Only wheels with steering enabled turn. To limit steering automatically, use a VehicleMovementBehavior and its SetMaximumSteering action.

Parameters

  • value number: The steering angle in radians.
  • wheelIndex integer: The index of the wheel to steer.

UpdateAfterPhysics(number seconds)

This action is called by the layer once per frame, after physics, and moves each wheel's visual item to match its wheel. Most users never need to call it directly.

Parameters

  • seconds number: The time since the last frame, in seconds.

UpdatePhysics(number timeStep)

This action is called by the layer once for every physics step. It casts a ray under each wheel, works out the suspension and tire forces, applies them to the chassis as impulses, and updates how far each wheel has spun. Most users never need to call it directly.

Parameters

  • timeStep number: The length of the physics step, in seconds.

Inherited Actions

13 actions from Libraries.Game.Physics.PhysicsBehavior3D
3 actions from Libraries.Language.Object