Skip to content

Commit

Permalink
Optimize IterativeSolver.processContactManifolds, closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
chandlerprall committed Nov 17, 2015
1 parent 9633a16 commit 9f1ecfb
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 13 deletions.
21 changes: 19 additions & 2 deletions build/goblin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6327,6 +6327,14 @@ Goblin.GeometryMethods = {
}
};
})();
Goblin.Utility = {
getUid: (function() {
var uid = 0;
return function() {
return uid++;
};
})()
};
/**
* Extends a given shape by sweeping a line around it
*
Expand Down Expand Up @@ -6887,6 +6895,8 @@ Goblin.AABB.prototype.testRayIntersect = (function(){
* @constructor
*/
Goblin.ContactDetails = function() {
this.uid = Goblin.Utility.getUid();

/**
* first body in the contact
*
Expand Down Expand Up @@ -7276,6 +7286,8 @@ Goblin.GhostBody.prototype.checkForEndedContacts = function() {
* @constructor
*/
Goblin.IterativeSolver = function() {
this.existing_contact_ids = {};

/**
* Holds contact constraints generated from contact manifolds
*
Expand Down Expand Up @@ -7362,6 +7374,8 @@ Goblin.IterativeSolver = function() {

var idx = solver.contact_constraints.indexOf( this );
solver.contact_constraints.splice( idx, 1 );

delete solver.existing_contact_ids[ this.contact.uid ];
};
/**
* used to remove friction constraints from the system when their contacts are destroyed
Expand Down Expand Up @@ -7424,15 +7438,18 @@ Goblin.IterativeSolver.prototype.processContactManifolds = function( contact_man
for ( i = 0; i < contacts_length; i++ ) {
contact = manifold.points[i];

var existing_constraint = null;
/*var existing_constraint = null;
for ( j = 0; j < this.contact_constraints.length; j++ ) {
if ( this.contact_constraints[j].contact === contact ) {
existing_constraint = this.contact_constraints[j];
break;
}
}
}*/
var existing_constraint = this.existing_contact_ids.hasOwnProperty( contact.uid );

if ( !existing_constraint ) {
this.existing_contact_ids[contact.uid] = true;

// Build contact constraint
constraint = Goblin.ObjectPool.getObject( 'ContactConstraint' );
constraint.buildFromContact( contact );
Expand Down
4 changes: 2 additions & 2 deletions build/goblin.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/boxes.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
box.goblin.position.y = 1;
box.goblin.linear_velocity.y = 15;
box.goblin.restitution = 0.3;
box.goblin.friction = 3;
};

exampleUtils.world.addListener(
Expand Down
1 change: 0 additions & 1 deletion examples/compound-shapes.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<script type="text/javascript">
window.onload = function() {
exampleUtils.initialize(); // Creates Three.js renderer & GoblinPhysics world
exampleUtils.world.solver.relaxation = 0.6; // Resolve penetrations more aggressively

var ground = exampleUtils.createPlane( 1, 20, 20, 0, exampleUtils.createMaterial( 'pebbles', 5, 5 ) );
ground.goblin.restitution = 0.1;
Expand Down
1 change: 1 addition & 0 deletions examples/mesh-mesh.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<script type="text/javascript">
window.onload = function() {
exampleUtils.initialize(); // Creates Three.js renderer & GoblinPhysics world
exampleUtils.world.solver.relaxation = 0.1; // relax penetrations solving to avoid jitter

// Making ground a MeshShape as well because Mesh-Mesh collision is MUCH faster than Mesh-Convex
var ground_geometry = new THREE.PlaneGeometry( 50, 50 );
Expand Down
2 changes: 1 addition & 1 deletion examples/mesh-shape.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
)
);

mesh.goblin = new Goblin.RigidBody( shape, 15 );
mesh.goblin = new Goblin.RigidBody( shape, 30 );
mesh.goblin.position.y = 1.85;

exampleUtils.objects.push( mesh );
Expand Down
2 changes: 2 additions & 0 deletions src/classes/ContactDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @constructor
*/
Goblin.ContactDetails = function() {
this.uid = Goblin.Utility.getUid();

/**
* first body in the contact
*
Expand Down
14 changes: 7 additions & 7 deletions src/classes/IterativeSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @constructor
*/
Goblin.IterativeSolver = function() {
this.existing_contact_ids = {};

/**
* Holds contact constraints generated from contact manifolds
*
Expand Down Expand Up @@ -91,6 +93,8 @@ Goblin.IterativeSolver = function() {

var idx = solver.contact_constraints.indexOf( this );
solver.contact_constraints.splice( idx, 1 );

delete solver.existing_contact_ids[ this.contact.uid ];
};
/**
* used to remove friction constraints from the system when their contacts are destroyed
Expand Down Expand Up @@ -153,15 +157,11 @@ Goblin.IterativeSolver.prototype.processContactManifolds = function( contact_man
for ( i = 0; i < contacts_length; i++ ) {
contact = manifold.points[i];

var existing_constraint = null;
for ( j = 0; j < this.contact_constraints.length; j++ ) {
if ( this.contact_constraints[j].contact === contact ) {
existing_constraint = this.contact_constraints[j];
break;
}
}
var existing_constraint = this.existing_contact_ids.hasOwnProperty( contact.uid );

if ( !existing_constraint ) {
this.existing_contact_ids[contact.uid] = true;

// Build contact constraint
constraint = Goblin.ObjectPool.getObject( 'ContactConstraint' );
constraint.buildFromContact( contact );
Expand Down
8 changes: 8 additions & 0 deletions src/classes/Utils/Utility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Goblin.Utility = {
getUid: (function() {
var uid = 0;
return function() {
return uid++;
};
})()
};

0 comments on commit 9f1ecfb

Please sign in to comment.