mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-24 13:29:01 -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;
|
||||
|
||||
var Contact = require('./Contact');
|
||||
|
||||
(function() {
|
||||
|
||||
/**
|
||||
|
@ -21,15 +19,12 @@ 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,
|
||||
|
@ -37,11 +32,12 @@ var Contact = require('./Contact');
|
|||
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);
|
||||
|
@ -57,31 +53,28 @@ 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;
|
||||
}
|
||||
|
||||
// optimise array size
|
||||
var supportCount = supports.length;
|
||||
if (supportCount < activeContacts.length) {
|
||||
activeContacts.length = supportCount;
|
||||
}
|
||||
|
||||
pair.separation = collision.depth;
|
||||
|
@ -124,4 +117,4 @@ var Contact = require('./Contact');
|
|||
}
|
||||
};
|
||||
|
||||
})();
|
||||
})();
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -444,4 +451,4 @@ var Common = require('../core/Common');
|
|||
return upper.concat(lower);
|
||||
};
|
||||
|
||||
})();
|
||||
})();
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue