Added confirmedActive speed optimisation to matter.js and removed Common.indexOf

This commit is contained in:
Richard Davey 2017-11-24 00:37:09 +00:00
parent f1ea070256
commit ffeff9d2f7
5 changed files with 9 additions and 10 deletions

View file

@ -176,7 +176,6 @@ var Body = require('./Body');
* @return {composite} The original compositeA with the composite removed * @return {composite} The original compositeA with the composite removed
*/ */
Composite.removeComposite = function(compositeA, compositeB, deep) { Composite.removeComposite = function(compositeA, compositeB, deep) {
// var position = Common.indexOf(compositeA.composites, compositeB);
var position = compositeA.composites.indexOf(compositeB); var position = compositeA.composites.indexOf(compositeB);
if (position !== -1) { if (position !== -1) {
Composite.removeCompositeAt(compositeA, position); Composite.removeCompositeAt(compositeA, position);
@ -230,7 +229,6 @@ var Body = require('./Body');
* @return {composite} The original composite with the body removed * @return {composite} The original composite with the body removed
*/ */
Composite.removeBody = function(composite, body, deep) { Composite.removeBody = function(composite, body, deep) {
// var position = Common.indexOf(composite.bodies, body);
var position = composite.bodies.indexOf(body); var position = composite.bodies.indexOf(body);
if (position !== -1) { if (position !== -1) {
Composite.removeBodyAt(composite, position); Composite.removeBodyAt(composite, position);
@ -284,7 +282,6 @@ var Body = require('./Body');
* @return {composite} The original composite with the constraint removed * @return {composite} The original composite with the constraint removed
*/ */
Composite.removeConstraint = function(composite, constraint, deep) { Composite.removeConstraint = function(composite, constraint, deep) {
// var position = Common.indexOf(composite.constraints, constraint);
var position = composite.constraints.indexOf(constraint); var position = composite.constraints.indexOf(constraint);
if (position !== -1) { if (position !== -1) {
Composite.removeConstraintAt(composite, position); Composite.removeConstraintAt(composite, position);

View file

@ -272,7 +272,6 @@ var Common = require('../core/Common');
*/ */
var _bucketRemoveBody = function(grid, bucket, body) { var _bucketRemoveBody = function(grid, bucket, body) {
// remove from bucket // remove from bucket
// bucket.splice(Common.indexOf(bucket, body), 1);
bucket.splice(bucket.indexOf(body), 1); bucket.splice(bucket.indexOf(body), 1);
// update pair counts // update pair counts

View file

@ -33,6 +33,7 @@ var Contact = require('./Contact');
activeContacts: [], activeContacts: [],
separation: 0, separation: 0,
isActive: true, isActive: true,
confirmedActive: true,
isSensor: bodyA.isSensor || bodyB.isSensor, isSensor: bodyA.isSensor || bodyB.isSensor,
timeCreated: timestamp, timeCreated: timestamp,
timeUpdated: timestamp, timeUpdated: timestamp,

View file

@ -44,7 +44,6 @@ var Common = require('../core/Common');
collisionStart = pairs.collisionStart, collisionStart = pairs.collisionStart,
collisionEnd = pairs.collisionEnd, collisionEnd = pairs.collisionEnd,
collisionActive = pairs.collisionActive, collisionActive = pairs.collisionActive,
activePairIds = [],
collision, collision,
pairId, pairId,
pair, pair,
@ -55,12 +54,15 @@ var Common = require('../core/Common');
collisionEnd.length = 0; collisionEnd.length = 0;
collisionActive.length = 0; collisionActive.length = 0;
for (i = 0; i < pairsList.length; i++) {
pairsList[i].confirmedActive = false;
}
for (i = 0; i < collisions.length; i++) { for (i = 0; i < collisions.length; i++) {
collision = collisions[i]; collision = collisions[i];
if (collision.collided) { if (collision.collided) {
pairId = Pair.id(collision.bodyA, collision.bodyB); pairId = Pair.id(collision.bodyA, collision.bodyB);
activePairIds.push(pairId);
pair = pairsTable[pairId]; pair = pairsTable[pairId];
@ -76,6 +78,7 @@ var Common = require('../core/Common');
// update the pair // update the pair
Pair.update(pair, collision, timestamp); Pair.update(pair, collision, timestamp);
pair.confirmedActive = true;
} else { } else {
// pair did not exist, create a new pair // pair did not exist, create a new pair
pair = Pair.create(collision, timestamp); pair = Pair.create(collision, timestamp);
@ -91,8 +94,7 @@ var Common = require('../core/Common');
// deactivate previously active pairs that are now inactive // deactivate previously active pairs that are now inactive
for (i = 0; i < pairsList.length; i++) { for (i = 0; i < pairsList.length; i++) {
pair = pairsList[i]; pair = pairsList[i];
// if (pair.isActive && Common.indexOf(activePairIds, pair.id) === -1) { if (!pair.confirmedActive) {
if (pair.isActive && activePairIds.indexOf(pair.id) === -1) {
Pair.setActive(pair, false, timestamp); Pair.setActive(pair, false, timestamp);
collisionEnd.push(pair); collisionEnd.push(pair);
} }

View file

@ -217,7 +217,7 @@ module.exports = Common;
* @return {boolean} True if the object is a string, otherwise false * @return {boolean} True if the object is a string, otherwise false
*/ */
Common.isString = function(obj) { 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 {array} haystack
* @param {object} needle * @param {object} needle
* @return {number} The position of needle in haystack, otherwise -1. * @return {number} The position of needle in haystack, otherwise -1.
*/
Common.indexOf = function(haystack, needle) { Common.indexOf = function(haystack, needle) {
if (haystack.indexOf) if (haystack.indexOf)
return haystack.indexOf(needle); return haystack.indexOf(needle);
@ -381,6 +380,7 @@ module.exports = Common;
return -1; return -1;
}; };
*/
/** /**
* A cross browser compatible array map implementation. * A cross browser compatible array map implementation.