From 4e599fdf8090143ac787e179c8dd52d305ded1a2 Mon Sep 17 00:00:00 2001 From: jbphet Date: Wed, 1 Aug 2018 12:09:04 -0600 Subject: [PATCH] added code to correct for potential drift in the monatomic solid "crystal", see https://github.com/phetsims/states-of-matter-basics/issues/15 --- .../model/engine/AbstractPhaseStateChanger.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/js/common/model/engine/AbstractPhaseStateChanger.js b/js/common/model/engine/AbstractPhaseStateChanger.js index cf38a2b2..a91e383f 100644 --- a/js/common/model/engine/AbstractPhaseStateChanger.js +++ b/js/common/model/engine/AbstractPhaseStateChanger.js @@ -27,9 +27,12 @@ define( function( require ) { * @constructor */ function AbstractPhaseStateChanger( multipleParticleModel ) { + + // @private this.multipleParticleModel = multipleParticleModel; this.moleculeLocation = new Vector2(); this.random = phet.joist.random; + this.reusableVector = new Vector2(); } statesOfMatter.register( 'AbstractPhaseStateChanger', AbstractPhaseStateChanger ); @@ -128,6 +131,7 @@ define( function( require ) { var moleculesPlaced = 0; var xPos; var yPos; + this.reusableVector.setXY( 0, 0 ); for ( var i = 0; i < numberOfMolecules; i++ ) { // Position one layer of molecules. @@ -149,6 +153,9 @@ define( function( require ) { var yVel = temperatureSqrt * this.random.nextGaussian(); moleculeVelocities[ moleculeIndex ].setXY( xVel, yVel ); + // Track total velocity in the X direction. + this.reusableVector.addXY( xVel, yVel ); + // Assign an initial rotational angle (has no effect for single-atom data sets) moleculeRotationAngles[ moleculeIndex ] = randomizeRotationalAngle ? this.random.nextDouble() * 2 * Math.PI : @@ -158,6 +165,14 @@ define( function( require ) { moleculesInsideContainer[ i ] = true; } } + + // The number of particles is often not large enough to count on having a zero average X and Y velocity, so the + // substance can end up with some initial drive. This compensates for that problem. + var xAdjustment = -this.reusableVector.x / numberOfMolecules; + var yAdjustment = -this.reusableVector.y / numberOfMolecules; + for ( i = 0; i < numberOfMolecules; i++ ) { + moleculeVelocities[ i ].addXY( xAdjustment, yAdjustment ); + } }, /**