Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast World.contactMaterials lookup #287

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/material/ContactMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,20 @@ function ContactMaterial(materialA, materialB, options){
}

ContactMaterial.idCounter = 0;

/**
* Returns unique hash that was generated based on material id's. Usage: World.contactMaterials[hash].
* @private
* @method getHash
*/
ContactMaterial.prototype.getHash = function(){
return ContactMaterial.createHash(this.materialA, this.materialB);
};

ContactMaterial.createHash = function(materialA, materialB){
if (materialA.id <= materialB.id) {
return materialA.id + '-' + materialB.id;
} else {
return materialB.id + '-' + materialA.id;
}
};
34 changes: 13 additions & 21 deletions src/world/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ function World(options){
this.solveConstraints = true;

/**
* The ContactMaterials added to the World.
* Hash object containing ContactMaterials added to the World.
* @property contactMaterials
* @type {Array}
* @type {Object}
*/
this.contactMaterials = [];
this.contactMaterials = {};

/**
* World time.
Expand Down Expand Up @@ -400,7 +400,7 @@ World.prototype.addConstraint = function(constraint){
* @param {ContactMaterial} contactMaterial
*/
World.prototype.addContactMaterial = function(contactMaterial){
this.contactMaterials.push(contactMaterial);
this.contactMaterials[contactMaterial.getHash()] = contactMaterial;
};

/**
Expand All @@ -410,7 +410,7 @@ World.prototype.addContactMaterial = function(contactMaterial){
* @param {ContactMaterial} cm
*/
World.prototype.removeContactMaterial = function(cm){
arrayRemove(this.contactMaterials, cm);
delete this.contactMaterials[cm.getHash()];
};

/**
Expand All @@ -419,17 +419,9 @@ World.prototype.removeContactMaterial = function(cm){
* @param {Material} materialA
* @param {Material} materialB
* @return {ContactMaterial} The matching ContactMaterial, or false on fail.
* @todo Use faster hash map to lookup from material id's
*/
World.prototype.getContactMaterial = function(materialA,materialB){
var cmats = this.contactMaterials;
for(var i=0, N=cmats.length; i!==N; i++){
var cm = cmats[i];
if((cm.materialA === materialA && cm.materialB === materialB) || (cm.materialA === materialB && cm.materialB === materialA)){
return cm;
}
}
return false;
return this.contactMaterials[ContactMaterial.createHash(materialA, materialB)] || false;
};

/**
Expand Down Expand Up @@ -1166,9 +1158,8 @@ World.prototype.clear = function(){

// Remove all contact materials
var cms = this.contactMaterials;
i = cms.length;
while(i--){
this.removeContactMaterial(cms[i]);
for (var hash in cms) {
delete cms[hash];
}
};

Expand Down Expand Up @@ -1234,8 +1225,8 @@ World.prototype.setGlobalStiffness = function(stiffness){

// Set for all contact materials
var contactMaterials = this.contactMaterials;
for(var i=0; i !== contactMaterials.length; i++){
var c = contactMaterials[i];
for(var hash in contactMaterials){
var c = contactMaterials[hash];
c.stiffness = c.frictionStiffness = stiffness;
}

Expand All @@ -1253,8 +1244,9 @@ World.prototype.setGlobalRelaxation = function(relaxation){
setGlobalEquationParams(this, { relaxation: relaxation });

// Set for all contact materials
for(var i=0; i !== this.contactMaterials.length; i++){
var c = this.contactMaterials[i];
var contactMaterials = this.contactMaterials;
for(var hash in contactMaterials){
var c = contactMaterials[hash];
c.relaxation = c.frictionRelaxation = relaxation;
}

Expand Down
2 changes: 1 addition & 1 deletion test/world/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ exports.clear = function(test){
test.deepEqual(world.bodies, []);
test.deepEqual(world.springs, []);
test.deepEqual(world.constraints, []);
test.deepEqual(world.contactMaterials, []);
test.deepEqual(world.contactMaterials, {});

test.done();
};
Expand Down