-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use ToleranceProperty for applied force and displacement, to avoid up…
…date cycles, #52 Signed-off-by: Chris Malley <cmalley@pixelzoom.com>
- Loading branch information
Showing
2 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2018, University of Colorado Boulder | ||
|
||
/** | ||
* NumberProperty that sets its values only if the new value is sufficiently different from the current value. | ||
* Used to avoid update cycles that are due to floating point error. | ||
* See https://github.com/phetsims/hookes-law/issues/52 | ||
* | ||
* @author Chris Malley (PixelZoom, Inc.) | ||
*/ | ||
define( function( require ) { | ||
'use strict'; | ||
|
||
// modules | ||
var hookesLaw = require( 'HOOKES_LAW/hookesLaw' ); | ||
var inherit = require( 'PHET_CORE/inherit' ); | ||
var NumberProperty = require( 'AXON/NumberProperty' ); | ||
|
||
/** | ||
* @param {number} value | ||
* @param {Object} options | ||
* @constructor | ||
*/ | ||
function ToleranceProperty( value, options ) { | ||
|
||
options = _.extend( { | ||
tolerance: 1e-10 | ||
}, options ); | ||
|
||
// @private | ||
this.tolerance = options.tolerance; | ||
|
||
NumberProperty.call( this, value, options ); | ||
} | ||
|
||
hookesLaw.register( 'ToleranceProperty', ToleranceProperty ); | ||
|
||
return inherit( NumberProperty, ToleranceProperty, { | ||
|
||
/** | ||
* Sets the value if it's difference from the current value exceeds some tolerance. | ||
* Otherwise the value is not set, silently ignored, and this is a no-op. | ||
* @param {number} value | ||
*/ | ||
set: function( value ) { | ||
if ( Math.abs( value - this.get() ) > this.tolerance ) { | ||
NumberProperty.prototype.set.call( this, value ); | ||
} | ||
} | ||
} ); | ||
} ); |