From 8a71a2178ced363724d8aa9aedb7131e540d3465 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 16 Aug 2017 16:30:28 +0100 Subject: [PATCH] Added Collide event into the world. --- v3/src/physics/impact/Solver.js | 5 +++ v3/src/physics/impact/World.js | 39 +++++++++++++++----- v3/src/physics/impact/events/CollideEvent.js | 29 +++++++++++++++ v3/src/physics/impact/events/index.js | 7 ++++ 4 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 v3/src/physics/impact/events/CollideEvent.js create mode 100644 v3/src/physics/impact/events/index.js diff --git a/v3/src/physics/impact/Solver.js b/v3/src/physics/impact/Solver.js index 9de375286..bf3636216 100644 --- a/v3/src/physics/impact/Solver.js +++ b/v3/src/physics/impact/Solver.js @@ -1,6 +1,7 @@ var SeperateX = require('./SeperateX'); var SeperateY = require('./SeperateY'); var COLLIDES = require('./COLLIDES'); +var Events = require('./events'); // Impact Physics Solver @@ -30,6 +31,8 @@ var Solver = function (world, bodyA, bodyB) bodyA.collideWith(bodyB, 'y'); bodyB.collideWith(bodyA, 'y'); + + world.events.dispatch(new Events.COLLIDE(bodyA, bodyB)); } else if (bodyA.last.y + bodyA.size.y > bodyB.last.y && bodyA.last.y < bodyB.last.y + bodyB.size.y) { @@ -44,6 +47,8 @@ var Solver = function (world, bodyA, bodyB) bodyA.collideWith(bodyB, 'x'); bodyB.collideWith(bodyA, 'x'); + + world.events.dispatch(new Events.COLLIDE(bodyA, bodyB)); } }; diff --git a/v3/src/physics/impact/World.js b/v3/src/physics/impact/World.js index fc6f7a2ad..bfb7fba25 100644 --- a/v3/src/physics/impact/World.js +++ b/v3/src/physics/impact/World.js @@ -19,6 +19,8 @@ var World = new Class({ this.scene = scene; + this.events = scene.sys.events; + this.bodies = new Set(); this.gravity = gravity; @@ -30,6 +32,13 @@ var World = new Class({ this.delta = 0; + this.timeScale = 1; + + // Impacts maximum time step is 20 fps. + this.maxStep = 0.05; + + this.enabled = true; + this._lastId = 0; }, @@ -47,19 +56,31 @@ var World = new Class({ return body; }, + pause: function () + { + this.enabled = false; + + return this; + }, + + resume: function () + { + this.enabled = true; + + return this; + }, + update: function (time, delta) { - if (this.bodies.size === 0) + if (!this.enabled || this.bodies.size === 0) { return; } - // Impact uses a divided delta value - delta /= 1000; + // Impact uses a divided delta value that is clamped to the maxStep (20fps) maximum + this.delta = Math.min(delta / 1000, this.maxStep) * this.timeScale; - this.delta = delta; - - // Update all bodies + // Update all active bodies var i; var body; @@ -68,19 +89,17 @@ var World = new Class({ var hash = {}; var size = this.cellSize; - // Update all active bodies - for (i = 0; i < len; i++) { body = bodies[i]; if (body.enabled) { - body.update(delta); + body.update(this.delta); } } - // Run collision against them all now they're in the new positions + // Run collision against them all now they're in the new positions from the udpate for (i = 0; i < len; i++) { diff --git a/v3/src/physics/impact/events/CollideEvent.js b/v3/src/physics/impact/events/CollideEvent.js new file mode 100644 index 000000000..f5ae968a9 --- /dev/null +++ b/v3/src/physics/impact/events/CollideEvent.js @@ -0,0 +1,29 @@ +var Class = require('../../../utils/Class'); +var Event = require('../../../events/Event'); + +var CollideEvent = new Class({ + + Extends: Event, + + initialize: + + function CollideEvent (bodyA, bodyB) + { + Event.call(this, 'COLLIDE_EVENT'); + + // The first body involved in the collision] + this.bodyA = bodyA; + + // The second body involved in the collision] + this.bodyB = bodyB; + + // The Game Object associated with bodyA (if any) + this.gameObjectA = bodyA.gameObject; + + // The Game Object associated with bodyB (if any) + this.gameObjectB = bodyB.gameObject; + } + +}); + +module.exports = CollideEvent; diff --git a/v3/src/physics/impact/events/index.js b/v3/src/physics/impact/events/index.js new file mode 100644 index 000000000..ba4dfaa84 --- /dev/null +++ b/v3/src/physics/impact/events/index.js @@ -0,0 +1,7 @@ +// Phaser.Physics.Impact.Events + +module.exports = { + + COLLIDE: require('./CollideEvent') + +};