Skip to content

Commit

Permalink
moved collision events into Engine.update, changed collisionStart to …
Browse files Browse the repository at this point in the history
…trigger before collisions resolved
  • Loading branch information
liabru committed Jul 9, 2014
1 parent c5212e1 commit 69b1d05
Showing 1 changed file with 14 additions and 32 deletions.
46 changes: 14 additions & 32 deletions src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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++) {
Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 69b1d05

Please sign in to comment.