-
Notifications
You must be signed in to change notification settings - Fork 22
/
Physics.js
executable file
·50 lines (44 loc) · 1.29 KB
/
Physics.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
Engine.traits.Physics = function()
{
Engine.Trait.call(this);
this.enabled = true;
this.mass = 0;
this.inertia = new THREE.Vector2();
this.momentum = new THREE.Vector2();
}
Engine.Util.extend(Engine.traits.Physics, Engine.Trait);
Engine.traits.Physics.prototype.__timeshift = function physicsTimeshift(dt)
{
if (this.enabled && dt) {
/* It is very important that gravity is applied before the
velocity is summed. Otherwise objects will not be pulled into the
ground between ticks and collision detection in resting
state will only happen every other tick. */
this._applyGravity(dt);
this._host.velocity.set(0, 0);
this._host.velocity.add(this.inertia);
this._host.velocity.add(this.momentum);
}
}
Engine.traits.Physics.prototype._applyGravity = function(dt)
{
if (this.mass === 0) {
return false;
}
var g = this._host.world.gravityForce;
if (g && (g.x || g.y)) {
this.inertia.x += g.x * dt;
this.inertia.y -= g.y * dt;
}
return true;
}
Engine.traits.Physics.prototype.bump = function(x, y)
{
this.inertia.x += x;
this.inertia.y += y;
}
Engine.traits.Physics.prototype.zero = function()
{
this.momentum.multiplyScalar(0);
this.inertia.multiplyScalar(0);
}