Physics Balancing With BodyThrust

So after spending wayyyyyy to much time trying to find the perfect suspension system with constraints I finally called it quits and started doing research on how other games on different platforms handle car suspension/Physics. Eventually I came across the Ray Cast and apply thrust upwards on the car. I can’t seem to figure out how to balance the forces properly. Even after looking at the standard Roblox Jeep I have no idea how to actually make this work nicely.


local Thrusters = {}

local Mass = 0;
local Height = 5;

local Up = Vector3.new(0,1,0)

for _,v in ipairs(script.Parent:GetDescendants()) do
	if v:IsA("BasePart") then
		Mass = Mass + (v:GetMass() * game.Workspace.Gravity);
	end	
end

for _,v in ipairs(script.Parent:GetChildren()) do
	if v.Name == "Thruster" then
		table.insert(Thrusters,v)
	end
end

--table.insert(Thrusters,script.Parent.Thruster)


game:GetService("RunService").Stepped:Connect(function()
	for _,v in ipairs(Thrusters) do
		local BodyThrust = v:FindFirstChild("BodyThrust")
		if not BodyThrust then
			BodyThrust = Instance.new("BodyThrust", v)
		end		
		local Cast = Ray.new(v.Position,(Vector3.new(0,-1,0).unit) * Height)
		local Part,Position = game.Workspace:FindPartOnRayWithIgnoreList(Cast,{v})
		if Part and Position then
			local Previous = BodyThrust.Force
			local CompressionRatio = ((v.Position - Position).magnitude / Height) 
			BodyThrust.Force = Vector3.new(0, CompressionRatio * Height *  (Mass)/4,0)
			v.BillboardGui.TextLabel.Text = math.ceil(CompressionRatio * 100)/100
		else
			BodyThrust.Force = Vector3.new()
		end
	end
end)

1 Like

I think that @ScriptOn has made something similar in the past. Maybe he knows the calculations?

1 Like

You need PiD. Basically physics negating springs that tell the car how hard it can thrust up/down to stay stable.

4 Likes

I should probably make a tutorial for this at some point, this question has come up alot recently. For some reason I cannot find the thread I normally link to using the search function, so tl;dr, the default Roblox jeep has a good suspension for you to use as a starting point.

2 Likes

would be use full,

You could essentially apply Hooke’s law with some derivative term in order to create a force that restores your position, in this case, height only, back to a desired value that you wish to hover at.

Like some are saying, simple PID Control on ROBLOX is sufficient in order to stabilize and converge, and is already relatively advanced in this context.

(if you really want to have fun, you could derive a state-space model and use pole-placement for a nice and optimal control scheme :slight_smile: way too overkill though)

Would it help to change the force of X and Z to something other than 0? Check to see if the model in action changes these values at all too and if so, maybe setting x to the bodythrust.force.x and same for z instead of zero could be something that makes things different better or worse.

Thinking of doing something similar but using VectorForce my goal is to align a plank with terrain, the plank is welded to the character which causes it to always have an orientation of v3.new(0,~,0)

Should i do this?