MotorVehicle
Chassis Programming Made Easy
About
Programming chassis in video games is a very overwhelming, difficult, as well as tedious process that is hard to get it just right, this library was written to solve this problem. It’s also a great place to dissect the code to learn on how to write your own chassis.
If you are planning on making a realistic car racing game, it’s probably best in your interest to avoid this library as it is not meant to be used in racing games. But if you are making a game like Bloxburg, Emergency Response: Liberty County, or something that doesn’t primarily focus on motor vehicles and you want to save yourself some time, this is probably a library for you.
Showcase
Getting Started
You will need to get the library and initialize a new instace of the class.
Source Code: GitHub
Roblox Model: Roblox
Example Place:
MotorVehicle Example Project.rbxl (764.4 KB)
local bus = MotorVehicle.new({
root = VehicleSeat,
wheels = {Chassis.RL, Chassis.RR},
torque = 1000,
maxSteerAngle = 45,
turnSpeed = 3,
gearRatio = {2.97, 2.07, 1.43, 1.00, 0.84, 0.56},
})
And whenever you are ready, you can compute properties via the method below:
local output = bus:compute(deltaTime, {
steerFloat = -VehicleSeat.SteerFloat,
throttleFloat = VehicleSeat.ThrottleFloat
})
Documentation
You will need to know how to rig a vehicle before doing this, I highly recommend watching this tutorial.
Initialization Configuration
root
Type: BasePart
The most important part of the vehicle, it will be used to determinate certain things like direction, speed, etc.
wheels
Type: {BasePart}
An array of wheels that will be driven (aka “powered”) by the motor, those will be used to determinate certain states, the library will not drive them, that will be your job to do so.
torque
Type: number
(preferablly an integer)
The measurement of your car’s ability to do work, meaning that the more torque, the greater amount of power an engine can produce. If your engine has a lot of torque, your car can accelerate more quickly when the vehicle is beginning to start (explanation taken from kia website).
maxSteerAngle
Type: number
(preferablly an integer)
Determinates how far the wheel can be turned (angle in degrees).
turnSpeed
Type: number
Arbitrary number which defines the top speed at which the wheels can turn.
gearRatio
Type: {number}
(preferablly a float)
Ordered array of floats that determinate ratio per gear (their order matters as the system takes ratio from the left side and as the gear goes up the index which is used to take the gear ratio will also increase, move to the right).
Image source: https://www.theengineerspost.com/gear-ratio/
Methods
getPitch
Returns: number
Computes pitch for the engine sound. Make sure to perform extra math operations on the returned value to suits your specific audio (eg. pitch + 0.5
). It automatically clamps the number to prevent large spike when vehicle falls or glitches out (aka you won’t go deaf when the car glitches out at 3000 studs per second).
getSpeed
Returns: number
Determinates the vehicle speed and returns it. The returned value is always positive as your day should be :)
(no pun intended)
getThrottleState
Returns: number
Determinates whether the vehicle is in neutral, breaking, or accelerating. You can take the returned value and compare it against an ThrottleState
enum included in Enums
module script.
eg.
getThrottleState() == Enums.ThrottleState.Breaking
getDirection
Returns: number
Determinates whether the vehicle is going forward, backwards, or is resting in place.
-
1
moving forward -
0
resting in place -
-1
moving backwards
compute
Returns:
{
angularVelocity: number, -- Angular velocity to be applied to vehicle's motor(s)
angle: number, -- Turn angle for the wheels
motorMaxTorque: number, -- Maximum torque that the vehicle's motor(s) can run at
motorMaxAngularAcceleration: number -- Maximum angular acceleration that the vehicle's motor(s) can have
}
Parameters:
deltaTime: number,
config: { steerFloat: number, throttleFloat: number }?
Computes values required to make the vehicle react to a given input.
eg:
for _, wheelAtt in wheelAtts do
wheelAtt.Orientation = Vector3.new(0, output.angle, 90)
end
for _, motor in motors do
motor.MotorMaxAngularAcceleration = output.motorMaxAngularAcceleration
motor.MotorMaxTorque = output.motorMaxTorque
motor.AngularVelocity = output.angularVelocity
end