# I need help with raycast springs

So, I am trying to make a spring system using raycasts for a suspension. What is the best method to use for this? I don’t know where to start and I need help with some of the math. Mostly, I need to know how to interact with forces on ROBLOX. How would you go about applying the equation f = -kx or f=kx ?

3 Likes

This thread should have everything you’re looking for.

2 Likes

I appreciate this, but I would like an explanation on how to implement this from scratch, and how to do it with the ROBLOX physics directly. I don’t want to use a library or any constraints (I know you weren’t talking about constraints, just wanted in case someone else replies)

2 Likes

The thread contains multiple exams of Hook’s law, Newton’s second law and the formula for dampening. The thread gives you an idea of how these formulas combine to make a spring module of your own.

To make a physics simulation for a spring, you can use RunService, more specifically, the `.Heartbeat` signal. The signal returns a `deltaTime` (dt for short) You can use this argument for updating a spring properly.

Remember, simulating springs requires exponentiation and summation (Of course custom spring modules may differ) It’s something I can’t describe in detail within this thread (because it’d take me days to write out)

If you have a good understanding of these “laws” and “formulas”, creating a value based spring system won’t be hard. If you’d like to read about @ Quenty’s spring module, here’s his take on it.

I can’t elaborate on much more, however if you’re looking for more sources, I can help whenever. I hope you find what you’re looking for though, good luck

1 Like

I’ve seen that thread before and I know it has formulas and an explanation of the math, but what I need is how to use that on ROBLOX (the imagination platform). I am aware of the formulas behind it but how do you apply the forces or what would you do to control an object’s motion? I do really appreciate your help though.

1 Like

Read the module and the code within it. You can use OOP to create a spring class via a `ModuleScript`. Here’s a really vague and abstract example:

``````local spring = {}
spring.__index = spring

-- functions
function spring.new()
local self = {}
setmetatable(self, spring)

self.position = Vector3.zero
self.velocity = Vector3.zero

return self
end

function spring:Update()
-- Here you'd do the actual math
-- Exponentiation and or summation
end

function spring:GiveVelocity(velocity: Vector3)
-- This method gives the "spring" velocity/momentum which will be broken down via the ":Update()" method.
self.velocity += velocity
end

return spring
``````

Remember, the `:Update()` method is only used to break down the given forces (in this case velocity) and then apply it to the current “position”.

Imagine you push something, you give that “something” momentum (in our case I’ll refer to it as velocity) that velocity breaks down over time through friction and air resistance, right? The same applies here.

With the abstract spring class I provided you with, here’s an example of a spring being created via a `Script`:

``````local spring = require(PATH_TO_MODULE)

-- this is the code for actually "updating" the spring every physics step:
runService.Heartbeat:Connect(function(deltaTime: number)
mySpring:Update()
end)

local mySpring = spring.new()
local currentPosition = mySpring.position -- this is the position of the spring, in other words, the "rest" position.

-- let's apply some velocity to our spring:
mySpring:GiveVelocity(Vector3.new(1, 0, 0)) -- giving force to the x axis

-- now over time the velocity breaks down and the spring settles at its "rest" position.
-- but right now we've given it velocity, the "rest" position is altered and is moving about

print(mySpring.position) -- 0.892, 0, 0

task.wait(2) -- wait a couple seconds for the spring to break down the velocity

print(mySpring.position) -- 0, 0, 0
``````

Keep in mind that this is really abstract and you should look into these classes and modular programming things a bit more.

That aside though, I’m sure you’re wondering how do you translate this “position” value to something such as a part?

Well let’s use a part as our example. When we “call” `spring.position`, we’re actually getting the spring’s position in that current time. We can translate the position into a vector with `Vector3.new(x, y, z)`. In our `.Heartbeat` signal, we can add in something like this to account for that `position`:

``````runService.Heartbeat:Connect(function(deltaTime: number)
mySpring:Update()

part.Position = mySpring.position -- part being a reference to a BasePart
end)
``````

I for sure could have explained everything a lot better, but I think this should somewhat guide you in a desired direction.

That’s all I can really add onto my original reply. If you need any assistance though, I’d be happy to help out. Good luck (again)

2 Likes

Constraints is how you interact with the physics engine in Roblox. For a simulated car (without setting up SpringConstraints & Prismatics) you’d probably want to use a vector force at each axle, and just apply the solved reactionary spring force to that.

If you wanted to simulate the physics yourself, you’d want to use this equation to resolve the acceleration at each axle, then update the CFrame as described by @weakmetatable

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.