Raycast Suspension System not Leveling Out

I am currently trying to develop a suspension system that uses raycast to level itself out. It currently works when travelling in straight paths but once you bank it starts rolling over and not self-correcting. This is the code that updates the red blocks or thrusters.

--Separate setup code for each wheel. (v = thruster)
TD[v] = {DX = 0, D2X = 0, Limit = v.Limit.Value, Stiffness = v.Stiffness.Value, Rigidity = v.Rigidity.Value}


local Data = TD[Thruster]
local BF = Thruster:FindFirstChild("BodyForce")
local TWeld = Thruster.WheelWeld
local Hit, Pos, Norm = ThrusterCast(Thruster.Position, Thruster.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * (Data.Limit * 2))
local Distance = (Thruster.Position - Pos).Magnitude
							
local x = Data.Limit - Distance
local f = Data.Stiffness * x + Data.Rigidity*(x-Data.DX) + Data.Rigidity/2*(x-Data.D2X)
							
TD[Thruster].D2X = Data.DX
TD[Thruster].DX = x
							
							
BF.Force = Vector3.new(0, f * (Mass / #Thrusters * 2), 0)
TWeld.C1 = CFrame.new(0, math.min(Distance - Thruster.Wheel.Size.Y / 2, Data.Limit), 0) * CFrame.Angles(0, math.rad(180), 0)

This is based off the code that I got here with some modifications. The value in the middle is always the negative weight of the vehicle doubled. From my understanding because the BodyForces on the right side of the vehicle are negative while the left are positive they should level out.


5 Likes

Why is it positive on one side and negative on the other?

The forces on the positive side are trying to pull that side of the vehicle up while the negative side is trying pull the vehicle down to balance it. Under normal circumstances both sides are positive. I found by setting the force to 0 when the thruster was too high was even less effective for me.

Are you making car or hover car?
because I have been working on hoverCars recently and have some results I can share
That’s all I use for equation, maybe it is not full scaled but it works for many instances:
m = mas of the car
g = gravity
F = mg
X = target height
y = y position of car
hit_position = position of object hit by raycasting
x0 = X - (y - hit_position.Y)
K = stiffness
B = resistance coefficient
V = Y Velocity
f = F + (x0K - BV)

1 Like

How are we supposed to use these variables to try to solve what issue is posted in the OP though?

1 Like

I am making a car but the principles for a car and hover car from what I can understand are the same. Due to the fact the wheels are non-collided and are simply there to look good. As for the variables you provided I am already using them in a equation provided by Oseday.

Issue was BodyForces were keeping the vehicle floating even after the force was changed. This issue was corrected by using BodyThrusts instead.

Slightly unrelated (as this issue has been solved), but for this case, you probably want to use a VectorForce and not a BodyForce for performance reasons.

1 Like

Note taken. I’ll convert my system to use VectorForces later. What would the substitute for a BodyAngularVelocity be?

Torque.

1 Like

Alright thank you.