Spring
A physical model of a spring, useful in many applications.
A spring is an object that will compute based upon Hooke's law. Properties only evaluate upon index making this model good for lazy applications.
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local spring = Spring.new(Vector3.zero)
RunService.RenderStepped:Connect(function()
if UserInputService:IsKeyDown(Enum.KeyCode.W) then
spring.Target = Vector3.new(0, 0, 1)
else
spring.Target = Vector3.zero
end
print(spring.Position) -- A smoothed out version of the input keycode W
end)
A good visualization can be found here, provided by Defaultio: https://www.desmos.com/calculator/hn2i9shxbz
Properties
Position
Spring.Position:
T
The current position at the given clock time. Assigning the position will change the spring to have that position.
local spring = Spring.new(0)
print(spring.Position) --> 0
p
Spring.p:
T
Alias for Spring.Position
Velocity
Spring.Velocity:
T
The current velocity. Assigning the velocity will change the spring to have that velocity.
local spring = Spring.new(0)
print(spring.Velocity) --> 0
v
Spring.v:
T
Alias for Spring.Velocity
Target
Spring.Target:
T
The current target. Assigning the target will change the spring to have that target.
local spring = Spring.new(0)
print(spring.Target) --> 0
t
Spring.t:
T
Alias for Spring.Target
Damper
Spring.Damper:
number
The current damper, defaults to 1. At 1 the spring is critically damped. At less than 1, it will be underdamped, and thus, bounce, and at over 1, it will be critically damped.
d
Spring.d:
number
Alias for Spring.Damper
Speed
Spring.Speed:
number
The speed, defaults to 1, but should be between [0, infinity)
s
Spring.s:
number
Alias for Spring.Speed
Clock
Spring.Clock:
(
)
→
number
The current clock object to syncronize the spring against.
Functions
new
Spring.
new
(
initial:
T
,
--
The initial parameter is a number or Vector3 (anything with * number and addition/subtraction).
clock?:
(
)
→
number
--
The clock function is optional, and is used to update the spring
) →
Spring
<
T
>
Constructs a new Spring at the position and target specified, of type T.
-- Linear spring
local linearSpring = Spring.new(0)
-- Vector2 spring
local vector2Spring = Spring.new(Vector2.zero)
-- Vector3 spring
local vector3Spring = Spring.new(Vector3.zero)
Impulse
Spring:
Impulse
(
velocity:
T
--
The velocity to impulse with
) →
(
)
Impulses the spring, increasing velocity by the amount given. This is useful to make something shake, like a Mac password box failing.
TimeSkip
Spring:
TimeSkip
(
delta:
number
--
Time to skip forwards
) →
(
)
Instantly skips the spring forwards by that amount time
SetTarget
Spring:
SetTarget
(
value:
T
,
--
The target to set
doNotAnimate:
boolean?
--
Whether or not to animate
) →
(
)
Sets the actual target. If doNotAnimate is set, then animation will be skipped.