From 6931276cabaa71cfdb67006587f8f572d7ca10c2 Mon Sep 17 00:00:00 2001 From: Brice Chevalier Date: Fri, 24 Nov 2017 10:41:18 +0900 Subject: [PATCH 1/2] Algorithm optimization of Pair methods create and update --- src/collision/Contact.js | 38 ------------------------ src/collision/Pair.js | 64 ++++++++++++++++++---------------------- src/geometry/Vertices.js | 11 +++++-- src/module/main.js | 1 - 4 files changed, 38 insertions(+), 76 deletions(-) delete mode 100644 src/collision/Contact.js diff --git a/src/collision/Contact.js b/src/collision/Contact.js deleted file mode 100644 index aeaa30c..0000000 --- a/src/collision/Contact.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* The `Matter.Contact` module contains methods for creating and manipulating collision contacts. -* -* @class Contact -*/ - -var Contact = {}; - -module.exports = Contact; - -(function() { - - /** - * Creates a new contact. - * @method create - * @param {vertex} vertex - * @return {contact} A new contact - */ - Contact.create = function(vertex) { - return { - id: Contact.id(vertex), - vertex: vertex, - normalImpulse: 0, - tangentImpulse: 0 - }; - }; - - /** - * Generates a contact id. - * @method id - * @param {vertex} vertex - * @return {string} Unique contactID - */ - Contact.id = function(vertex) { - return vertex.body.id + '_' + vertex.index; - }; - -})(); diff --git a/src/collision/Pair.js b/src/collision/Pair.js index 7f654b5..8db57bb 100644 --- a/src/collision/Pair.js +++ b/src/collision/Pair.js @@ -8,8 +8,6 @@ var Pair = {}; module.exports = Pair; -var Contact = require('./Contact'); - (function() { /** @@ -21,26 +19,24 @@ var Contact = require('./Contact'); */ Pair.create = function(collision, timestamp) { var bodyA = collision.bodyA, - bodyB = collision.bodyB, - parentA = collision.parentA, - parentB = collision.parentB; + bodyB = collision.bodyB; var pair = { id: Pair.id(bodyA, bodyB), bodyA: bodyA, bodyB: bodyB, - contacts: {}, activeContacts: [], separation: 0, isActive: true, isSensor: bodyA.isSensor || bodyB.isSensor, timeCreated: timestamp, timeUpdated: timestamp, - inverseMass: parentA.inverseMass + parentB.inverseMass, - friction: Math.min(parentA.friction, parentB.friction), - frictionStatic: Math.max(parentA.frictionStatic, parentB.frictionStatic), - restitution: Math.max(parentA.restitution, parentB.restitution), - slop: Math.max(parentA.slop, parentB.slop) + collision: null, + inverseMass: 0, + friction: 0, + frictionStatic: 0, + restitution: 0, + slop: 0 }; Pair.update(pair, collision, timestamp); @@ -56,31 +52,29 @@ var Contact = require('./Contact'); * @param {number} timestamp */ Pair.update = function(pair, collision, timestamp) { - var contacts = pair.contacts, - supports = collision.supports, - activeContacts = pair.activeContacts, - parentA = collision.parentA, - parentB = collision.parentB; - pair.collision = collision; - pair.inverseMass = parentA.inverseMass + parentB.inverseMass; - pair.friction = Math.min(parentA.friction, parentB.friction); - pair.frictionStatic = Math.max(parentA.frictionStatic, parentB.frictionStatic); - pair.restitution = Math.max(parentA.restitution, parentB.restitution); - pair.slop = Math.max(parentA.slop, parentB.slop); - activeContacts.length = 0; - - if (collision.collided) { - for (var i = 0; i < supports.length; i++) { - var support = supports[i], - contactId = Contact.id(support), - contact = contacts[contactId]; - if (contact) { - activeContacts.push(contact); - } else { - activeContacts.push(contacts[contactId] = Contact.create(support)); - } + if (collision.collided) { + var supports = collision.supports, + activeContacts = pair.activeContacts; + parentA = collision.parentA, + parentB = collision.parentB; + + pair.inverseMass = parentA.inverseMass + parentB.inverseMass; + pair.friction = Math.min(parentA.friction, parentB.friction); + pair.frictionStatic = Math.max(parentA.frictionStatic, parentB.frictionStatic); + pair.restitution = Math.max(parentA.restitution, parentB.restitution); + pair.slop = Math.max(parentA.slop, parentB.slop); + + for (var i = 0; i < supports.length; i++) { + activeContacts[i] = supports[i].contact; + } + + // Optimizing out resizing of the array + // Most likely unnecessary + var supportCount = supports.length; + if (supportCount < activeContacts.length) { + activeContacts.length = supportCount; } pair.separation = collision.depth; @@ -123,4 +117,4 @@ var Contact = require('./Contact'); } }; -})(); +})(); \ No newline at end of file diff --git a/src/geometry/Vertices.js b/src/geometry/Vertices.js index 1dd769f..406118e 100644 --- a/src/geometry/Vertices.js +++ b/src/geometry/Vertices.js @@ -44,9 +44,16 @@ var Common = require('../core/Common'); y: point.y, index: i, body: body, - isInternal: false + isInternal: false, + contact: null }; + vertex.contact = { + vertex: vertex, + normalImpulse: 0, + tangentImpulse: 0 + }; + vertices.push(vertex); } @@ -443,4 +450,4 @@ var Common = require('../core/Common'); return upper.concat(lower); }; -})(); +})(); \ No newline at end of file diff --git a/src/module/main.js b/src/module/main.js index cb7eb7b..504103b 100644 --- a/src/module/main.js +++ b/src/module/main.js @@ -4,7 +4,6 @@ Matter.Body = require('../body/Body'); Matter.Composite = require('../body/Composite'); Matter.World = require('../body/World'); -Matter.Contact = require('../collision/Contact'); Matter.Detector = require('../collision/Detector'); Matter.Grid = require('../collision/Grid'); Matter.Pairs = require('../collision/Pairs'); From 235cb2d8bc2e8489c34b32435a83ac5b4224c28a Mon Sep 17 00:00:00 2001 From: Brice Chevalier Date: Fri, 24 Nov 2017 10:54:52 +0900 Subject: [PATCH 2/2] linted code --- src/collision/Pair.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/collision/Pair.js b/src/collision/Pair.js index 8db57bb..62ba725 100644 --- a/src/collision/Pair.js +++ b/src/collision/Pair.js @@ -56,7 +56,7 @@ module.exports = Pair; if (collision.collided) { var supports = collision.supports, - activeContacts = pair.activeContacts; + activeContacts = pair.activeContacts, parentA = collision.parentA, parentB = collision.parentB; @@ -74,7 +74,7 @@ module.exports = Pair; // Most likely unnecessary var supportCount = supports.length; if (supportCount < activeContacts.length) { - activeContacts.length = supportCount; + activeContacts.length = supportCount; } pair.separation = collision.depth;