Skip to content

Commit 8a71a21

Browse files
committed
Added Collide event into the world.
1 parent 01a6592 commit 8a71a21

4 files changed

Lines changed: 70 additions & 10 deletions

File tree

v3/src/physics/impact/Solver.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var SeperateX = require('./SeperateX');
22
var SeperateY = require('./SeperateY');
33
var COLLIDES = require('./COLLIDES');
4+
var Events = require('./events');
45

56
// Impact Physics Solver
67

@@ -30,6 +31,8 @@ var Solver = function (world, bodyA, bodyB)
3031

3132
bodyA.collideWith(bodyB, 'y');
3233
bodyB.collideWith(bodyA, 'y');
34+
35+
world.events.dispatch(new Events.COLLIDE(bodyA, bodyB));
3336
}
3437
else if (bodyA.last.y + bodyA.size.y > bodyB.last.y && bodyA.last.y < bodyB.last.y + bodyB.size.y)
3538
{
@@ -44,6 +47,8 @@ var Solver = function (world, bodyA, bodyB)
4447

4548
bodyA.collideWith(bodyB, 'x');
4649
bodyB.collideWith(bodyA, 'x');
50+
51+
world.events.dispatch(new Events.COLLIDE(bodyA, bodyB));
4752
}
4853
};
4954

v3/src/physics/impact/World.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ var World = new Class({
1919

2020
this.scene = scene;
2121

22+
this.events = scene.sys.events;
23+
2224
this.bodies = new Set();
2325

2426
this.gravity = gravity;
@@ -30,6 +32,13 @@ var World = new Class({
3032

3133
this.delta = 0;
3234

35+
this.timeScale = 1;
36+
37+
// Impacts maximum time step is 20 fps.
38+
this.maxStep = 0.05;
39+
40+
this.enabled = true;
41+
3342
this._lastId = 0;
3443
},
3544

@@ -47,19 +56,31 @@ var World = new Class({
4756
return body;
4857
},
4958

59+
pause: function ()
60+
{
61+
this.enabled = false;
62+
63+
return this;
64+
},
65+
66+
resume: function ()
67+
{
68+
this.enabled = true;
69+
70+
return this;
71+
},
72+
5073
update: function (time, delta)
5174
{
52-
if (this.bodies.size === 0)
75+
if (!this.enabled || this.bodies.size === 0)
5376
{
5477
return;
5578
}
5679

57-
// Impact uses a divided delta value
58-
delta /= 1000;
80+
// Impact uses a divided delta value that is clamped to the maxStep (20fps) maximum
81+
this.delta = Math.min(delta / 1000, this.maxStep) * this.timeScale;
5982

60-
this.delta = delta;
61-
62-
// Update all bodies
83+
// Update all active bodies
6384

6485
var i;
6586
var body;
@@ -68,19 +89,17 @@ var World = new Class({
6889
var hash = {};
6990
var size = this.cellSize;
7091

71-
// Update all active bodies
72-
7392
for (i = 0; i < len; i++)
7493
{
7594
body = bodies[i];
7695

7796
if (body.enabled)
7897
{
79-
body.update(delta);
98+
body.update(this.delta);
8099
}
81100
}
82101

83-
// Run collision against them all now they're in the new positions
102+
// Run collision against them all now they're in the new positions from the udpate
84103

85104
for (i = 0; i < len; i++)
86105
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var Class = require('../../../utils/Class');
2+
var Event = require('../../../events/Event');
3+
4+
var CollideEvent = new Class({
5+
6+
Extends: Event,
7+
8+
initialize:
9+
10+
function CollideEvent (bodyA, bodyB)
11+
{
12+
Event.call(this, 'COLLIDE_EVENT');
13+
14+
// The first body involved in the collision]
15+
this.bodyA = bodyA;
16+
17+
// The second body involved in the collision]
18+
this.bodyB = bodyB;
19+
20+
// The Game Object associated with bodyA (if any)
21+
this.gameObjectA = bodyA.gameObject;
22+
23+
// The Game Object associated with bodyB (if any)
24+
this.gameObjectB = bodyB.gameObject;
25+
}
26+
27+
});
28+
29+
module.exports = CollideEvent;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Phaser.Physics.Impact.Events
2+
3+
module.exports = {
4+
5+
COLLIDE: require('./CollideEvent')
6+
7+
};

0 commit comments

Comments
 (0)