Custom scripted suspension not working

I’m making a scripted chassis, but it doesn’t work as intended, here is my code:

local d = 2

local a = 1000 --Stiffness, higher means higher height at suspension
local b = 0.6 --Rigidity higher means the suspensions will get less affected 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

local sus_height = 4

local mass = 0;

for _, v in pairs(script.Parent:GetDescendants()) do
	if v:IsA("BasePart") then
		mass = mass + v:GetMass()
	end
end

while true do
	--For every suspension, in this case, 4 of them

	for _, Wheel in pairs(wheels) do

		local params = RaycastParams.new()

		params.FilterType = Enum.RaycastFilterType.Blacklist

		params.FilterDescendantsInstances = {Wheel}

		local ray = workspace:Raycast(Wheel.Position, Vector3.new(0, -10, 0), params)

		if ray then
			
			local distanceToGround = (Wheel.Position - ray.Position).Magnitude
			
			local suspension_distance = (sus_height - distanceToGround)

			local x = d - suspension_distance --X is how much the suspension is contracted

			local f = a*x + b*(x-ST.dx) - b/2*(x-2*ST.dx+ST.d2x) --Use the equation

			ST.d2x = ST.dx --Store the past values
			ST.dx = x

			platform.Thrust.Force = Vector3.new(0,f,0) --Apply the forces
			Wheel.Thrust.Force = Vector3.new(0,-f,0)
		else
			Wheel.Thrust.Force = Vector3.new()
		end
		game:GetService("RunService").Stepped:Wait()
	end
end

It doesn’t push the wheels down nor push the car up.

I have connected the wheels using cylindrical constraints, and I am using VectorForces instead of BodyThrusts.

Here’s an image of how the chassis looks when I run the game;

Help is appreciated, thank you for reading :slight_smile:

lowercase wheels is undefined in your code

I wouldn’t try pushing the wheels down, just use gravity for that, since no matter where the wheel is pushing down on it is just meeting solid ground, unless your wheel Force is in the platform as well.

I haven’t had issues using the Roblox SpringConstraints, you just need to make sure you tweak the SpringConstraint | Roblox Creator Documentation, SpringConstraint | Roblox Creator Documentation, SpringConstraint | Roblox Creator Documentation, and SpringConstraint | Roblox Creator Documentation so the spring behaves properly depending on the Mass of the vehicle you are supporting.

1 Like

Sorry for the late response, but I am trying to tweak the spring constraint for a bit of time, but I’m still not getting a good result on rocks, ice, basalt, etc… I’ll show what I’m trying to make the spring work depending on the mass of the car
Code;

local car = script.Parent

local force = 0
local damping = 0
local bounce = 60

local suspension = 2

local mass = 0

for _, v in pairs(car:GetDescendants()) do
	if v:IsA("BasePart") then
		mass += v:GetMass()*workspace.Gravity
	end
end

force = mass*suspension
damping = force/bounce

for _, v in pairs(car:GetDescendants()) do
	if v:IsA("SpringConstraint") then
		v.Stiffnes = mass
		v.Damping = damping
		v.MaxForce = force
	end
end

It just shakes a lot on rocks and other solid stuff. I’d appreciate a help :wink:

I’ve never done a scripted chassis before. I always build the car with the constraints in it, then fine tune it after the chassis and body are put on it.
I also make some Parts Massless to help with tuning.
I’m guessing you are trying to make a ‘one chassis fits all’ type of model.