Skip to content

Commit

Permalink
set inherent (non-dragging) velocity to zero when dragging, also did …
Browse files Browse the repository at this point in the history
…some code cleanup, see #479
  • Loading branch information
jbphet committed Feb 9, 2021
1 parent 7a224d3 commit 306dc50
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 13 additions & 5 deletions js/balloons-and-static-electricity/model/BalloonModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class BalloonModel {
phetioReadOnly: true
} );

// @public {Vector2}
// @public {Vector2} - The velocity of the balloon when moving freely, i.e. NOT when it is being dragged.
// use new Vector2( 0, 0 ) instead of Vector2.ZERO so equality check won't be thwarted by ImmutableVector2
this.velocityProperty = new Vector2Property( new Vector2( 0, 0 ), {
tandem: tandem.createTandem( 'velocityProperty' ),
Expand Down Expand Up @@ -282,8 +282,8 @@ class BalloonModel {
this.positionProperty.get().y + this.height
);

// when position changes, update bounds of balloon in play area, direction of movement, and whether or not the
// the balloon is touching an object - no need to dispose as balloons exist for life of sim
// When the position changes, update the bounds of balloon, direction of movement, and whether or not the the
// balloon is touching an object. No need to dispose as balloons exist for life of sim.
this.positionProperty.link( ( position, oldPosition ) => {
this.bounds.setMinMax( position.x, position.y, position.x + this.width, position.y + this.height );

Expand All @@ -304,8 +304,16 @@ class BalloonModel {
}
} );

// when the balloon is released, reset the timer that indicates when balloon was released
this.isDraggedProperty.link( isDragged => {
this.isDraggedProperty.lazyLink( isDragged => {

// When the user starts dragging a balloon, set its non-dragging velocity to zero.
if ( isDragged ){

// Use new Vector2( 0, 0 ) instead of Vector2.ZERO so equality check won't be thwarted by ImmutableVector2.
this.velocityProperty.set( new Vector2( 0, 0 ) );
}

// When the balloon is released, reset the timer that indicates when it was released.
if ( !isDragged ) {
this.timeSinceRelease = 0;
}
Expand Down
8 changes: 6 additions & 2 deletions js/balloons-and-static-electricity/view/BalloonNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ class BalloonNode extends Node {
model.isDraggedProperty.link( updateAccessibleDescription );
globalModel.showChargesProperty.link( updateAccessibleDescription );

// @private - the drag handler needs to be updated in a step function, see KeyboardDragHandler for more
// information
// @private - the drag handler needs to be updated in a step function, see KeyboardDragHandler for more information
const boundaryUtterance = new Utterance();
this.keyboardDragHandler = new KeyboardDragListener( {
downDelta: 0,
Expand Down Expand Up @@ -320,6 +319,11 @@ class BalloonNode extends Node {
grabDragInteraction.reset();
} );

model.velocityProperty.link( velocity => {
console.log( 'velocity.magnitude = ' + velocity.magnitude );
} );

// Handle a query parameter that adds a line and a marker at the "charge center". This can be useful for debugging.
if ( BASEQueryParameters.showBalloonChargeCenter ) {
const parentToLocalChargeCenter = this.parentToLocalPoint( model.getChargeCenter() );
this.addChild( new Rectangle( 0, 0, 5, 5, { fill: 'green', center: parentToLocalChargeCenter } ) );
Expand Down

0 comments on commit 306dc50

Please sign in to comment.