From 69b1d05efc0a43b6f92adbd9deb4108aab1aebe7 Mon Sep 17 00:00:00 2001 From: liabru Date: Wed, 9 Jul 2014 17:46:44 +0100 Subject: [PATCH] moved collision events into Engine.update, changed collisionStart to trigger before collisions resolved --- src/core/Engine.js | 46 ++++++++++++++-------------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/src/core/Engine.js b/src/core/Engine.js index 09b4159..1fe4671 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -154,9 +154,6 @@ var Engine = {}; // update Engine.update(engine, delta, correction); - // trigger events that may have occured during the step - _triggerCollisionEvents(engine); - // render Engine.render(engine); @@ -165,7 +162,9 @@ var Engine = {}; }; /** - * Moves the simulation forward in time by `delta` ms. Triggers `beforeUpdate` and `afterUpdate` events. + * Moves the simulation forward in time by `delta` ms. + * Triggers `beforeUpdate` and `afterUpdate` events. + * Triggers `collisionStart`, `collisionActive` and `collisionEnd` events. * @method update * @param {engine} engine * @param {number} delta @@ -243,6 +242,10 @@ var Engine = {}; if (engine.enableSleeping) Sleeping.afterCollisions(pairs.list, timing.timeScale); + // trigger collision events + if (pairs.collisionStart.length > 0) + Events.trigger(engine, 'collisionStart', { pairs: pairs.collisionStart }); + // iteratively resolve velocity between collisions Resolver.preSolveVelocity(pairs.list); for (i = 0; i < engine.velocityIterations; i++) { @@ -255,6 +258,13 @@ var Engine = {}; } Resolver.postSolvePosition(allBodies); + // trigger collision events + if (pairs.collisionActive.length > 0) + Events.trigger(engine, 'collisionActive', { pairs: pairs.collisionActive }); + + if (pairs.collisionEnd.length > 0) + Events.trigger(engine, 'collisionEnd', { pairs: pairs.collisionEnd }); + // update metrics log Metrics.update(engine.metrics, engine); @@ -329,34 +339,6 @@ var Engine = {}; } }; - /** - * Triggers collision events - * @method _triggerMouseEvents - * @private - * @param {engine} engine - */ - var _triggerCollisionEvents = function(engine) { - var pairs = engine.pairs; - - if (pairs.collisionStart.length > 0) { - Events.trigger(engine, 'collisionStart', { - pairs: pairs.collisionStart - }); - } - - if (pairs.collisionActive.length > 0) { - Events.trigger(engine, 'collisionActive', { - pairs: pairs.collisionActive - }); - } - - if (pairs.collisionEnd.length > 0) { - Events.trigger(engine, 'collisionEnd', { - pairs: pairs.collisionEnd - }); - } - }; - /* * * Events Documentation