Skip to content

Commit

Permalink
Separation optimizations: Lower iterations for separations, use mutab…
Browse files Browse the repository at this point in the history
…le Vector2.add(), and move numMolecules declaration out of loops. #115
  • Loading branch information
Denz1994 committed Mar 9, 2020
1 parent 5104eb3 commit 6888d1d
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions js/common/model/Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class Kit {
return bucket.element.isSameElement( element );
} );
assert && assert( elementBucket !== null, 'Element does not have an associated bucket.' );
return elementBucket
return elementBucket;
}

/**
Expand Down Expand Up @@ -296,10 +296,9 @@ class Kit {
*
* @param {Atom2} a - Atom A
* @param {Atom2} b - Atom B
* @param {boolean} skipSeparation
* @public
*/
breakBond( a, b, skipSeparation ) {
breakBond( a, b ) {

// get our old and new molecule structures
const oldMolecule = this.getMolecule( a );
Expand All @@ -313,9 +312,7 @@ class Kit {
newMolecules.forEach( this.addMolecule.bind( this ) );

// push the new separate molecules away
if ( !skipSeparation ) {
this.separateMoleculeDestinations();
}
this.separateMoleculeDestinations();
}

/**
Expand Down Expand Up @@ -445,15 +442,14 @@ class Kit {
* @private
*/
separateMoleculeDestinations() {
// TODO: performance: general optimization
let maxIterations = 500;
let maxIterations = 200;
const pushAmount = 10; // how much to push two molecules away
const availablePlayAreaBounds = this.collectionLayout.availablePlayAreaBounds;
const numMolecules = this.molecules.length;

let foundOverlap = true;
while ( foundOverlap && maxIterations-- >= 0 ) {
foundOverlap = false;
const numMolecules = this.molecules.length;
for ( let i = 0; i < numMolecules; i++ ) {
const a = this.molecules[ i ];

Expand Down Expand Up @@ -489,8 +485,8 @@ class Kit {
foundOverlap = true;

// get perturbed centers. this is so that if two molecules have the exact same centers, we will push them away
const aCenter = aBounds.center.plus( new Vector2( phet.joist.random.nextDouble() - 0.5, phet.joist.random.nextDouble() - 0.5 ) );
const bCenter = bBounds.center.plus( new Vector2( phet.joist.random.nextDouble() - 0.5, phet.joist.random.nextDouble() - 0.5 ) );
const aCenter = aBounds.center.add( new Vector2( phet.joist.random.nextDouble() - 0.5, phet.joist.random.nextDouble() - 0.5 ) );
const bCenter = bBounds.center.add( new Vector2( phet.joist.random.nextDouble() - 0.5, phet.joist.random.nextDouble() - 0.5 ) );

// delta from center of A to center of B, scaled to half of our push amount.
const delta = bCenter.minus( aCenter ).normalized().times( pushAmount );
Expand Down

0 comments on commit 6888d1d

Please sign in to comment.