-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.js
163 lines (155 loc) · 4.87 KB
/
model.js
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
"use strict";
var Model = {};
var eps = 0.0001;
Model.helpers = {
/*
* Used internally for animating
*/
verletIntegration: function(obj, newA, dt) {
var x = obj.value;
var v = obj.velocity;
var a = obj.acceleration;
var halfV = v + 0.5*a*dt;
obj.value = x + halfV*dt;
obj.velocity = halfV + 0.5*newA*dt;
obj.acceleration = newA;
if (obj.endCondition(obj)) {
obj.finished = true;
}
return obj;
},
constrain: function(simulation, constraints) {
return function(obj, dt, t) {
// Integrate first, then check if the constraint is
// fulfilled, if not, run the constraint to fix the problem
var o = simulation(obj, dt, t);
for (var i = 0, l = constraints.length; i < l; i++) {
constraints[i](o, dt);
}
return o;
};
},
stopControlled: function(o) {
var cond = Math.abs(o.endValue-o.value) < eps && Math.abs(o.velocity) < eps;
if (cond) {
// Make sure the end value is exactly endValue
o.value = o.endValue;
}
return cond;
},
stopUncontrolled: function(o) {
return Math.abs(o.velocity) < eps && Math.abs(o.acceleration) < eps;
},
dontStop: function(_) {
return false;
}
};
Model.constraints = {
// TODO: still buggy!
// I think I'm doing this wrong, especially the check to see if velocity is big enough
// seems wrong. However, it seems to work ok...
elasticBoundaries: function(lower, upper, bounceConstant) {
var l = typeof lower !== 'undefined' ? lower : -Infinity;
var u = typeof upper !== 'undefined' ? upper : Infinity;
var e = typeof bounceConstant !== 'undefined' ? bounceConstant : 0.8;
return function(o, dt) {
var b = (o.value < l) ? l : ((o.value > u) ? u : undefined);
if (typeof b !== "undefined") {
o.value = b;
// I have no idea why, but this seems to be helping to stop the jittering
if (Math.abs(o.velocity) < 1) {
o.acceleration = o.acceleration*o.acceleration * o.velocity;
if (Math.abs(o.velocity) < 0.05) {
o.acceleration = 0;
o.velocity = 0;
}
}
o.velocity = -e*o.velocity;
}
};
}
};
Model.forces = {
gravity: function(g) {
return function(o) {
return -g;
};
},
/* Drag equation with a high Reynolds number */
fluidDrag: function(dragConstant) {
return function(o) {
var v = o.velocity;
if (v > 0) {
return -dragConstant*v*v;
} else {
return dragConstant*v*v;
}
};
},
/* Drag equation with a low Reynolds number */
airDrag: function(dragConstant) {
return function(o) { return -dragConstant*o.velocity; };
},
spring: function(springConstant) {
return function(o) {
return (o.endValue - o.value) * springConstant;
};
},
damper: function(damperConstant) {
return function(o) { return -damperConstant*o.velocity; };
}
};
// ### Controlled ###
Model.controlled = {};
Model.controlled.make = {
massSpringDamper: function(mass, springConstant, damperConstant) {
var fd = Model.forces.damper(damperConstant);
var fs = Model.forces.spring(springConstant);
return function(obj, dt, t) {
return Model.helpers.verletIntegration(obj, (fs(obj)+fd(obj))/mass, dt);
};
}
};
Model.controlled.make.dampedHarmonicOscillator = function(frequency, damping) {
return Model.controlled.make.massSpringDamper(1, frequency*frequency, 2*frequency*damping);
};
Model.controlled.make.criticallyDamped = function(frequency) {
return Model.controlled.make.dampedHarmonicOscillator(1, frequency);
};
Model.controlled.make.damper = function(mass, damping) {
return Model.controlled.make.massSpringDamper(mass, 0, damping);
};
Model.controlled.underDamped = Model.controlled.make.dampedHarmonicOscillator(10, 0.5);
Model.controlled.overDamped = Model.controlled.make.dampedHarmonicOscillator(10, 1.3);
Model.controlled.criticallyDamped = Model.controlled.make.criticallyDamped(10);
// ### Uncontrolled ###
Model.uncontrolled = {};
Model.uncontrolled.make = {
gravity: function(g) {
var fg = Model.forces.gravity(g);
return function(obj, dt, t) {
return Model.helpers.verletIntegration(obj, fg(obj), dt);
};
},
airDrag: function(constant) {
var f = Model.forces.airDrag(constant);
return function(obj, dt, t) {
return Model.helpers.verletIntegration(obj, f(obj), dt);
};
},
fluidDrag: function(constant) {
var f = Model.forces.fluidDrag(constant);
return function(obj, dt, t) {
return Model.helpers.verletIntegration(obj, f(obj), dt);
};
}
};
Model.uncontrolled.gravity = Model.uncontrolled.make.gravity(10);
Model.uncontrolled.gravityUpsideDown = Model.uncontrolled.make.gravity(-10);
Model.uncontrolled.airDrag = Model.uncontrolled.make.airDrag(0.5);
Model.uncontrolled.damper = Model.uncontrolled.make.airDrag(5);
Model.uncontrolled.fluidDrag = Model.uncontrolled.make.fluidDrag(5);
Model.uncontrolled.nothing = function(obj, dt, t) {
Model.helpers.verletIntegration(obj, 0, dt);
};
module.exports = Model;