I have a spring in place for a grapple gun like item and all is working well, but I want to know how to calculate the amount of studs the character is going to be brought forward each frame so I can do some extra math to prevent it from overshooting the desired target too much. The only issue is I have no clue how to do that. Any help is appreciated!
I’m still having issues with this, none of my math seems to line up.
Distance (actual distance to target) / InitialDistance (distance to target when the gun is fired) = Percentage (the percentage of the distance left to travel)
NewForce = Percentage x InitialForce
That is not what I want. I already have it in a spot I’m happy with, there’s just one issue where if the stiffness of the spring is too high, it can bring the character forward so much that they clip through things. I want to know how far a character will travel in a single frame in order to tone down the stiffness to prevent overshooting.
What do you mean by each frame?
Each time the force for the grapple gun is calculated, so Runservice.RenderStepped. Frame delta is probably going to be used in the equation I want.
Maybe:
amount of studs the char will be brought per frame… = math.abs(end position - player position)/delta
The spring stiffness is for sure going to have to be in the equation as well. The character is not being brought forward the amount of delta each time. For the spring right now, I have it so that the spring has a dampening of 0 and the stiffness is changed every frame to be a mostly consistent speed the whole way to the point. I just want to know that speed in studs per second or frame (multiplied by delta). The current code is a basic algebra equation as follows.
local power = 8000
local springLength = spring.CurrentLength - spring.FreeLength
local fixed2 = -( -power / springLength)
spring.Damping = 0
spring.Stiffness = fixed2
can you show the script where you change the stiffness?
That is the script where the stiffness if being changed. Look at the bottom line.
What it’s doing is keeping the force of the spring at a constant 8000 units (newtons?), no matter the distance.
So you want to convert the force of the spring to a velocity?
Yes, I want to get the force of the spring as a velocity.
local velocity
local force = fixed2
local power = 8000
velocity = (power/force)
convert it to studs or multiply by delta as you said
Spring force is not already in studs. I’m literally asking HOW to convert it into studs. I know that it needs to be done, I’m asking HOW it’s done through the force of the spring.
local velocity
local force = fixed2
local power = 8000
velocity = (power/force)*delta
That does not work, that gives me back the springLength,
I have yet to receive a solution to this weird problem.
Did you read my answer?
You can also change the Damping of the spring to get the same result.
If you still want to calculate it try calculating the percentage (the distance remaining / initial distance), then multiply the percentage by the Force.
That will decrease the force as the item gets closer to it’s target.
I want the system to be very specific. I made the stiffness change to counter out the length and made dampening zero just so the force would remain consistent.
local function getSpringForce(spring)
if not spring:IsA("SpringConstraint") then
warn(spring .. " is not a spring constraint!")
return
end
local currentLength = spring.CurrentLength
local freeLength = spring.FreeLength
if (spring.LimitsEnabled) then
currentLength = math.clamp(currentLength, spring.MinLength, spring.MaxLength)
freeLength = math.clamp(freeLength, spring.MinLength, spring.MaxLength)
end
local springLength = currentLength - freeLength
local axis = spring.Attachment0.WorldPosition - spring.Attachment1.WorldPosition
if axis.Magnitude > 0 then
axis = axis.Unit
end
local effectiveVelocity = spring.Attachment0.Parent.Velocity - spring.Attachment1.Parent.Velocity
-- https://en.wikipedia.org/wiki/Harmonic_oscillator
-- f = -k * x - c * dx/dt + fext
-- Gravity may not be all of the external forces; friction may affect this, but it's harder to account for
local forceExternal = Vector3.new(0, -workspace.Gravity, 0)
local force = -spring.Stiffness * springLength - spring.Damping * axis:Dot(effectiveVelocity) + axis:Dot(forceExternal)
force = math.clamp(force, -spring.MaxForce, spring.MaxForce)
return force
end
I don’t want anything other than what I’m asking for, and that’s how to convert the force value returned by the function on the spring constraint’s wiki into velocity.