Skip to content

Commit

Permalink
Added phase matching so the chart doesn't get jagged
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Feb 27, 2019
1 parent 8140190 commit ab160ee
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion js/model/ACSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
} );
}

/**
Expand Down Expand Up @@ -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 )
);
}
}
Expand Down

0 comments on commit ab160ee

Please sign in to comment.