VehicleMovementBehavior
Package: Libraries.Interface.Behaviors.Games · Inherits from: Libraries.Language.Object, Libraries.Interface.Behaviors.Behavior
A VehicleMovementBehavior drives a Vehicle. It turns three simple commands into engine, steering, and brake values for the vehicle's wheels: 1. Throttle, from -1 to 1. Positive values drive forward and negative values drive in reverse. 2. Steering, from -1 to 1. Positive values turn left and negative values turn right. 3. Braking, from 0 to 1. By default, the commands come from the keyboard: the up and down arrows for throttle, the left and right arrows for steering, and the space bar for the brake. The keys can be changed. To drive the vehicle some other way, such as from a computer-controlled player, turn the keyboard off with SetUseKeyboard(false) and set the commands with SetThrottle, SetSteering, and SetBraking. A VehicleMovementBehavior is a Behavior, so the easiest way to use it is to add it to the vehicle's chassis with AddBehavior. The chassis then updates the behavior once per frame, before the physics runs, so the vehicle responds to input in the same frame. It can also be updated by hand, by calling its Update action from the game's Update action instead.
Example Code
use Libraries.Game.Physics.Vehicles.Vehicle
use Libraries.Interface.Behaviors.Games.VehicleMovementBehavior
// After the vehicle, its chassis, and its wheels are set up:
VehicleMovementBehavior movement
movement:SetVehicle(vehicle)
movement:SetEngineForce(4200)
movement:SetReverseForce(2800)
movement:AddDriveWheel(0)
movement:AddDriveWheel(1)
chassis:AddBehavior(movement)
Actions
AddBrakeWheel(integer index)
This action makes the wheel at the given index a brake wheel, which the brake is applied to. If no brake wheels are added, every wheel is a brake wheel.
Parameters
indexinteger: The index of the wheel in the vehicle.
AddDriveWheel(integer index)
This action makes the wheel at the given index a drive wheel, which the engine force is applied to. If no drive wheels are added, every wheel is a drive wheel.
Parameters
indexinteger: The index of the wheel in the vehicle.
Dispose()
This action is called when the behavior is removed from an item. It releases the vehicle's engine, steering, and brakes, so the vehicle does not keep driving on its own.
EmptyBrakeWheels()
This action removes all brake wheels, so that every wheel is a brake wheel again.
EmptyDriveWheels()
This action removes all drive wheels, so that every wheel is a drive wheel again.
GetBrakeForce()
This action returns the braking force applied to each braked wheel at full braking, in newtons. The default is 1500. The force relates to how quickly the vehicle slows down by this equation: brake force = mass * deceleration / braked wheels The mass is in kilograms, and the deceleration is in meters per second each second. Turned around, the vehicle slows at about brake force * braked wheels / mass. For example, with the default of 1500, an 800 kilogram vehicle with four braked wheels slows at about 1500 * 4 / 800 = 2.5 meters per second each second, about a quarter of gravity.
Returns
number: The braking force for each braked wheel, in newtons.
GetBrakeKey()
This action returns the key that brakes.
Returns
integer: The key code.
GetBraking()
This action returns the braking command, from 0 for no brake to 1 for full brake.
Returns
number: The braking command.
GetEngineForce()
This action returns the engine force used at full forward throttle.
Returns
number: The forward engine force.
GetForwardKey()
This action returns the key that drives forward.
Returns
integer: The key code.
GetLeftKey()
This action returns the key that steers left.
Returns
integer: The key code.
GetMaximumSteering()
This action returns the steering angle used at full steering, in radians.
Returns
number: The largest steering angle.
GetReverseForce()
This action returns the engine force used at full reverse throttle.
Returns
number: The reverse engine force, as a positive number.
GetReverseKey()
This action returns the key that drives in reverse.
Returns
integer: The key code.
GetRightKey()
This action returns the key that steers right.
Returns
integer: The key code.
GetSteering()
This action returns the steering command, from -1 for full right to 1 for full left.
Returns
number: The steering command.
GetSteeringSpeed()
This action returns how fast the steering moves toward the steering command, in steering units per second. 0 means it moves there at once.
Returns
number: The steering speed.
GetThrottle()
This action returns the throttle command, from -1 for full reverse to 1 for full forward.
Returns
number: The throttle command.
GetThrottleSpeed()
This action returns how fast the throttle moves toward the throttle command, in throttle units per second. 0 means it moves there at once.
Returns
number: The throttle speed.
GetVehicle()
This action returns the vehicle this behavior drives.
Returns
Libraries.Game.Physics.Vehicles.Vehicle: The vehicle, or undefined if none has been set.
IsActive()
This action returns whether the behavior is active. An inactive behavior sends no throttle, steering, or brake to the vehicle.
Returns
boolean: true if the behavior is active, and false otherwise.
IsFinished()
This action returns whether the behavior is finished. A behavior added with AddBehavior keeps running until Stop is called.
Returns
boolean: true if Stop has been called, and false otherwise.
IsUsingKeyboard()
This action returns whether the behavior reads the keyboard.
Returns
boolean: true if the keyboard is used, and false otherwise.
SetActive(boolean active)
This action turns the behavior on or off. While it is off, it sends no throttle, steering, or brake to the vehicle and ignores the keyboard. This is useful when the same keys are used for something else for a while, such as operating a forklift's mast.
Parameters
activeboolean: true to turn the behavior on, false to turn it off.
SetBrakeForce(number force)
This action sets the braking force applied to each braked wheel at full braking, in newtons. The default is 1500. To choose a value, multiply the vehicle's mass by how quickly it should slow down, and divide by the number of braked wheels: brake force = mass * deceleration / braked wheels The mass is in kilograms, and the deceleration is in meters per second each second. Gravity is about 9.8, so 4.9 gives a gentle stop and 7.5 to 9 gives a hard stop. For example, an 800 kilogram vehicle with four braked wheels and a deceleration of 7.5 needs 800 * 7.5 / 4 = 1500. Very strong brakes can tip a tall vehicle forward, especially with grippy tires. If the vehicle flips when braking, lower this value.
Parameters
forcenumber: The braking force for each braked wheel, in newtons.
SetBrakeKey(integer key)
This action sets the key that brakes. The default is the space bar.
Parameters
keyinteger: The key code.
SetBraking(number braking)
This action sets the braking command, from 0 for no brake to 1 for full brake. Values outside that range are limited to it. When the keyboard is turned on, the keyboard replaces this value every frame.
Parameters
brakingnumber: The braking command.
SetEngineForce(number force)
This action sets the engine force used at full forward throttle. The default is 1000.
Parameters
forcenumber: The forward engine force.
SetForwardKey(integer key)
This action sets the key that drives forward. The default is the up arrow. Use the constants in KeyboardEvent, such as keys:W.
Parameters
keyinteger: The key code.
SetLeftKey(integer key)
This action sets the key that steers left. The default is the left arrow.
Parameters
keyinteger: The key code.
SetMaximumSteering(number angle)
This action sets the steering angle used at full steering, in radians. The default is 0.3. The vehicle may limit the angle further.
Parameters
anglenumber: The largest steering angle.
SetReverseForce(number force)
This action sets the engine force used at full reverse throttle, as a positive number. The default is 1000.
Parameters
forcenumber: The reverse engine force.
SetReverseKey(integer key)
This action sets the key that drives in reverse. The default is the down arrow.
Parameters
keyinteger: The key code.
SetRightKey(integer key)
This action sets the key that steers right. The default is the right arrow.
Parameters
keyinteger: The key code.
SetSteering(number steering)
This action sets the steering command, from -1 for full right to 1 for full left. Values outside that range are limited to it. When the keyboard is turned on, the keyboard replaces this value every frame.
Parameters
steeringnumber: The steering command.
SetSteeringSpeed(number speed)
This action sets how fast the steering moves toward the steering command, in steering units per second, where 1 unit is full steering to one side. For example, a speed of 2 takes half a second to turn from straight ahead to full left. The default, 0, moves there at once.
Parameters
speednumber: The steering speed.
SetThrottle(number throttle)
This action sets the throttle command, from -1 for full reverse to 1 for full forward. Values outside that range are limited to it. When the keyboard is turned on, the keyboard replaces this value every frame.
Parameters
throttlenumber: The throttle command.
SetThrottleSpeed(number speed)
This action sets how fast the throttle moves toward the throttle command, in throttle units per second, where 1 unit is full throttle. The default, 0, moves there at once.
Parameters
speednumber: The throttle speed.
SetUseKeyboard(boolean useKeyboard)
This action sets whether the behavior reads the keyboard. When it does, the keyboard sets the throttle, steering, and braking commands every frame. Turn it off to set the commands yourself.
Parameters
useKeyboardboolean
SetVehicle(Libraries.Game.Physics.Vehicles.Vehicle vehicle)
This action sets the vehicle this behavior drives.
Parameters
vehicleLibraries.Game.Physics.Vehicles.Vehicle: The vehicle to drive.
Stop()
This action stops the behavior. If it was added to an item with AddBehavior, the item removes it at the start of the next frame. The vehicle's engine, steering, and brakes are released.
Update(number seconds)
This action is called once per frame when the behavior has been added to an item with AddBehavior. It can also be called by hand from a game's Update action. It reads the keyboard, if the keyboard is turned on, and then sends engine, steering, and brake values to the vehicle.
Parameters
secondsnumber: The time since the last frame, in seconds.