mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-23 09:26:51 -05:00
Merge branch 'pr/526'
* pr/526: linted code Algorithm optimization of Pair methods create and update
This commit is contained in:
commit
52f3734b3e
4 changed files with 37 additions and 76 deletions
|
@ -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;
|
|
||||||
};
|
|
||||||
|
|
||||||
})();
|
|
|
@ -8,8 +8,6 @@ var Pair = {};
|
||||||
|
|
||||||
module.exports = Pair;
|
module.exports = Pair;
|
||||||
|
|
||||||
var Contact = require('./Contact');
|
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,15 +19,12 @@ var Contact = require('./Contact');
|
||||||
*/
|
*/
|
||||||
Pair.create = function(collision, timestamp) {
|
Pair.create = function(collision, timestamp) {
|
||||||
var bodyA = collision.bodyA,
|
var bodyA = collision.bodyA,
|
||||||
bodyB = collision.bodyB,
|
bodyB = collision.bodyB;
|
||||||
parentA = collision.parentA,
|
|
||||||
parentB = collision.parentB;
|
|
||||||
|
|
||||||
var pair = {
|
var pair = {
|
||||||
id: Pair.id(bodyA, bodyB),
|
id: Pair.id(bodyA, bodyB),
|
||||||
bodyA: bodyA,
|
bodyA: bodyA,
|
||||||
bodyB: bodyB,
|
bodyB: bodyB,
|
||||||
contacts: {},
|
|
||||||
activeContacts: [],
|
activeContacts: [],
|
||||||
separation: 0,
|
separation: 0,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
|
@ -37,11 +32,12 @@ var Contact = require('./Contact');
|
||||||
isSensor: bodyA.isSensor || bodyB.isSensor,
|
isSensor: bodyA.isSensor || bodyB.isSensor,
|
||||||
timeCreated: timestamp,
|
timeCreated: timestamp,
|
||||||
timeUpdated: timestamp,
|
timeUpdated: timestamp,
|
||||||
inverseMass: parentA.inverseMass + parentB.inverseMass,
|
collision: null,
|
||||||
friction: Math.min(parentA.friction, parentB.friction),
|
inverseMass: 0,
|
||||||
frictionStatic: Math.max(parentA.frictionStatic, parentB.frictionStatic),
|
friction: 0,
|
||||||
restitution: Math.max(parentA.restitution, parentB.restitution),
|
frictionStatic: 0,
|
||||||
slop: Math.max(parentA.slop, parentB.slop)
|
restitution: 0,
|
||||||
|
slop: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
Pair.update(pair, collision, timestamp);
|
Pair.update(pair, collision, timestamp);
|
||||||
|
@ -57,31 +53,28 @@ var Contact = require('./Contact');
|
||||||
* @param {number} timestamp
|
* @param {number} timestamp
|
||||||
*/
|
*/
|
||||||
Pair.update = function(pair, collision, 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.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) {
|
if (collision.collided) {
|
||||||
activeContacts.push(contact);
|
var supports = collision.supports,
|
||||||
} else {
|
activeContacts = pair.activeContacts,
|
||||||
activeContacts.push(contacts[contactId] = Contact.create(support));
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// optimise array size
|
||||||
|
var supportCount = supports.length;
|
||||||
|
if (supportCount < activeContacts.length) {
|
||||||
|
activeContacts.length = supportCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
pair.separation = collision.depth;
|
pair.separation = collision.depth;
|
||||||
|
@ -124,4 +117,4 @@ var Contact = require('./Contact');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
|
@ -44,9 +44,16 @@ var Common = require('../core/Common');
|
||||||
y: point.y,
|
y: point.y,
|
||||||
index: i,
|
index: i,
|
||||||
body: body,
|
body: body,
|
||||||
isInternal: false
|
isInternal: false,
|
||||||
|
contact: null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
vertex.contact = {
|
||||||
|
vertex: vertex,
|
||||||
|
normalImpulse: 0,
|
||||||
|
tangentImpulse: 0
|
||||||
|
};
|
||||||
|
|
||||||
vertices.push(vertex);
|
vertices.push(vertex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -444,4 +451,4 @@ var Common = require('../core/Common');
|
||||||
return upper.concat(lower);
|
return upper.concat(lower);
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
|
@ -4,7 +4,6 @@ Matter.Body = require('../body/Body');
|
||||||
Matter.Composite = require('../body/Composite');
|
Matter.Composite = require('../body/Composite');
|
||||||
Matter.World = require('../body/World');
|
Matter.World = require('../body/World');
|
||||||
|
|
||||||
Matter.Contact = require('../collision/Contact');
|
|
||||||
Matter.Detector = require('../collision/Detector');
|
Matter.Detector = require('../collision/Detector');
|
||||||
Matter.Grid = require('../collision/Grid');
|
Matter.Grid = require('../collision/Grid');
|
||||||
Matter.Pairs = require('../collision/Pairs');
|
Matter.Pairs = require('../collision/Pairs');
|
||||||
|
|
Loading…
Reference in a new issue