Skip to content

Instantly share code, notes, and snippets.

@Nimblz
Last active Apr 16, 2021
Embed
What would you like to do?
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local character = script.Parent
local springModel = ReplicatedStorage.spring
springModel.Parent = workspace
local current = springModel.Current
local goal = springModel.Goal
local velocityAngle = 0
local velocityAxis = Vector3.new(1,0,0)
local stiffness = 3
local damping = 1
function stepSpring(dt)
local currentOrientation = current.CFrame - current.CFrame.p
local goalOrientation = goal.CFrame - goal.CFrame.p
local offset = goalOrientation:inverse() * currentOrientation
local offsetAxis, offsetAngle = offset:ToAxisAngle()
-- hookes law: F = -kx - bv
-- if you know lots of cool calculus you can do some
-- differential equations and get this as a function
-- of dt but im bad at math and dont know how that works :(
local force = -stiffness * offsetAngle
force = force - (velocityAngle * damping * (1/dt))
local dt_acceleration_mat = CFrame.fromAxisAngle(offsetAxis, force * dt)
local dt_velocity_mat = CFrame.fromAxisAngle(velocityAxis, velocityAngle) * dt_acceleration_mat
velocityAxis, velocityAngle = dt_velocity_mat:ToAxisAngle()
current.CFrame = current.CFrame * dt_velocity_mat
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment