-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWallTransition.js
299 lines (257 loc) · 7.98 KB
/
WallTransition.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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: david@famo.us
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
var PE = require('famous/physics/PhysicsEngine');
var Particle = require('famous/physics/bodies/Particle');
var Spring = require('famous/physics/forces/Spring');
var Wall = require('famous/physics/constraints/Wall');
var Vector = require('famous/math/Vector');
/**
* WallTransition is a method of transitioning between two values (numbers,
* or arrays of numbers) with a bounce. Unlike a SpringTransition
* The transition will not overshoot the target, but bounce back against it.
* The behavior of the bounce is specified by the transition options.
*
* @class WallTransition
* @constructor
*
* @param {Number|Array} [state=0] Initial state
*/
function WallTransition(state) {
state = state || 0;
this.endState = new Vector(state);
this.initState = new Vector();
this.spring = new Spring({anchor : this.endState});
this.wall = new Wall();
this._restTolerance = 1e-10;
this._dimensions = 1;
this._absRestTolerance = this._restTolerance;
this._callback = undefined;
this.PE = new PE();
this.particle = new Particle();
this.PE.addBody(this.particle);
this.PE.attach([this.wall, this.spring], this.particle);
}
WallTransition.SUPPORTS_MULTIPLE = 3;
/**
* @property WallTransition.DEFAULT_OPTIONS
* @type Object
* @protected
* @static
*/
WallTransition.DEFAULT_OPTIONS = {
/**
* The amount of time in milliseconds taken for one complete oscillation
* when there is no damping
* Range : [0, Infinity]
*
* @attribute period
* @type Number
* @default 300
*/
period : 300,
/**
* The damping of the snap.
* Range : [0, 1]
* 0 = no damping, and the spring will oscillate forever
* 1 = critically damped (the spring will never oscillate)
*
* @attribute dampingRatio
* @type Number
* @default 0.5
*/
dampingRatio : 0.5,
/**
* The initial velocity of the transition.
*
* @attribute velocity
* @type Number|Array
* @default 0
*/
velocity : 0,
/**
* The percentage of momentum transferred to the wall
*
* @attribute restitution
* @type Number
* @default 0.5
*/
resitution : 0.5
};
function _getEnergy() {
return this.particle.getEnergy() + this.spring.getEnergy(this.particle);
}
function _setAbsoluteRestTolerance() {
var distance = this.endState.sub(this.initState).normSquared();
this._absRestTolerance = (distance === 0)
? this._restTolerance
: this._restTolerance * distance;
}
function _wake() {
this.PE.wake();
}
function _sleep() {
this.PE.sleep();
}
function _setTarget(target) {
this.endState.set(target);
var dist = this.endState.sub(this.initState).norm();
this.wall.setOptions({
distance : this.endState.norm(),
normal : (dist === 0)
? this.particle.velocity.normalize(-1)
: this.endState.sub(this.initState).normalize(-1)
});
_setAbsoluteRestTolerance.call(this);
}
function _setParticlePosition(p) {
this.particle.position.set(p);
}
function _setParticleVelocity(v) {
this.particle.velocity.set(v);
}
function _getParticlePosition() {
return (this._dimensions === 0)
? this.particle.getPosition1D()
: this.particle.getPosition();
}
function _getParticleVelocity() {
return (this._dimensions === 0)
? this.particle.getVelocity1D()
: this.particle.getVelocity();
}
function _setCallback(callback) {
this._callback = callback;
}
function _update() {
if (this.PE.isSleeping()) {
if (this._callback) {
var cb = this._callback;
this._callback = undefined;
cb();
}
return;
}
var energy = _getEnergy.call(this);
if (energy < this._absRestTolerance) {
_sleep.call(this);
_setParticlePosition.call(this, this.endState);
_setParticleVelocity.call(this, [0,0,0]);
}
}
function _setupDefinition(def) {
var defaults = WallTransition.DEFAULT_OPTIONS;
if (def.period === undefined) def.period = defaults.period;
if (def.dampingRatio === undefined) def.dampingRatio = defaults.dampingRatio;
if (def.velocity === undefined) def.velocity = defaults.velocity;
if (def.restitution === undefined) def.restitution = defaults.restitution;
//setup spring
this.spring.setOptions({
period : def.period,
dampingRatio : def.dampingRatio
});
//setup wall
this.wall.setOptions({
restitution : def.restitution
});
//setup particle
_setParticleVelocity.call(this, def.velocity);
}
/**
* Resets the state and velocity
*
* @method reset
*
* @param {Number|Array} state State
* @param {Number|Array} [velocity] Velocity
*/
WallTransition.prototype.reset = function reset(state, velocity) {
this._dimensions = (state instanceof Array)
? state.length
: 0;
this.initState.set(state);
_setParticlePosition.call(this, state);
if (velocity) _setParticleVelocity.call(this, velocity);
_setTarget.call(this, state);
_setCallback.call(this, undefined);
};
/**
* Getter for velocity
*
* @method getVelocity
*
* @return velocity {Number|Array}
*/
WallTransition.prototype.getVelocity = function getVelocity() {
return _getParticleVelocity.call(this);
};
/**
* Setter for velocity
*
* @method setVelocity
*
* @return velocity {Number|Array}
*/
WallTransition.prototype.setVelocity = function setVelocity(velocity) {
this.call(this, _setParticleVelocity(velocity));
};
/**
* Detects whether a transition is in progress
*
* @method isActive
*
* @return {Boolean}
*/
WallTransition.prototype.isActive = function isActive() {
return !this.PE.isSleeping();
};
/**
* Halt the transition
*
* @method halt
*/
WallTransition.prototype.halt = function halt() {
this.set(this.get());
};
/**
* Getter
*
* @method get
*
* @return state {Number|Array}
*/
WallTransition.prototype.get = function get() {
_update.call(this);
return _getParticlePosition.call(this);
};
/**
* Set the end position and transition, with optional callback on completion.
*
* @method set
*
* @param state {Number|Array} Final state
* @param [definition] {Object} Transition definition
* @param [callback] {Function} Callback
*/
WallTransition.prototype.set = function set(state, definition, callback) {
if (!definition) {
this.reset(state);
if (callback) callback();
return;
}
this._dimensions = (state instanceof Array)
? state.length
: 0;
_wake.call(this);
_setupDefinition.call(this, definition);
_setTarget.call(this, state);
_setCallback.call(this, callback);
};
module.exports = WallTransition;
});