Skip to content

Commit

Permalink
defined DerivedProperties after listeners that update their dependenc…
Browse files Browse the repository at this point in the history
…ies, to mitigate intermediate values, #52

Signed-off-by: Chris Malley <cmalley@pixelzoom.com>
  • Loading branch information
pixelzoom committed Aug 18, 2018
1 parent dab4f7d commit 6b2f38b
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions js/common/model/Spring.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,52 @@ define( function( require ) {
phet.log && this.leftProperty.link(
function( left ) { phet.log( options.logName + ' left=' + left ); } );

//------------------------------------------------
// Property observers

// F: When applied force changes, maintain the spring constant, change displacement.
this.appliedForceProperty.link( function( appliedForce ) {
assert && assert( self.appliedForceRange.contains( appliedForce ),
'appliedForce is out of range: ' + appliedForce );

// x = F/k
self.displacementProperty.set( appliedForce / self.springConstantProperty.get() );
} );

// k: When spring constant changes, adjust either displacement or applied force.
this.springConstantProperty.link( function( springConstant ) {
assert && assert( self.springConstantRange.contains( springConstant ),
'springConstant is out of range: ' + springConstant );

if ( options.appliedForceRange ) {

// If the applied force range was specified, then maintain the applied force, change displacement.
// x = F/k
self.displacementProperty.set( self.appliedForceProperty.get() / springConstant );
}
else {

// The displacement range was specified via options - maintain the displacement, change applied force.
// This applies to the Energy screen.
// F = kx
self.appliedForceProperty.set( springConstant * self.displacementProperty.get() );
}
} );

// x: When displacement changes, maintain the spring constant, change applied force.
this.displacementProperty.link( function( displacement ) {
assert && assert( self.displacementRange.contains( displacement ),
'displacement is out of range: ' + displacement );

// F = kx
var appliedForce = self.springConstantProperty.get() * displacement;

// Constrain to range, needed due to floating point error.
appliedForce = self.appliedForceRange.constrainValue( appliedForce );

self.appliedForceProperty.set( appliedForce );
} );

//------------------------------------------------
// Derived properties

Expand Down Expand Up @@ -233,52 +279,6 @@ define( function( require ) {
phet.log && this.potentialEnergyProperty.link(
function( potentialEnergy ) { phet.log( options.logName + ' potentialEnergy=' + potentialEnergy ); } );

//------------------------------------------------
// Property observers

// F: When applied force changes, maintain the spring constant, change displacement.
this.appliedForceProperty.link( function( appliedForce ) {
assert && assert( self.appliedForceRange.contains( appliedForce ),
'appliedForce is out of range: ' + appliedForce );

// x = F/k
self.displacementProperty.set( appliedForce / self.springConstantProperty.get() );
} );

// k: When spring constant changes, adjust either displacement or applied force.
this.springConstantProperty.link( function( springConstant ) {
assert && assert( self.springConstantRange.contains( springConstant ),
'springConstant is out of range: ' + springConstant );

if ( options.appliedForceRange ) {

// If the applied force range was specified, then maintain the applied force, change displacement.
// x = F/k
self.displacementProperty.set( self.appliedForceProperty.get() / springConstant );
}
else {

// The displacement range was specified via options - maintain the displacement, change applied force.
// This applies to the Energy screen.
// F = kx
self.appliedForceProperty.set( springConstant * self.displacementProperty.get() );
}
} );

// x: When displacement changes, maintain the spring constant, change applied force.
this.displacementProperty.link( function( displacement ) {
assert && assert( self.displacementRange.contains( displacement ),
'displacement is out of range: ' + displacement );

// F = kx
var appliedForce = self.springConstantProperty.get() * displacement;

// Constrain to range, needed due to floating point error.
appliedForce = self.appliedForceRange.constrainValue( appliedForce );

self.appliedForceProperty.set( appliedForce );
} );

PhetioObject.call( this, options );
}

Expand Down

0 comments on commit 6b2f38b

Please sign in to comment.