Skip to content

Commit

Permalink
adding ids in constructors instead of in World
Browse files Browse the repository at this point in the history
  • Loading branch information
schteppe committed Feb 6, 2014
1 parent c5771b7 commit 1499e97
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/constraints/Constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ function Constraint(bodyA,bodyB){
* @property {Body} bodyB
*/
this.bodyB = bodyB;

/**
* @property {Number} id
*/
this.id = Constraint.idCounter++;
};

/**
Expand All @@ -33,3 +38,5 @@ function Constraint(bodyA,bodyB){
Constraint.prototype.update = function(){
throw new Error("method update() not implmemented in this Constraint subclass!");
};

Constraint.idCounter = 0;
3 changes: 2 additions & 1 deletion src/material/ContactMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function ContactMaterial(m1, m2, friction, restitution){
* Identifier of this material
* @property {Number} id
*/
this.id = -1;
this.id = ContactMaterial.idCounter++;

/**
* Participating materials
Expand Down Expand Up @@ -62,3 +62,4 @@ function ContactMaterial(m1, m2, friction, restitution){
this.frictionEquationRegularizationTime = 3;
};

ContactMaterial.idCounter = 0;
3 changes: 2 additions & 1 deletion src/material/Material.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function Material(name){
* @type {String}
*/
this.name = name;
this.id = -1;
this.id = Material.idCounter++;
};

Material.idCounter = 0;
4 changes: 4 additions & 0 deletions src/objects/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function Body(type){

this.type = type;

this.id = Body.idCounter++;

/**
* Reference to the world the body is living in
* @property world
Expand Down Expand Up @@ -82,3 +84,5 @@ Body.STATIC = 2;
* @type {Number}
*/
Body.KINEMATIC = 4;

Body.idCounter = 0;
24 changes: 2 additions & 22 deletions src/world/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ World.prototype.collisionMatrixTick = function(){
* @todo Adding an array of bodies should be possible. This would save some loops too
*/
World.prototype.add = function(body){
body.id = this.id();
body.index = this.bodies.length;
this.bodies.push(body);
body.world = this;
Expand All @@ -251,7 +250,6 @@ World.prototype.add = function(body){
*/
World.prototype.addConstraint = function(c){
this.constraints.push(c);
c.id = this.id();
};

/**
Expand All @@ -266,15 +264,6 @@ World.prototype.removeConstraint = function(c){
}
};

/**
* Generate a new unique integer identifyer
* @method id
* @return {Number}
*/
World.prototype.id = function(){
return this.nextId++;
};

/**
* Remove a rigid body from the simulation.
* @method remove
Expand All @@ -294,17 +283,13 @@ World.prototype.remove = function(body){
};

/**
* Adds a material to the World. A material can only be added once, it's added more times then nothing will happen.
* Adds a material to the World.
* @method addMaterial
* @param {Material} m
* @todo Necessary?
*/
World.prototype.addMaterial = function(m){
if(m.id === -1){
var n = this.materials.length;
this.materials.push(m);
m.id = this.materials.length-1;
}
this.materials.push(m);
};

/**
Expand All @@ -314,13 +299,8 @@ World.prototype.addMaterial = function(m){
*/
World.prototype.addContactMaterial = function(cmat) {

// Add materials if they aren't already added
this.addMaterial(cmat.materials[0]);
this.addMaterial(cmat.materials[1]);

// Add contact material
this.contactmaterials.push(cmat);
cmat.id = this.contactmaterials.length-1;

// Add current contact material to the material table
this.contactMaterialTable.set(cmat.materials[0].id,cmat.materials[1].id,cmat);
Expand Down

0 comments on commit 1499e97

Please sign in to comment.