Provide an overview of:
-
What does the code do and what are you not satisfied with?
The code works when uninterrupted but my vehicle freaks out when any weight is added along with a slight jitter when at its rest point. An example of both
-
What potential improvements have you considered?
I attempted to fix this with the method of reduced mass (not actually lowering the mass) but by calculating what mass was needed for stability from colliding parts. -
Where did I get the formulas?
I got them from Towards a Simpler, Stiffer, and more Stable Spring - Math and Physics - Tutorials - GameDev.net
I’ll leave the project file and the raycast code below. Also, I’m sorry to drop this on the dev forum but I’m completely clueless as to what to do now.
Raycast Car.rbxl (33.0 KB)
*Edit - I cleaned up the place file
local RunService = game:GetService("RunService")
--stats
local Car = script.Parent
local carStats = Car.Stats
--Global Variables
local previousPosition = Vector3.new()
--Raycast Params
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {Car.Body,Car.mass}
for _, part in pairs(Car:GetChildren()) do
if part.Name == "Thruster" or part.Name == "Thruster2" or part.Name == "Thruster3" or part.Name == "Thruster4" then
local connection = RunService.Heartbeat:Connect(function(deltaTime)
local raycastResult = workspace:Raycast(part.Position, part.CFrame.LookVector * (carStats.Height.Value), raycastParams)
raycastParams.FilterDescendantsInstances = {Car.mass,Car.Body}
if raycastResult then
local timestep = deltaTime
local Force
local function getPointVelocity(object, point)
return object.Velocity + object.RotVelocity:Cross(point - object.Position)
end
local pointVelocity = getPointVelocity(Car.Body, part.Position)
local suspDir = part.CFrame.LookVector
previousPosition = Vector3.new(0,pointVelocity.Y,0)
local RestLength = carStats.Height.Value --r
local currentLength = part.Position.Y - raycastResult.Position.Y --p
local displacement = currentLength-RestLength --x
local mass = 800
local dampingForce2 = mass/timestep
local springForce2 = mass/(timestep^2)
local Ck = tonumber(carStats.Ck.Value) --SpringForce
local Cd = tonumber(carStats.Cd.Value) --DampingForce
--f=-(m/t^2)*Ck*x -(m/t)*Cd*v
local Force3 = -springForce2*Ck*(displacement) -dampingForce2*Cd*(pointVelocity.Y)
part.BodyThrust.Force = Vector3.new(0,0,Force3)
else
part.BodyThrust.Force = Vector3.new()
end
end)
end
end