0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-01-12 16:08:50 -05:00

optimised Matter.Pair

This commit is contained in:
liabru 2021-12-05 16:56:41 +00:00
parent 81259663fd
commit d8a6380899
2 changed files with 21 additions and 37 deletions

View file

@ -18,21 +18,10 @@ module.exports = Contact;
*/ */
Contact.create = function(vertex) { Contact.create = function(vertex) {
return { return {
id: Contact.id(vertex),
vertex: vertex, vertex: vertex,
normalImpulse: 0, normalImpulse: 0,
tangentImpulse: 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;
};
})(); })();

View file

@ -21,15 +21,13 @@ 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: {}, contacts: [],
activeContacts: [], activeContacts: [],
separation: 0, separation: 0,
isActive: true, isActive: true,
@ -37,11 +35,11 @@ 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, inverseMass: 0,
friction: parentA.friction < parentB.friction ? parentA.friction : parentB.friction, friction: 0,
frictionStatic: parentA.frictionStatic > parentB.frictionStatic ? parentA.frictionStatic : parentB.frictionStatic, frictionStatic: 0,
restitution: parentA.restitution > parentB.restitution ? parentA.restitution : parentB.restitution, restitution: 0,
slop: parentA.slop > parentB.slop ? parentA.slop : parentB.slop slop: 0
}; };
Pair.update(pair, collision, timestamp); Pair.update(pair, collision, timestamp);
@ -61,34 +59,31 @@ var Contact = require('./Contact');
supports = collision.supports, supports = collision.supports,
activeContacts = pair.activeContacts, activeContacts = pair.activeContacts,
parentA = collision.parentA, parentA = collision.parentA,
parentB = collision.parentB; parentB = collision.parentB,
parentAVerticesLength = parentA.vertices.length;
pair.isActive = true;
pair.timeUpdated = timestamp;
pair.collision = collision; pair.collision = collision;
pair.separation = collision.depth;
pair.inverseMass = parentA.inverseMass + parentB.inverseMass; pair.inverseMass = parentA.inverseMass + parentB.inverseMass;
pair.friction = parentA.friction < parentB.friction ? parentA.friction : parentB.friction; pair.friction = parentA.friction < parentB.friction ? parentA.friction : parentB.friction;
pair.frictionStatic = parentA.frictionStatic > parentB.frictionStatic ? parentA.frictionStatic : parentB.frictionStatic; pair.frictionStatic = parentA.frictionStatic > parentB.frictionStatic ? parentA.frictionStatic : parentB.frictionStatic;
pair.restitution = parentA.restitution > parentB.restitution ? parentA.restitution : parentB.restitution; pair.restitution = parentA.restitution > parentB.restitution ? parentA.restitution : parentB.restitution;
pair.slop = parentA.slop > parentB.slop ? parentA.slop : parentB.slop; pair.slop = parentA.slop > parentB.slop ? parentA.slop : parentB.slop;
activeContacts.length = 0; activeContacts.length = 0;
if (collision.collided) { for (var i = 0; i < supports.length; i++) {
for (var i = 0; i < supports.length; i++) { var support = supports[i],
var support = supports[i], contactId = support.body === parentA ? support.index : parentAVerticesLength + support.index,
contactId = Contact.id(support), contact = contacts[contactId];
contact = contacts[contactId];
if (contact) { if (contact) {
activeContacts.push(contact); activeContacts.push(contact);
} else { } else {
activeContacts.push(contacts[contactId] = Contact.create(support)); activeContacts.push(contacts[contactId] = Contact.create(support));
}
} }
pair.separation = collision.depth;
Pair.setActive(pair, true, timestamp);
} else {
if (pair.isActive === true)
Pair.setActive(pair, false, timestamp);
} }
}; };