#Raycast Suspension 🚗

1 messages · Page 1 of 1 (latest)

stable heron
#

I'm making a raycast suspension system - but i need to add dampering to stop it bouncing
I pinned the video of what I have so far

Module 📜 :

local Suspesnion = {}

--// Constants \\--
local springStiffness = 300
local restLength = 1.3
local springTravel = 0.5
local wheelRadius = 1.25

local Vertices = {
    {1, 1, -1}, -- top front right
    {-1, 1, -1},-- top front left
    {1, 1, 1},  -- top back right
    {-1, 1, 1}  -- top back left
}

local function GetWheelPositions(Car : Part)

    local Size = Car.Size
    local Wheels = {}
    
    for _, Vector in pairs(Vertices) do
        table.insert(Wheels, (Car.CFrame * CFrame.new(Size .X/2 * Vector[1], Size .Y/2 * Vector[2], Size .Z/2 * Vector[3])).Position)
    end
    
    return Wheels
    
end

function Suspesnion.Suspesnion(car : Part)
    
    local Wheels = GetWheelPositions(car)
        
    for wheelName, OriginalPositon in pairs(Wheels) do
        -- Raycast Settings
        local Filters = { car }
        local RaycastParams = RaycastParams.new()
        RaycastParams.FilterDescendantsInstances = Filters
        
        local hit = game.workspace:Raycast(OriginalPositon, -car.CFrame.UpVector, RaycastParams)
        
        if hit then
            
            local currentSpringLength = (hit.Distance - wheelRadius)
            local springCompression = (restLength - currentSpringLength) / springTravel
            
            local springForce = (springStiffness + springCompression)
            
            car:ApplyImpulseAtPosition(springForce * car.CFrame.UpVector, OriginalPositon)
            
        end
    end    
end

return Suspesnion

Server 📜 :

-- Setup
local Car = game.Workspace:WaitForChild("Car")
local SuspensionModule = require(script.Suspension)

game:GetService("RunService").PreSimulation:Connect(function()
    SuspensionModule.Suspesnion(Car)
end)

Please someone help 🙏

#

Sorry about the noise 🙏

bright river
bright river
stable heron
#

how can i use spring constraint without wheels?

bright river
stable heron
#

hmm idk

#

i just need this dampering

bright river
#

spring constraint dose have dampering

stable heron
#

This is the formula for Dampering

D = Dampering
K = Spring Stiffness
M = Mass
ζ = 0.2 - 1

(2√ K*M)ζ = D
I just need to know how to apply it 😭

stable heron
#

Anyone?

deep zodiac
# stable heron Anyone?

I was working on a similar project a while ago, but was using vector forces instead of ApplyImpulse.

Here are some snippets of what I had. I dampened it by looking at the previous distances, maybe this is something you can consider.

local d = 4 --Suspension Length
local a = 1.8 --Stiffness
local b = 3.6 --Rigidity

game:GetService("RunService").Heartbeat:Connect(function()

    root.FL.VectorForce.Force = Vector3.new(0, 0, thrust_calculations(root.FL))
    root.FR.VectorForce.Force = Vector3.new(0, 0, thrust_calculations(root.FR))
    root.BL.VectorForce.Force = Vector3.new(0, 0, thrust_calculations(root.BL))
    root.BR.VectorForce.Force = Vector3.new(0, 0, thrust_calculations(root.BR))

end)

function thrust_calculations(wheel)
    
    local x = d - raycast(wheel) -- raycast() returns the distance the wheel is above the ground.

    local f = a*x + b*(x-wheel:GetAttribute("dx")) + b/2*(x-wheel:GetAttribute("d2x"))

    wheel:SetAttribute("d2x", wheel:GetAttribute("dx"))
    wheel:SetAttribute("dx", x)

    return -f * 1000
end

Hope this can be of some help.

stable heron
deep zodiac
stable heron
stable heron
deep zodiac
# stable heron and how would one go about getting those?

That's all in the code snippet. I chose to save the values as attributes on the wheel itself, so I just use GetAttribute to get the values, then use SetAttribute as shown to shift everything back one. I.e. each heartbeat, d2x gets deleted, dx becomes d2x, x becomes dx, and a new x value is created.

stable heron
fleet salmon
#

hmmm

#

idk vro 💔

#

im lowk buns at scripting

stable heron
#

lol

#

ts so complicated 🥀

fleet salmon
#

typeshi

stable heron
#
local Suspension = {}

-- Variables
local car = script.Parent.Parent
local carCFrame = car.CFrame:ToWorldSpace()

-- Constants
local d = 4
local a = 1.8
local b = 3.6

local Vertices = {
    {1, 1, -1}, -- top front right
    {-1, 1, -1},-- top front left
    {1, 1, 1},  -- top back right
    {-1, 1, 1}  -- top back left
}
local function GetWheelPositions()

    local Size = car.Size
    local Wheels = {}

    for _, Vector in pairs(Vertices) do
        table.insert(Wheels, (car.CFrame * CFrame.new(Size .X/2 * Vector[1], Size .Y/2 * Vector[2], Size .Z/2 * Vector[3])).Position)
    end

    return Wheels

end


local function CreateWheels()
    local WheelPositions = GetWheelPositions()    
    local Wheels = {}
    
    for wheelName, OriginalPosition in pairs(WheelPositions) do
        
        local Attachment = Instance.new("Attachment")
        Attachment.Position = OriginalPosition
        Attachment.Parent = car
        
        Attachment:SetAttribute("dx", 0)
        Attachment:SetAttribute("d2x", 0)
        
        table.insert(Wheels, Attachment)
        
    end
    
    for _, Wheel in pairs(Wheels) do
        local VectorForce = Instance.new("VectorForce")
        VectorForce.Parent = Wheel
    end
    
    return Wheels
end

local function raycast(Wheel)

    -- Raycast Settings
    local Filters = { car }
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = Filters

    local hit = game.workspace:Raycast(Wheel.Position, -car.CFrame.UpVector * d, raycastParams)

    return hit.Distance
    
end

local function calculate_thrust(Wheel)
    
    -- NOT MY MATH @John6774
    local x = d - raycast(Wheel)
    
    local f = a*x + b*(x-Wheel:GetAttribute("dx")) + b/2*(x-Wheel:GetAttribute("d2x"))
    
    Wheel:SetAttribute("d2x", Wheel:GetAttribute("dx"))
    Wheel:SetAttribute("dx", x)
        
    return f * 1000
    
end

local Wheels = CreateWheels()
function Suspension.Suspension()
    
    for _, Wheel in pairs(Wheels) do
        
        Wheel.VectorForce.Force = Vector3.new(0, 0, calculate_thrust(Wheel))
        

    end
    
end

return Suspension

dis is the script vro

deep zodiac
#

I'll take a look at this when I have a chance.

stable heron
deep zodiac
#

So you might have it the wrong way around

stable heron
#

alr thanks