From ffeff9d2f741ad606547eace36b793a8eae63f07 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 24 Nov 2017 00:37:09 +0000 Subject: [PATCH] Added confirmedActive speed optimisation to matter.js and removed Common.indexOf --- v3/src/physics/matter-js/lib/body/Composite.js | 3 --- v3/src/physics/matter-js/lib/collision/Grid.js | 1 - v3/src/physics/matter-js/lib/collision/Pair.js | 1 + v3/src/physics/matter-js/lib/collision/Pairs.js | 10 ++++++---- v3/src/physics/matter-js/lib/core/Common.js | 4 ++-- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/v3/src/physics/matter-js/lib/body/Composite.js b/v3/src/physics/matter-js/lib/body/Composite.js index 40c6163df..b46d65e19 100644 --- a/v3/src/physics/matter-js/lib/body/Composite.js +++ b/v3/src/physics/matter-js/lib/body/Composite.js @@ -176,7 +176,6 @@ var Body = require('./Body'); * @return {composite} The original compositeA with the composite removed */ Composite.removeComposite = function(compositeA, compositeB, deep) { - // var position = Common.indexOf(compositeA.composites, compositeB); var position = compositeA.composites.indexOf(compositeB); if (position !== -1) { Composite.removeCompositeAt(compositeA, position); @@ -230,7 +229,6 @@ var Body = require('./Body'); * @return {composite} The original composite with the body removed */ Composite.removeBody = function(composite, body, deep) { - // var position = Common.indexOf(composite.bodies, body); var position = composite.bodies.indexOf(body); if (position !== -1) { Composite.removeBodyAt(composite, position); @@ -284,7 +282,6 @@ var Body = require('./Body'); * @return {composite} The original composite with the constraint removed */ Composite.removeConstraint = function(composite, constraint, deep) { - // var position = Common.indexOf(composite.constraints, constraint); var position = composite.constraints.indexOf(constraint); if (position !== -1) { Composite.removeConstraintAt(composite, position); diff --git a/v3/src/physics/matter-js/lib/collision/Grid.js b/v3/src/physics/matter-js/lib/collision/Grid.js index b9e9fb3f9..7e1066fd6 100644 --- a/v3/src/physics/matter-js/lib/collision/Grid.js +++ b/v3/src/physics/matter-js/lib/collision/Grid.js @@ -272,7 +272,6 @@ var Common = require('../core/Common'); */ var _bucketRemoveBody = function(grid, bucket, body) { // remove from bucket - // bucket.splice(Common.indexOf(bucket, body), 1); bucket.splice(bucket.indexOf(body), 1); // update pair counts diff --git a/v3/src/physics/matter-js/lib/collision/Pair.js b/v3/src/physics/matter-js/lib/collision/Pair.js index 7f654b59a..ccfd08843 100644 --- a/v3/src/physics/matter-js/lib/collision/Pair.js +++ b/v3/src/physics/matter-js/lib/collision/Pair.js @@ -33,6 +33,7 @@ var Contact = require('./Contact'); activeContacts: [], separation: 0, isActive: true, + confirmedActive: true, isSensor: bodyA.isSensor || bodyB.isSensor, timeCreated: timestamp, timeUpdated: timestamp, diff --git a/v3/src/physics/matter-js/lib/collision/Pairs.js b/v3/src/physics/matter-js/lib/collision/Pairs.js index f6c1c48ab..83a899137 100644 --- a/v3/src/physics/matter-js/lib/collision/Pairs.js +++ b/v3/src/physics/matter-js/lib/collision/Pairs.js @@ -44,7 +44,6 @@ var Common = require('../core/Common'); collisionStart = pairs.collisionStart, collisionEnd = pairs.collisionEnd, collisionActive = pairs.collisionActive, - activePairIds = [], collision, pairId, pair, @@ -55,12 +54,15 @@ var Common = require('../core/Common'); collisionEnd.length = 0; collisionActive.length = 0; + for (i = 0; i < pairsList.length; i++) { + pairsList[i].confirmedActive = false; + } + for (i = 0; i < collisions.length; i++) { collision = collisions[i]; if (collision.collided) { pairId = Pair.id(collision.bodyA, collision.bodyB); - activePairIds.push(pairId); pair = pairsTable[pairId]; @@ -76,6 +78,7 @@ var Common = require('../core/Common'); // update the pair Pair.update(pair, collision, timestamp); + pair.confirmedActive = true; } else { // pair did not exist, create a new pair pair = Pair.create(collision, timestamp); @@ -91,8 +94,7 @@ var Common = require('../core/Common'); // deactivate previously active pairs that are now inactive for (i = 0; i < pairsList.length; i++) { pair = pairsList[i]; - // if (pair.isActive && Common.indexOf(activePairIds, pair.id) === -1) { - if (pair.isActive && activePairIds.indexOf(pair.id) === -1) { + if (!pair.confirmedActive) { Pair.setActive(pair, false, timestamp); collisionEnd.push(pair); } diff --git a/v3/src/physics/matter-js/lib/core/Common.js b/v3/src/physics/matter-js/lib/core/Common.js index 350e7518f..1086e7065 100644 --- a/v3/src/physics/matter-js/lib/core/Common.js +++ b/v3/src/physics/matter-js/lib/core/Common.js @@ -217,7 +217,7 @@ module.exports = Common; * @return {boolean} True if the object is a string, otherwise false */ Common.isString = function(obj) { - return toString.call(obj) === '[object String]'; + return Object.prototype.toString.call(obj) === '[object String]'; }; /** @@ -369,7 +369,6 @@ module.exports = Common; * @param {array} haystack * @param {object} needle * @return {number} The position of needle in haystack, otherwise -1. - */ Common.indexOf = function(haystack, needle) { if (haystack.indexOf) return haystack.indexOf(needle); @@ -381,6 +380,7 @@ module.exports = Common; return -1; }; + */ /** * A cross browser compatible array map implementation.