forked from n5ro/aframe-physics-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.js
274 lines (232 loc) · 8.74 KB
/
system.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
/* global THREE */
var CANNON = require('cannon-es'),
CONSTANTS = require('./constants'),
C_GRAV = CONSTANTS.GRAVITY,
C_MAT = CONSTANTS.CONTACT_MATERIAL;
var LocalDriver = require('./drivers/local-driver'),
WorkerDriver = require('./drivers/worker-driver'),
NetworkDriver = require('./drivers/network-driver'),
AmmoDriver = require('./drivers/ammo-driver');
/**
* Physics system.
*/
module.exports = AFRAME.registerSystem('physics', {
schema: {
// CANNON.js driver type
driver: { default: 'local', oneOf: ['local', 'worker', 'network', 'ammo'] },
networkUrl: { default: '', if: {driver: 'network'} },
workerFps: { default: 60, if: {driver: 'worker'} },
workerInterpolate: { default: true, if: {driver: 'worker'} },
workerInterpBufferSize: { default: 2, if: {driver: 'worker'} },
workerEngine: { default: 'cannon', if: {driver: 'worker'}, oneOf: ['cannon'] },
workerDebug: { default: false, if: {driver: 'worker'} },
gravity: { default: C_GRAV },
iterations: { default: CONSTANTS.ITERATIONS },
friction: { default: C_MAT.friction },
restitution: { default: C_MAT.restitution },
contactEquationStiffness: { default: C_MAT.contactEquationStiffness },
contactEquationRelaxation: { default: C_MAT.contactEquationRelaxation },
frictionEquationStiffness: { default: C_MAT.frictionEquationStiffness },
frictionEquationRegularization: { default: C_MAT.frictionEquationRegularization },
// Never step more than four frames at once. Effectively pauses the scene
// when out of focus, and prevents weird "jumps" when focus returns.
maxInterval: { default: 4 / 60 },
// If true, show wireframes around physics bodies.
debug: { default: false },
// If using ammo, set the default rendering mode for debug
debugDrawMode: { default: THREE.AmmoDebugConstants.NoDebug },
// If using ammo, set the max number of steps per frame
maxSubSteps: { default: 4 },
// If using ammo, set the framerate of the simulation
fixedTimeStep: { default: 1 / 60 }
},
/**
* Initializes the physics system.
*/
async init() {
var data = this.data;
// If true, show wireframes around physics bodies.
this.debug = data.debug;
this.callbacks = {beforeStep: [], step: [], afterStep: []};
this.listeners = {};
this.driver = null;
switch (data.driver) {
case 'local':
this.driver = new LocalDriver();
break;
case 'ammo':
this.driver = new AmmoDriver();
break;
case 'network':
this.driver = new NetworkDriver(data.networkUrl);
break;
case 'worker':
this.driver = new WorkerDriver({
fps: data.workerFps,
engine: data.workerEngine,
interpolate: data.workerInterpolate,
interpolationBufferSize: data.workerInterpBufferSize,
debug: data.workerDebug
});
break;
default:
throw new Error('[physics] Driver not recognized: "%s".', data.driver);
}
if (data.driver !== 'ammo') {
await this.driver.init({
quatNormalizeSkip: 0,
quatNormalizeFast: false,
solverIterations: data.iterations,
gravity: data.gravity,
});
this.driver.addMaterial({name: 'defaultMaterial'});
this.driver.addMaterial({name: 'staticMaterial'});
this.driver.addContactMaterial('defaultMaterial', 'defaultMaterial', {
friction: data.friction,
restitution: data.restitution,
contactEquationStiffness: data.contactEquationStiffness,
contactEquationRelaxation: data.contactEquationRelaxation,
frictionEquationStiffness: data.frictionEquationStiffness,
frictionEquationRegularization: data.frictionEquationRegularization
});
this.driver.addContactMaterial('staticMaterial', 'defaultMaterial', {
friction: 1.0,
restitution: 0.0,
contactEquationStiffness: data.contactEquationStiffness,
contactEquationRelaxation: data.contactEquationRelaxation,
frictionEquationStiffness: data.frictionEquationStiffness,
frictionEquationRegularization: data.frictionEquationRegularization
});
} else {
await this.driver.init({
gravity: data.gravity,
debugDrawMode: data.debugDrawMode,
solverIterations: data.iterations,
maxSubSteps: data.maxSubSteps,
fixedTimeStep: data.fixedTimeStep
});
}
this.initialized = true;
if (this.debug) {
this.setDebug(true);
}
},
/**
* Updates the physics world on each tick of the A-Frame scene. It would be
* entirely possible to separate the two – updating physics more or less
* frequently than the scene – if greater precision or performance were
* necessary.
* @param {number} t
* @param {number} dt
*/
tick: function (t, dt) {
if (!this.initialized || !dt) return;
var i;
var callbacks = this.callbacks;
for (i = 0; i < this.callbacks.beforeStep.length; i++) {
this.callbacks.beforeStep[i].beforeStep(t, dt);
}
this.driver.step(Math.min(dt / 1000, this.data.maxInterval));
for (i = 0; i < callbacks.step.length; i++) {
callbacks.step[i].step(t, dt);
}
for (i = 0; i < callbacks.afterStep.length; i++) {
callbacks.afterStep[i].afterStep(t, dt);
}
},
setDebug: function(debug) {
this.debug = debug;
if (this.data.driver === 'ammo' && this.initialized) {
if (debug && !this.debugDrawer) {
this.debugDrawer = this.driver.getDebugDrawer(this.el.object3D);
this.debugDrawer.enable();
} else if (this.debugDrawer) {
this.debugDrawer.disable();
this.debugDrawer = null;
}
}
},
/**
* Adds a body to the scene, and binds proxied methods to the driver.
* @param {CANNON.Body} body
*/
addBody: function (body, group, mask) {
var driver = this.driver;
if (this.data.driver === 'local') {
body.__applyImpulse = body.applyImpulse;
body.applyImpulse = function () {
driver.applyBodyMethod(body, 'applyImpulse', arguments);
};
body.__applyForce = body.applyForce;
body.applyForce = function () {
driver.applyBodyMethod(body, 'applyForce', arguments);
};
body.updateProperties = function () {
driver.updateBodyProperties(body);
};
this.listeners[body.id] = function (e) { body.el.emit('collide', e); };
body.addEventListener('collide', this.listeners[body.id]);
}
this.driver.addBody(body, group, mask);
},
/**
* Removes a body and its proxied methods.
* @param {CANNON.Body} body
*/
removeBody: function (body) {
this.driver.removeBody(body);
if (this.data.driver === 'local' || this.data.driver === 'worker') {
body.removeEventListener('collide', this.listeners[body.id]);
delete this.listeners[body.id];
body.applyImpulse = body.__applyImpulse;
delete body.__applyImpulse;
body.applyForce = body.__applyForce;
delete body.__applyForce;
delete body.updateProperties;
}
},
/** @param {CANNON.Constraint or Ammo.btTypedConstraint} constraint */
addConstraint: function (constraint) {
this.driver.addConstraint(constraint);
},
/** @param {CANNON.Constraint or Ammo.btTypedConstraint} constraint */
removeConstraint: function (constraint) {
this.driver.removeConstraint(constraint);
},
/**
* Adds a component instance to the system and schedules its update methods to be called
* the given phase.
* @param {Component} component
* @param {string} phase
*/
addComponent: function (component) {
var callbacks = this.callbacks;
if (component.beforeStep) callbacks.beforeStep.push(component);
if (component.step) callbacks.step.push(component);
if (component.afterStep) callbacks.afterStep.push(component);
},
/**
* Removes a component instance from the system.
* @param {Component} component
* @param {string} phase
*/
removeComponent: function (component) {
var callbacks = this.callbacks;
if (component.beforeStep) {
callbacks.beforeStep.splice(callbacks.beforeStep.indexOf(component), 1);
}
if (component.step) {
callbacks.step.splice(callbacks.step.indexOf(component), 1);
}
if (component.afterStep) {
callbacks.afterStep.splice(callbacks.afterStep.indexOf(component), 1);
}
},
/** @return {Array<object>} */
getContacts: function () {
return this.driver.getContacts();
},
getMaterial: function (name) {
return this.driver.getMaterial(name);
}
});