/
Kinematics.lua
208 lines (179 loc) · 5.08 KB
/
Kinematics.lua
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
--[=[
Basic kinematics calculator that can be used like a spring. See [Spring] also.
@class Kinematics
]=]
local Kinematics = {}
Kinematics.ClassName = "Kinematics"
--[=[
Constructs a new kinematics class.
```lua
local kinematics = Kinematics.new(0)
kinematics.Acceleration = -32
kinematics.Velocity = 10
print(kinematics.Position) --> 0
task.wait(1)
print(kinematics.Position) --> -10
```
@param initial T -- The initial parameter is a number or Vector3 (anything with * number and addition/subtraction).
@param clock? () -> number -- The clock function is optional, and is used to update the kinematics class
@return Kinematics<T>
]=]
function Kinematics.new(initial, clock)
initial = initial or 0
local self = setmetatable({}, Kinematics)
rawset(self, "_clock", clock or os.clock)
rawset(self, "_position0", initial)
rawset(self, "_velocity0", 0*initial)
rawset(self, "_acceleration", 0*initial)
rawset(self, "_speed", 1)
rawset(self, "_time0", self._clock())
return self
end
--[=[
Impulses the current kinematics object, applying velocity to it.
@param velocity T
]=]
function Kinematics:Impulse(velocity)
self.Velocity = self.Velocity + velocity
end
--[=[
Skips forward in the set amount of time dictated by `delta`
@param delta number
]=]
function Kinematics:TimeSkip(delta)
assert(type(delta) == "number", "Bad delta")
local now = self._clock()
local position, velocity = self:_positionVelocity(now+delta)
rawset(self, "_position0", position)
rawset(self, "_velocity0", velocity)
rawset(self, "_time0", now)
end
--[=[
Sets data from some external source all at once.
This is useful for synchronizing the network.
@param startTime number
@param position0 T
@param velocity0 T
@param acceleration T
]=]
function Kinematics:SetData(startTime, position0, velocity0, acceleration)
rawset(self, "_time0", startTime)
rawset(self, "_position0", position0)
rawset(self, "_velocity0", velocity0)
rawset(self, "_acceleration", acceleration)
end
--[=[
Gets and sets the current position of the Kinematics
@prop Position T
@within Kinematics
]=]
--[=[
Gets and sets the current velocity of the Kinematics
@prop Velocity T
@within Kinematics
]=]
--[=[
Gets and sets the acceleration.
@prop Acceleration T
@within Kinematics
]=]
--[=[
Gets and sets the start time.
@prop StartTime T
@within Kinematics
]=]
--[=[
Gets and set the start position
@prop StartPosition T
@within Kinematics
]=]
--[=[
Sets the start velocity
@prop StartVelocity T
@within Kinematics
]=]
--[=[
Sets the playback speed
@prop Speed number
@within Kinematics
]=]
--[=[
Returns how old the kinematics is
@prop Age number
@readonly
@within Kinematics
]=]
--[=[
The current clock object to syncronize the kienmatics against.
@prop Clock () -> number
@within Kinematics
]=]
function Kinematics:__index(index)
local now = self._clock()
if Kinematics[index] then
return Kinematics[index]
elseif index == "Position" then
local position, _ = self:_positionVelocity(now)
return position
elseif index == "Velocity" then
local _, velocity = self:_positionVelocity(now)
return velocity
elseif index == "Acceleration" then
return rawget(self, "_acceleration")
elseif index == "StartTime" then
return rawget(self, "_time0")
elseif index == "StartPosition" then
return rawget(self, "_position0")
elseif index == "StartVelocity" then
return rawget(self, "_velocity0")
elseif index == "Speed" then
return rawget(self, "_speed")
elseif index == "Age" then
return self._clock() - rawget(self, "_time0")
elseif index == "Clock" then
return rawget(self, "_clock")
else
error(("%q is not a valid member of Kinematics"):format(tostring(index)), 2)
end
end
function Kinematics:__newindex(index, value)
local now = self._clock()
if index == "Position" then
local _, velocity = self:_positionVelocity(now)
rawset(self, "_position0", value)
rawset(self, "_velocity0", velocity)
elseif index == "Velocity" then
local position, _ = self:_positionVelocity(now)
rawset(self, "_position0", position)
rawset(self, "_velocity0", value)
elseif index == "Acceleration" then
local position, velocity = self:_positionVelocity(now)
rawset(self, "_position0", position)
rawset(self, "_velocity0", velocity)
rawset(self, "_acceleration", value)
elseif index == "Speed" then
local position, velocity = self:_positionVelocity(now)
rawset(self, "_position0", position)
rawset(self, "_velocity0", velocity)
rawset(self, "_speed", value)
elseif index == "Clock" then
local position, velocity = self:_positionVelocity(now)
rawset(self, "_position0", position)
rawset(self, "_velocity0", velocity)
rawset(self, "_clock", value)
rawset(self, "_time0", value())
else
error(("%q is not a valid member of Kinematics"):format(tostring(index)), 2)
end
rawset(self, "_time0", now)
end
function Kinematics:_positionVelocity(now)
local s = rawget(self, "_speed")
local dt = s*(now - rawget(self, "_time0"))
local a0 = rawget(self, "_acceleration")
local v0 = rawget(self, "_velocity0")
local p0 = rawget(self, "_position0")
return p0 + v0*dt + 0.5*dt*dt*a0,
v0 + a0*dt
end
return Kinematics