Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSpring.lua
More file actions
Latest commit
182 lines (148 loc) · 4.64 KB
/
Copy pathSpring.lua
File metadata and controls
182 lines (148 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
-- Simulates the motion of a critically damped spring
-- @author fractality
--[[
MIT License
Copyright (c) 2020 Parker Stebbins
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
------------------------------------------------------------------------
-- API:
-- Spring Spring.new(double damp, double freq, vector pos)
-- void Spring:SetGoal(vector goal)
-- void Spring:SetFrequency(double freq)
-- void Spring:SetDampingRatio(double damp)
-- vector Spring:GetPosition()
-- vector Spring:GetVelocity()
-- vector Spring:Update(double dt)
-- void Spring:Reset(vector state)
--
-- Notes:
-- The state vector type must implement the following metamethods:
-- vector __mul(vector, double)
-- vector __add(vector, vector)
-- vector __sub(vector, vector)
------------------------------------------------------------------------
localSpring= {}
Spring.__index=Spring
localpi=math.pi
localexp=math.exp
localsin=math.sin
localcos=math.cos
localsqrt=math.sqrt
localEPS=1e-4
functionSpring.new(dampingRatio, frequency, position)
assert(type(dampingRatio) =="number")
assert(type(frequency) =="number")
assert(dampingRatio*frequency>=0, "Spring does not converge")
returnsetmetatable(
{
d=dampingRatio,
f=frequency,
g=position,
p=position,
v=position*0-- Match the original vector type
},
Spring
)
end
functionSpring:Reset(position)
self.p=position
self.v=position*0
end
functionSpring:SetGoal(newGoal)
self.g=newGoal
end
functionSpring:SetFrequency(newFreq)
self.f=newFreq
end
functionSpring:SetDampingRatio(newDamp)
self.d=newDamp
end
functionSpring:GetGoal()
returnself.g
end
functionSpring:GetPosition()
returnself.p
end
functionSpring:GetVelocity()
returnself.v
end
functionSpring:Update(dt)
locald=self.d
localf=self.f*2*pi
localg=self.g
localp0=self.p
localv0=self.v
localoffset=p0-g
localdecay=exp(-d*f*dt)
localp1, v1
ifd==1then-- Critically damped
p1= (offset* (1+f*dt) +v0*dt) *decay+g
v1= (v0* (1-f*dt) -offset* (f*f*dt)) *decay
elseifd<1then-- Underdamped
localc=sqrt(1-d*d)
locali=cos(f*c*dt)
localj=sin(f*c*dt)
-- Damping ratios approaching 1 can cause division by small numbers.
-- To fix that, group terms around z=j/c and find an approximation for z.
-- Start with the definition of z:
-- z = sin(dt*f*c)/c
-- Substitute a=dt*f:
-- z = sin(a*c)/c
-- Take the Maclaurin expansion of z with respect to c:
-- z = a - (a^3*c^2)/6 + (a^5*c^4)/120 + O(c^6)
-- z ≈ a - (a^3*c^2)/6 + (a^5*c^4)/120
-- Rewrite in Horner form:
-- z ≈ a + ((a*a)*(c*c)*(c*c)/20 - c*c)*(a*a*a)/6
localz
ifc>EPSthen
z=j/c
else
locala=dt*f
z=a+ ((a*a) * (c*c) * (c*c) /20-c*c) * (a*a*a) /6
end
-- Frequencies approaching 0 present a similar problem.
-- We want an approximation for y as f approaches 0, where:
-- y = sin(dt*f*c)/(f*c)
-- Substitute b=dt*c:
-- y = sin(b*c)/b
-- Now reapply the process from z.
localy
iff*c>EPSthen
y=j/ (f*c)
else
localb=f*c
y=dt+ ((dt*dt) * (b*b) * (b*b) /20-b*b) * (dt*dt*dt) /6
end
p1= (offset* (i+d*z) +v0*y) *decay+g
v1= (v0* (i-z*d) -offset* (z*f)) *decay
else-- Overdamped
localc=sqrt(d*d-1)
localr1=-f* (d-c)
localr2=-f* (d+c)
localco2= (v0-offset*r1) / (2*f*c)
localco1=offset-co2
locale1=co1*exp(r1*dt)
locale2=co2*exp(r2*dt)
p1=e1+e2+g
v1=e1*r1+e2*r2
end
self.p=p1
self.v=v1
returnp1
end
returnSpring