From ab160ee9a40a3b23364764bc926bf8428e5da539 Mon Sep 17 00:00:00 2001 From: samreid Date: Wed, 27 Feb 2019 06:39:55 -0700 Subject: [PATCH] Added phase matching so the chart doesn't get jagged --- js/model/ACSource.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/js/model/ACSource.js b/js/model/ACSource.js index ca1ca642e..0f40011ed 100644 --- a/js/model/ACSource.js +++ b/js/model/ACSource.js @@ -52,6 +52,19 @@ define( require => { // @public (read-only) {number} - the number of decimal places to show in readouts and controls this.numberOfDecimalPlaces = 1; + + // @private + this.phase = 0; + + // @private + this.time = 0; + + // Phase matching so the chart doesn't get jagged + this.frequencyProperty.link( ( frequency, oldFrequency ) => { + const oldArgument = 2 * Math.PI * oldFrequency * this.time + this.phase; + // 2 * Math.PI * frequency * this.time + newPhase = oldArg; + this.phase = oldArgument - 2 * Math.PI * frequency * this.time; + } ); } /** @@ -83,8 +96,9 @@ define( require => { * @public */ step( time, dt ) { + this.time = time; this.voltageProperty.set( - this.maximumVoltageProperty.value * Math.sin( 2 * Math.PI * this.frequencyProperty.value * time ) + this.maximumVoltageProperty.value * Math.sin( 2 * Math.PI * this.frequencyProperty.value * time + this.phase ) ); } }