Last active
April 16, 2021 01:59
-
-
Save Nimblz/ddd6298fd179213f9ab6465ca0863253 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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