Skip to content

Commit d8a6380

Browse files
committed
optimised Matter.Pair
1 parent 8125966 commit d8a6380

File tree

2 files changed

+21
-37
lines changed

2 files changed

+21
-37
lines changed

src/collision/Contact.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,10 @@ module.exports = Contact;
1818
*/
1919
Contact.create = function(vertex) {
2020
return {
21-
id: Contact.id(vertex),
2221
vertex: vertex,
2322
normalImpulse: 0,
2423
tangentImpulse: 0
2524
};
2625
};
27-
28-
/**
29-
* Generates a contact id.
30-
* @method id
31-
* @param {vertex} vertex
32-
* @return {string} Unique contactID
33-
*/
34-
Contact.id = function(vertex) {
35-
return vertex.body.id + '_' + vertex.index;
36-
};
3726

3827
})();

src/collision/Pair.js

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,25 @@ var Contact = require('./Contact');
2121
*/
2222
Pair.create = function(collision, timestamp) {
2323
var bodyA = collision.bodyA,
24-
bodyB = collision.bodyB,
25-
parentA = collision.parentA,
26-
parentB = collision.parentB;
24+
bodyB = collision.bodyB;
2725

2826
var pair = {
2927
id: Pair.id(bodyA, bodyB),
3028
bodyA: bodyA,
3129
bodyB: bodyB,
32-
contacts: {},
30+
contacts: [],
3331
activeContacts: [],
3432
separation: 0,
3533
isActive: true,
3634
confirmedActive: true,
3735
isSensor: bodyA.isSensor || bodyB.isSensor,
3836
timeCreated: timestamp,
3937
timeUpdated: timestamp,
40-
inverseMass: parentA.inverseMass + parentB.inverseMass,
41-
friction: parentA.friction < parentB.friction ? parentA.friction : parentB.friction,
42-
frictionStatic: parentA.frictionStatic > parentB.frictionStatic ? parentA.frictionStatic : parentB.frictionStatic,
43-
restitution: parentA.restitution > parentB.restitution ? parentA.restitution : parentB.restitution,
44-
slop: parentA.slop > parentB.slop ? parentA.slop : parentB.slop
38+
inverseMass: 0,
39+
friction: 0,
40+
frictionStatic: 0,
41+
restitution: 0,
42+
slop: 0
4543
};
4644

4745
Pair.update(pair, collision, timestamp);
@@ -61,34 +59,31 @@ var Contact = require('./Contact');
6159
supports = collision.supports,
6260
activeContacts = pair.activeContacts,
6361
parentA = collision.parentA,
64-
parentB = collision.parentB;
62+
parentB = collision.parentB,
63+
parentAVerticesLength = parentA.vertices.length;
6564

65+
pair.isActive = true;
66+
pair.timeUpdated = timestamp;
6667
pair.collision = collision;
68+
pair.separation = collision.depth;
6769
pair.inverseMass = parentA.inverseMass + parentB.inverseMass;
6870
pair.friction = parentA.friction < parentB.friction ? parentA.friction : parentB.friction;
6971
pair.frictionStatic = parentA.frictionStatic > parentB.frictionStatic ? parentA.frictionStatic : parentB.frictionStatic;
7072
pair.restitution = parentA.restitution > parentB.restitution ? parentA.restitution : parentB.restitution;
7173
pair.slop = parentA.slop > parentB.slop ? parentA.slop : parentB.slop;
74+
7275
activeContacts.length = 0;
7376

74-
if (collision.collided) {
75-
for (var i = 0; i < supports.length; i++) {
76-
var support = supports[i],
77-
contactId = Contact.id(support),
78-
contact = contacts[contactId];
77+
for (var i = 0; i < supports.length; i++) {
78+
var support = supports[i],
79+
contactId = support.body === parentA ? support.index : parentAVerticesLength + support.index,
80+
contact = contacts[contactId];
7981

80-
if (contact) {
81-
activeContacts.push(contact);
82-
} else {
83-
activeContacts.push(contacts[contactId] = Contact.create(support));
84-
}
82+
if (contact) {
83+
activeContacts.push(contact);
84+
} else {
85+
activeContacts.push(contacts[contactId] = Contact.create(support));
8586
}
86-
87-
pair.separation = collision.depth;
88-
Pair.setActive(pair, true, timestamp);
89-
} else {
90-
if (pair.isActive === true)
91-
Pair.setActive(pair, false, timestamp);
9287
}
9388
};
9489

0 commit comments

Comments
 (0)