Implementing critically damped spring equation into Roblx

So I believe many people knows this article about damped springs.

There are lot of equations, but at the end, there’s code and in that code there’s equations implemented, but I’m having problem with what should I apply velocity to, I don’t understand what is this equation calculating.

local expt = exp(-omega*dt)
local c1 = velocity + omega * x0
local c2 = (c1*dt + x0)*expt
pos = target + c2
velocity = (c1*expt) - (c2*omega)

First of all, I have car with four thrusters at each corner, those thrusters do the suspension and they use bodyThrusts for that. So I need to calculate force since bodyThrust uses force.

I just need to calculate force for critically damped scrings, because my current springs are under-Damped and doesn’t look and behave as I would like to.
I use this equation for now(Under-Damped):

local x0 =  X - (thruster.Position.Y - ray_hit_position.Y)
local X = 20 -- resting height
local K = 200 -- stiffness 
local B = 100  
local f =  base_Part:GetMass()*g + (x0*K - B*V)
4 Likes

From what I’ve read so far, the velocity is just there to increment the position value, which is what you need to apply. This script, while originally meant for spring physics, can be applied to any kind of a harmonic movement that has to be smoothed.

What do you actually mean by applying it?
That’s where I’m stuck. I don’t know where and how to apply this.

1 Like

The position value is a number ranging from the min offset to the max offset. You apply it to whatever you are trying to smooth out.

1 Like

So should I apply it here?

local x0 =  X - (thruster.Position.Y - ray_hit_position.Y)
local X = 20 -- resting height
local K = 200 -- stiffness 
local B = 100  
local f =  base_Part:GetMass()*g + (x0*K - B*V)

in x0?

1 Like

Sorry for the late response, but I only managed to get to a PC lately to test this out.

The force of a spring is calculated through Hooke’s law as F=-k*x. Damping force is calculated as F=c *v.

Thd final general equation for a damped spring would be a combination of the 2 formulas as:
F = -stiff*(PRest-PCurr)- (PCurr-PLast) /dt* c
In case of a critically damped spring, c=2sqrt(mass * stiff), with the mass of the object at the end of the spring, or the spring itself. (I changed the names of variables for the sake of an easier explanation)

In your case, you’ll probably have to edit the force per frame (kinda complicated). But this is mostly used with Euler or Verlet. With Verlet, you’d just have to calculate the acceleration from the product of forces (F ** delta ** delta) and insert it into the formula.

5 Likes

Thank you very much!!! But where’s the raycasted objects position taken into account? and is P XYZposition or only Y position

1 Like

This is a general formula, so it doesn’t matter whether you use XYZ or just Y (using a vector gives you some kind of a pendulum, using a single dimension gives you a fixed spring).
As for raycasting, you can constantly set the current position to the hit distance, where the force will try pushing away.

and PLast would be position on previous frame or height car is at currently?

1 Like

Last frame.
Gotta add more characters for this comment.

Since I’m using runService should I use stepped, ResnerStepped or HeartBeat.

1 Like