I’ve almost got my car suspension to work, however, i need help with one last thing,
I need to put mass into the current formula for the suspension, and since i suck at physics, im unsure how to do it:
function GetDist(part)
local dir = part.CFrame.upVector * 100
local ray = Ray.new(part.Position, Vector3.new(0,-150,0))
local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, {Car}, false, true)
return (part.Position - pos).Magnitude
end
local d = 5
local a = 1.8 --Stiffness, higher means higher height at suspension
local b = .6 --Rigidity higher means the suspensions will get less effected by slight changes and will balance out unlike Roblox's suspensions. This is the part that dampens the suspension.
local ST = {dx=0,d2x=0} --For storing the derivatives per suspension
game:GetService("RunService").Stepped:Connect(function()
for i, Wheel in pairs(Car.Wheels:GetChildren()) do
local x = d - GetDist(Wheel) --X is how much the suspension is contracted
local f = (a*x + b*(x-ST.dx) + b/2*(x-ST.d2x)) --Use the equation
ST.d2x = ST.dx --Store the past values
ST.dx = x
--print(f)
Chassis.BodyThrust.Force = Vector3.new(0,f,0) --Apply the forces
Wheel.BodyThrust.Force = Vector3.new(0,-f,0)
end
end)
What’s your intended behaviour here? Similarly, what glitches out? I don’t tend to use Roblox’s inherited :GetMass() method, instead I do something like:
local function getMass(part)
-- this function calculates the mass of a singular part given its volume and the density as defined by the PhysicalProperties class
local density = PhysicalProperties.new(part.Material).Density
local size = part.Size
local volume = size.x * size.y * size.x
return density * volume
end
local function recurseMass(obj)
-- this function recursively calculates a model's mass by finding all the parts and calling the 'getMass' function
local mass = 0
local recurse do
function recurse(item)
for i, v in next, item:GetChildren() do
if v:IsA 'BasePart' then
mass = mass + getMass(v)
end
recurse(v)
end
end
end
return mass
end
print(
('More accurate mass of a Part is %d vs the :GetMass() method which thinks it\'s %d'):format(getMass(Instance.new 'Part'), Instance.new('Part'):GetMass())
)
-- "More accurate mass of a Part is 13 vs the :GetMass() method which thinks it's 6"
I’ve tried the mass function you sent me and it does the same thing in the video, I don’t think it’s a problem with the mass, the suspension isn’t working.
Also, i’m only getting the chassis’ mass because the wheels’ density is very low 0.01, making their mass practically 0. and the wheels and chassis are the only non-massless parts.
All four of them are the exact same, except the wheels are a bit further the front of the car, which still should matter that much if the suspension is actually working
local DriveSeat = script.Parent
local function getMass(part)
-- this function calculates the mass of a singular part given its volume and the density as defined by the PhysicalProperties class
local density = PhysicalProperties.new(part.Material).Density
local size = part.Size
local volume = size.x * size.y * size.x
return density * volume
end
local Car = script.Parent.Parent.Parent.Parent
local Chassis = Car.Body.Chassis
local Weight = getMass(Chassis) * workspace.Gravity
local Sounds = script.Parent.Parent.Parent.Parent.Sounds
local EngineSound = Sounds.Engine
local BasePlayback = 0.3
local Throttle = 1 / 15 + 1
local Ignore = Car:GetDescendants()
function GetDist(part)
local dir = part.CFrame.upVector * 100
local ray = Ray.new(part.Position, Vector3.new(0,-150,0))
local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, {Car}, false, true)
return (part.Position - pos).Magnitude
end
local d = 5
local a = 1.8 --Stiffness, higher means higher height at suspension
local b = .6 --Rigidity higher means the suspensions will get less effected by slight changes and will balance out unlike Roblox's suspensions. This is the part that dampens the suspension.
local ST = {dx=0,d2x=0} --For storing the derivatives per suspension
game:GetService("RunService").Stepped:Connect(function()
for i, Wheel in pairs(Car.Wheels:GetChildren()) do
local x = d - GetDist(Wheel) --X is how much the suspension is contracted
local f = (a*x + b*(x-ST.dx) + b/2*(x-ST.d2x)) * (Chassis:GetMass() * 17.57) --Use the equation
ST.d2x = ST.dx --Store the past values
ST.dx = x
--print(f)
Chassis.BodyThrust.Force = Vector3.new(0,f,0) --Apply the forces
if Wheel.Name == "backW" then
Wheel.BodyThrust.Force = Vector3.new(0,f,0)
else
Wheel.BodyThrust.Force = Vector3.new(0,-f,0)
end
end
end)
It shouldn’t work as all the wheels are orientated the same, the only reason in my demonstration the back came up first was because the center of balance is slightly further away from the rear wheels