Car Suspension (Scripted)

Open studio settings, and scroll down until you see the setting “Show Diagnostics Bar” and enable that, it will then show ur framerate on the bottom right hand corner of your screen.

1 Like

Why are you calculating Force for Same bodythrust twice?

The second time is the damping force, used to smoothen out the suspension and help prevent excessive bouncing.

1 Like

What is O and is thrusterHeight Y position of the thruster and Height the desirable one?

ThrusterHeight is the distance between the thruster and the ground, and O is what I call a ‘Physics Packet’ since this code did come from my Chassis.

2 Likes

So what Should I do with O?

30 characters :smiley:

O is irrelevant in this scenario, I tried to take it out as much as possible but the only pieces of it used in this bit is O.Height (Which is just the desired height) O.Suspension (Which is the Stiffness of the suspension) and O.Mass (Which is vehicle mass)

1 Like

https://gyazo.com/ffbe4bf9caf9b858d0c40ddc0b00e213 the result is not good, because its calculating less amount off force and doesn’t tilt at all, but maybe I do something wrong.

           X = 10
           x0 = thruster.Pos.Y - hit_position.Y

	local force = model.Engine:GetMass() * 3
	local damping = (model.Engine:GetMass() * 3) * 1/(1/dt)--0.01--force / Bounce

	f = Vector3.new(0, ((X - x0)^2) * (force / X^2) * math.min(1,(1/dt)/60), 0)
	local thrusterDamping = i.Part.CFrame:toObjectSpace(CFrame.new(i.Part.Velocity + i.Part.Position)).p * damping-- * 1/(1/aj)
	f = f - Vector3.new(0, thrusterDamping.Y * math.min(1,(1/dt)/60), 0)
			thruster.BodyThrust.Location = Vector3.new(0,1,0)--b8.CFrame.upVector
1 Like

First off, you are just getting the mass of the engine, what I would do instead is

function getMass(model)
  local mass = 0
  for _,v in ipairs(model:GetDescendants()) do
    if v:IsA("BasePart") then mass = mass + v:GetMass() end
  end
  return mass
end

and I don’t see any code where you actually apply ‘f’

1 Like

everything else is massless, so I don’t need to do that. in terms of applying force, I just do

thruster.BodyThrust.Force = Vector3.new(0, 4*f, 0 )

why 4*f, instead I would prob do Vector3.new(0, workspace.Gravity * f, 0) which might help out a bit.

That’s smart of you, but it doesn’t change much, it still is not tilting. But raising stiffness to even 1000 doesn’t help either. My formula works fine if not stuttering

can you post the place file here so I can test some different things in it?

SuspensionVehicle.rbxm (15.5 KB)
here’s file with all the scripts

3 Likes

might be an issue on my end, but I can’t download it. Can you post a link to an uncopylocked Game?

Solution to the vehicle is that it might have a bodygyro and that might cause it!

Hi i have made a suspension car and i dont know if i can reduce the amount of recources that it uses as my game will have driving NPCs that use the vehicles that you can steal from them.
I need at least 50 vehicles and i struggle to get this done with the limits of roblox
the activity idles around 1% and that is not good for the rate. Any help will be taken
code down below.

local RS = game:GetService("RunService")
local VehicleSeat = script.Parent.Body.DriveSeat
--Vehicle controller
VehicleSeat.Anchored = true
local CenterPart = Instance.new("Part")
CenterPart.Size = Vector3.new(1,1,1)
CenterPart.CFrame = script.Parent:GetModelCFrame()
CenterPart.Parent = script.Parent
CenterPart.Name = "CenterPart"
local Weld = Instance.new("WeldConstraint")
Weld.Parent = CenterPart
Weld.Part0 = CenterPart
Weld.Part1 = VehicleSeat
VehicleSeat.Anchored = false
Mass = 0
Vehicle = script.Parent
for i,x in pairs(Vehicle:GetDescendants()) do
	if x:IsA("BasePart") then
		Mass = Mass + x:GetMass()*game.Workspace.Gravity
	end
end
-- Settings
Bounce = 200
Height = 4
Suspension = 4
Traction = 10000000
for i,x in pairs(Vehicle.Edges:GetChildren()) do
	local Weld = Instance.new("Motor6D")
	Weld.Parent = x.Wheel
	Weld.Name = "SuspensionWeld"
	Weld.Part0 = x.Wheel
	Weld.Part1 = x
end
--
force = Mass * Suspension
damping = force / Bounce
--
while true do
	game:GetService("RunService").Stepped:wait()
	--VehicleSeat.Velocity = VehicleSeat.Velocity*0.999*(1-0.01)
	for a,Thruster in pairs(Vehicle.Edges:GetChildren()) do
		if Thruster.Wheel:FindFirstChild("SuspensionWeld") ~= nil then
			local bodyThrust = Thruster.BodyThrust
			local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(Ray.new(Thruster.Position, Thruster.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * Height),{script.Parent})
			local thrusterHeight = (position - Thruster.Position).magnitude
			if hit then
				--If we're on the ground, apply some forces to push the wheel up
				bodyThrust.force = Vector3.new(0, ((Height - thrusterHeight)^2) * (force / Height^2), 0)
				local thrusterDamping = Thruster.CFrame:toObjectSpace(CFrame.new(Thruster.Velocity + Thruster.Position)).p * damping
				bodyThrust.force = bodyThrust.force - Vector3.new(0, thrusterDamping.Y, 0)
			else
				bodyThrust.force = Vector3.new(0, 0, 0)
			end
			Thruster.Wheel.SuspensionWeld.C0 = CFrame.new(0.5,math.min(thrusterHeight,Height * 0.8) - (Thruster.Wheel.Size.Y / 2), 0)*CFrame.Angles(0,math.rad(180),0)
			Thruster.Traction.MaxForce = Vector3.new(Traction,0,Traction)
			Thruster.Traction.Velocity = Vector3.new()
		end
	end
end

It works with no issues, just it causes alot of activity. You can use this code if you so want to!

3 Likes

I would still recommend using an existing chassis from another player if possible - it took me about a month to get a basic car chassis that works reliably and with decent handling from scratch, and many more to get a chassis that was release worthy, and that’s with a lot of background in both Roblox and physics.

If you’re still quite new to Roblox it’s quite a big leap in difficulty and may be more effort than it’s worth if cars aren’t the number 1 focus of the game.

1 Like

I myself am current revisiting this whole raycasting chassis topic again, and this post too- I’ve been looking over this post since about this time last year and I was surprised to find it’s somehow active again?

The thing I’ve been struggling with is actually damping the springs- I just don’t get the formula. If I could get that down, I could easily make the rest of the chassis with no issue, so it sucks that one thing is holding me back.
I think the best I can do for now is looking over all these new resources and study how they apply damping.

1 Like

What part of it is causing problems? Just getting the equation to work at all, or tuning it correctly?

1 Like

If you want, I got a model that uses scripted suspension and it uses around 0.5% - 2% of activity


I hope this helps with your work!

1 Like