UNPKG

1.9 kBJavaScriptView Raw
1function stiffnessFromOrigamiValue(oValue) {
2 return (oValue - 30) * 3.62 + 194;
3}
4
5function dampingFromOrigamiValue(oValue) {
6 return (oValue - 8) * 3 + 25;
7}
8
9function fromOrigamiTensionAndFriction(tension, friction) {
10 return {
11 stiffness: stiffnessFromOrigamiValue(tension),
12 damping: dampingFromOrigamiValue(friction),
13 };
14}
15
16function fromBouncinessAndSpeed(bounciness, speed) {
17 function normalize(value, startValue, endValue) {
18 return (value - startValue) / (endValue - startValue);
19 }
20
21 function projectNormal(n, start, end) {
22 return start + n * (end - start);
23 }
24
25 function linearInterpolation(t, start, end) {
26 return t * end + (1 - t) * start;
27 }
28
29 function quadraticOutInterpolation(t, start, end) {
30 return linearInterpolation(2 * t - t * t, start, end);
31 }
32
33 function b3Friction1(x) {
34 return 0.0007 * Math.pow(x, 3) - 0.031 * Math.pow(x, 2) + 0.64 * x + 1.28;
35 }
36
37 function b3Friction2(x) {
38 return 0.000044 * Math.pow(x, 3) - 0.006 * Math.pow(x, 2) + 0.36 * x + 2;
39 }
40
41 function b3Friction3(x) {
42 return (
43 0.00000045 * Math.pow(x, 3) -
44 0.000332 * Math.pow(x, 2) +
45 0.1078 * x +
46 5.84
47 );
48 }
49
50 function b3Nobounce(tension) {
51 if (tension <= 18) {
52 return b3Friction1(tension);
53 } else if (tension > 18 && tension <= 44) {
54 return b3Friction2(tension);
55 } else {
56 return b3Friction3(tension);
57 }
58 }
59
60 var b = normalize(bounciness / 1.7, 0, 20);
61 b = projectNormal(b, 0, 0.8);
62 var s = normalize(speed / 1.7, 0, 20);
63 var bouncyTension = projectNormal(s, 0.5, 200);
64 var bouncyFriction = quadraticOutInterpolation(
65 b,
66 b3Nobounce(bouncyTension),
67 0.01
68 );
69
70 return {
71 stiffness: stiffnessFromOrigamiValue(bouncyTension),
72 damping: dampingFromOrigamiValue(bouncyFriction),
73 };
74}
75
76module.exports = {
77 fromOrigamiTensionAndFriction,
78 fromBouncinessAndSpeed,
79};