-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpring.cs
More file actions
382 lines (336 loc) · 10.5 KB
/
Spring.cs
File metadata and controls
382 lines (336 loc) · 10.5 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using UnityEngine;
namespace Malware.Springs
{
public class FloatSpring
{
public FloatSpring (float initial, float s, float d)
{
this.time = Time.time;
this.position = initial;
this.velocity = 0f * initial;
this.target = initial;
this.Speed = s;
this.Damper = d;
}
public FloatSpring (float initial)
{
this.time = Time.time;
this.position = initial;
this.velocity = 0f * initial;
this.target = initial;
this.Speed = 1f;
this.Damper = 1f;
}
public FloatSpring ()
{
this.time = Time.time;
this.position = 1f;
this.velocity = 0f * 1f;
this.target = 1f;
this.Speed = 1f;
this.Damper = 1f;
}
float time;
float position;
float velocity;
float target;
float damper = 1;
float speed = 1;
/// <summary>
/// The value of the spring, at the time of the request.
/// </summary>
public float Value
{
get
{
PositionAndVelocity pv = PositionVelocity (Time.time);
return pv.Position;
}
set
{
PositionAndVelocity pv = PositionVelocity (Time.time);
position = value;
velocity = pv.Velocity;
time = Time.time;
}
}
/// <summary>
/// The velocity of the spring, at the time of the request.
/// </summary>
public float Velocity
{
get
{
PositionAndVelocity pv = PositionVelocity (Time.time);
return pv.Velocity;
}
set
{
PositionAndVelocity pv = PositionVelocity (Time.time);
position = pv.Position;
velocity = value;
time = Time.time;
}
}
/// <summary>
/// The acceleration of the spring, at the time of the request. Settings the acceleration will act as a push for the spring.
/// </summary>
public float Acceleration
{
get
{
float x = Time.time - time;
float c0 = position = target;
if (speed == 0)
return 0f;
else if (damper < 1)
{
float c = Mathf.Sqrt (1 - Mathf.Pow (Damper, 2f));
float c1 = (velocity / speed + damper * c0) / c;
return Mathf.Pow (speed, 2f) * (c0 - 2 * c1 + c1 * speed * x) / Mathf.Exp (speed * x);
}
else
{
float c1 = velocity / speed + c0;
return speed * speed * (c0 - 2 * c1 + c1 * speed * x) / Mathf.Pow (2.718281828459045f, speed * x);
}
}
set
{
PositionAndVelocity pv = PositionVelocity (Time.time);
position = pv.Position;
velocity = pv.Velocity + value;
time = Time.time;
}
}
/// <summary>
/// The target value of the spring.
/// </summary>
public float Target
{
get
{
return target;
}
set
{
PositionAndVelocity pv = PositionVelocity (Time.time);
position = pv.Position;
target = value;
time = Time.time;
}
}
/// <summary>
/// The damper of the spring, a range from 0 to 1, with 0 having no damping (spring never stops overshooting), to 1 having max damping (no overshooting).
/// </summary>
public float Damper
{
get
{
return damper;
}
set
{
PositionAndVelocity pv = PositionVelocity (Time.time);
damper = Mathf.Clamp01 (value);
time = Time.time;
position = pv.Position;
velocity = pv.Velocity;
}
}
/// <summary>
/// The speed of the spring.
/// </summary>
public float Speed
{
get
{
return speed;
}
set
{
PositionAndVelocity pv = PositionVelocity (Time.time);
speed = Mathf.Max (value, 0);
time = Time.time;
position = pv.Position;
velocity = pv.Velocity;
}
}
PositionAndVelocity PositionVelocity (float tick)
{
PositionAndVelocity returned = new PositionAndVelocity ();
float x = tick - time;
float c0 = position - target;
if (speed == 0)
{
returned.Position = position;
returned.Velocity = 0f;
return returned;
}
else if (damper < 1)
{
float c = Mathf.Sqrt (1 - Mathf.Pow (damper, 2f));
float c1 = (velocity / speed + damper * c0) / c;
float co = Mathf.Cos (c * speed * x);
float si = Mathf.Sin (c * speed * x);
float e = Mathf.Exp (damper * speed * x);
returned.Position = target + (c0 * co + c1 * si) / e;
returned.Velocity = speed * ((c * c1 - damper * c0) * co - (c * c0 + damper * c1) * si) / e;
return returned;
}
else
{
float c1 = velocity / speed + c0;
float e = Mathf.Exp (speed * x);
returned.Position = target + (c0 + c1 * speed * x) / e;
returned.Velocity = speed * (c1 - c0 - c1 * speed * x) / e;
return returned;
}
}
/// <summary>
/// Pushes the spring with the input acceleration.
/// </summary>
public void Accelerate (float acceleration)
{
float newTick = Time.time;
PositionAndVelocity pv = PositionVelocity (newTick);
position = pv.Position;
velocity = pv.Velocity + acceleration;
time = newTick;
}
private class PositionAndVelocity
{
public float Position;
public float Velocity;
}
}
public class Vector3Spring
{
public Vector3Spring (Vector3 initial, float springSpeed, float springDampening)
{
xSpring = new FloatSpring (initial.x, springSpeed, springDampening);
ySpring = new FloatSpring (initial.y, springSpeed, springDampening);
zSpring = new FloatSpring (initial.z, springSpeed, springDampening);
}
public Vector3Spring (Vector3 initial)
{
xSpring = new FloatSpring (initial.x);
ySpring = new FloatSpring (initial.y);
zSpring = new FloatSpring (initial.z);
}
public Vector3Spring ()
{
xSpring = new FloatSpring ();
ySpring = new FloatSpring ();
zSpring = new FloatSpring ();
}
private FloatSpring xSpring;
private FloatSpring ySpring;
private FloatSpring zSpring;
/// <summary>
/// The position of the spring, at the time of the request.
/// </summary>
public Vector3 Position
{
get
{
return new Vector3 (xSpring.Value, ySpring.Value, zSpring.Value);
}
set
{
xSpring.Value = value.x;
ySpring.Value = value.y;
zSpring.Value = value.z;
}
}
/// <summary>
/// The velocity of the spring, at the time of the request.
/// </summary>
public Vector3 Velocity
{
get
{
return new Vector3 (xSpring.Velocity, ySpring.Velocity, zSpring.Velocity);
}
set
{
xSpring.Velocity = value.x;
ySpring.Velocity = value.y;
zSpring.Velocity = value.z;
}
}
/// <summary>
/// The acceleration of the spring, at the time of the request. Settings the acceleration will act as a push for the spring.
/// </summary>
public Vector3 Acceleration
{
get
{
return new Vector3 (xSpring.Acceleration, ySpring.Acceleration, zSpring.Acceleration);
}
set
{
xSpring.Acceleration = value.x;
ySpring.Acceleration = value.y;
zSpring.Acceleration = value.z;
}
}
/// <summary>
/// The target of the spring.
/// </summary>
public Vector3 Target
{
get
{
return new Vector3 (xSpring.Target, ySpring.Target, zSpring.Target);
}
set
{
xSpring.Target = value.x;
ySpring.Target = value.y;
zSpring.Target = value.z;
}
}
/// <summary>
/// The damper of the spring, a range from 0 to 1, with 0 having no damping (spring never stops overshooting), to 1 having max damping (no overshooting).
/// </summary>
public float Damper
{
get
{
return xSpring.Damper;
}
set
{
xSpring.Damper = value;
ySpring.Damper = value;
zSpring.Damper = value;
}
}
/// <summary>
/// The speed of the spring.
/// </summary>
public float Speed
{
get
{
return xSpring.Speed;
}
set
{
xSpring.Speed = value;
ySpring.Speed = value;
zSpring.Speed = value;
}
}
/// <summary>
/// Pushes the spring with the input acceleration.
/// </summary>
public void Accelerate (Vector3 acceleration)
{
xSpring.Accelerate (acceleration.x);
ySpring.Accelerate (acceleration.y);
zSpring.Accelerate (acceleration.z);
}
}
}