Scoop API

scoop:scoop.surface:0.1.0

src/shared/animation/spring.scoop

Selected production source · src/shared/animation/spring.scoop

1package scoop.surface
3import scoop.math.*
5private enum SpringKind {
6 Under, Critical, Over
7}
9private class SpringPlan(
10 private val target: AnimationVector,
11 private val a: AnimationVector,
12 private val b: AnimationVector,
13 private val kind: SpringKind,
14 private val rate: Double,
15 private val frequency: Double,
16 public override val duration: Double
17): MotionPlan {
18 public override fun sample(seconds: Double): MotionSample {
19 if (seconds >= duration)
20 return MotionSample(target, AnimationVector())
21 val time = max(0.0, seconds)
22 return when (kind) {
23 case Under -> {
24 val sine = sin(frequency * time)
25 val cosine = cos(frequency * time)
26 val decay = exp(-rate * time)
27 val offset = a.scale(cosine).add(b.scale(sine))
28 val velocity =
29 b.scale(frequency).subtract(a.scale(rate)).scale(cosine)
30 .subtract(b.scale(rate).add(a.scale(frequency)).scale(sine))
31 MotionSample(target.add(offset.scale(decay)), velocity.scale(decay))
32 }
33 case Critical -> {
34 val decay = exp(-rate * time)
35 val offset = a.add(b.scale(time))
36 MotionSample(
37 target.add(offset.scale(decay)),
38 b.subtract(offset.scale(rate)).scale(decay)
39 )
40 }
41 case Over -> {
42 val first = a.scale(exp(-rate * time))
43 val second = b.scale(exp(-frequency * time))
44 MotionSample(
45 target.add(first).add(second),
46 first.scale(-rate).add(second.scale(-frequency))
47 )
48 }
49 }
50 }
53internal fun springPlan(
54 from: AnimationVector,
55 to: AnimationVector,
56 velocity: AnimationVector,
57 stiffness: Double,
58 damping: Double,
59 displacement: Double,
60 speed: Double
61): MotionPlan {
62 requireAnimation(
63 stiffness.isFinite() &&
64 stiffness >
65 0.0 &&
66 damping.isFinite() &&
67 damping >
68 0.0 &&
69 displacement.isFinite() &&
70 displacement >
71 0.0 &&
72 speed.isFinite() &&
73 speed >
74 0.0,
75 "invalid spring parameters"
76 )
77 val omega = sqrt(stiffness)
78 val initial = from.subtract(to)
79 if (damping < 1.0) {
80 val rate = omega * damping
81 val frequency = omega * sqrt((1.0 - damping) * (1.0 + damping))
82 val b = velocity.add(initial.scale(rate)).scale(1.0 / frequency)
83 val positionBound = initial.maximum() + b.maximum()
84 val speedBound =
85 b.scale(frequency).subtract(initial.scale(rate)).maximum() +
86 b.scale(rate).add(initial.scale(frequency)).maximum()
87 return springResult(
88 to,
89 initial,
90 b,
91 SpringKind.Under,
92 rate,
93 frequency,
94 settling(positionBound, speedBound, rate, displacement, speed)
95 )
96 }
97 if (damping == 1.0) {
98 val b = velocity.add(initial.scale(omega))
99 val factor = 2.0 / exp(1.0)
100 val positionBound = initial.maximum() + b.maximum() * factor / omega
101 val speedBound = b.subtract(initial.scale(omega)).maximum() + b.maximum() * factor
102 return springResult(
103 to,
104 initial,
105 b,
106 SpringKind.Critical,
107 omega,
108 omega,
109 settling(positionBound, speedBound, omega / 2.0, displacement, speed)
110 )
111 }
112 val sum = damping + sqrt(damping - 1.0) * sqrt(damping + 1.0)
113 val slow = omega / sum
114 val fast = omega * sum
115 val a = velocity.add(initial.scale(fast)).scale(1.0 / (fast - slow))
116 val b = initial.subtract(a)
117 return springResult(
118 to,
119 a,
120 b,
121 SpringKind.Over,
122 slow,
123 fast,
124 settling(
125 a.maximum() + b.maximum(),
126 a.scale(slow).maximum() + b.scale(fast).maximum(),
127 slow,
128 displacement,
129 speed
130 )
131 )
134private fun settling(
135 positionBound: Double,
136 speedBound: Double,
137 decay: Double,
138 displacement: Double,
139 speed: Double
140): Double {
141 requireAnimation(
142 positionBound.isFinite() && speedBound.isFinite() && decay.isFinite() && decay > 0.0,
143 "spring coefficient overflow"
144 )
145 val positionTime =
146 if (positionBound <= displacement)
147 0.0
148 else
149 (log(positionBound) - log(displacement)) / decay
150 val speedTime =
151 if (speedBound <= speed)
152 0.0
153 else
154 (log(speedBound) - log(speed)) / decay
155 val result = max(positionTime, speedTime)
156 requireAnimation(result.isFinite(), "spring duration overflow")
157 return result
160private fun springResult(
161 target: AnimationVector,
162 a: AnimationVector,
163 b: AnimationVector,
164 kind: SpringKind,
165 rate: Double,
166 frequency: Double,
167 duration: Double
168): MotionPlan {
169 a.check();
170 b.check()
171 requireAnimation(
172 rate.isFinite() && frequency.isFinite() && rate > 0.0 && frequency > 0.0,
173 "spring frequency overflow"
174 )
175 return SpringPlan(target, a, b, kind, rate, frequency, duration)