Skip to content

Commit

Permalink
Move PoolScaleHeightProperty into PoolScale, see #148
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jun 4, 2024
1 parent 07a2323 commit 322fd8d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
15 changes: 3 additions & 12 deletions js/common/model/DensityBuoyancyModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import TModel from '../../../../joist/js/TModel.js';
import { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js';
import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import PoolScale from './PoolScale.js';
import PoolScaleHeightProperty from './PoolScaleHeightProperty.js';
import DensityBuoyancyCommonConstants from '../DensityBuoyancyCommonConstants.js';

// constants
const BLOCK_SPACING = 0.01;
Expand Down Expand Up @@ -102,8 +100,7 @@ export default class DensityBuoyancyModel implements TModel {
private spillingWaterOutOfBoat = false;

// Scale for the pool and its heightProperty, if we are using it
public readonly poolScale: Scale | null = null;
public readonly poolScaleHeightProperty: NumberProperty;
public readonly poolScale: PoolScale | null = null;

public constructor( providedOptions?: DensityBuoyancyModelOptions ) {
const options = optionize<DensityBuoyancyModelOptions, DensityBuoyancyModelOptions>()( {
Expand Down Expand Up @@ -413,15 +410,9 @@ export default class DensityBuoyancyModel implements TModel {
} );
} );

// TODO: https://github.com/phetsims/density-buoyancy-common/issues/148 maybe create the pool scale height property in PoolScale?
const poolScaleTandem = tandem.createTandem( 'poolScale' );
this.poolScaleHeightProperty = new PoolScaleHeightProperty( DensityBuoyancyCommonConstants.POOL_SCALE_INITIAL_HEIGHT, {
range: new Range( 0, 1 ),
tandem: options.usePoolScale ? poolScaleTandem.createTandem( 'heightProperty' ) : Tandem.OPT_OUT
} );
if ( options.usePoolScale ) {

this.poolScale = new PoolScale( this.engine, this.gravityProperty, poolScaleTandem );
this.poolScale = new PoolScale( this.engine, this.gravityProperty, tandem.createTandem( 'poolScale' ) );

// Make sure to render it
this.availableMasses.push( this.poolScale );
Expand Down Expand Up @@ -567,7 +558,7 @@ export default class DensityBuoyancyModel implements TModel {
this.pool.reset();
this.masses.forEach( mass => mass.reset() );

this.poolScaleHeightProperty && this.poolScaleHeightProperty.reset();
this.poolScale && this.poolScale.reset();
}

/**
Expand Down
17 changes: 17 additions & 0 deletions js/common/model/PoolScale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ import Scale, { DisplayType, SCALE_HEIGHT, SCALE_WIDTH } from './Scale.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import { Shape } from '../../../../kite/js/imports.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import PoolScaleHeightProperty from './PoolScaleHeightProperty.js';
import DensityBuoyancyCommonConstants from '../DensityBuoyancyCommonConstants.js';
import Range from '../../../../dot/js/Range.js';

// To prevent objects from being dragged beneath the scale, we extend the invisible part of the scale vertically downward.
const SCALE_INVISIBLE_VERTICAL_EXTENSION_FACTOR = 8.26;

export default class PoolScale extends Scale {

// Unitless value between 0 and 1 that represents how high the scale is above the bottom of the pool.
// See PoolScaleHeightControl for the mapping to model coordinates.
public readonly heightProperty: PoolScaleHeightProperty;

public constructor( engine: PhysicsEngine, gravityProperty: TProperty<Gravity>, tandem: Tandem ) {

const vertices = [
Expand All @@ -39,6 +46,16 @@ export default class PoolScale extends Scale {
},
tandem: tandem
} );

this.heightProperty = new PoolScaleHeightProperty( DensityBuoyancyCommonConstants.POOL_SCALE_INITIAL_HEIGHT, {
range: new Range( 0, 1 ),
tandem: tandem.createTandem( 'heightProperty' )
} );
}

public override reset(): void {
super.reset();
this.heightProperty.reset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion js/common/view/DensityBuoyancyScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ export default class DensityBuoyancyScreenView<Model extends DensityBuoyancyMode
} );

if ( model.poolScale ) {
this.poolScaleHeightControl = new PoolScaleHeightControl( model.poolScale, model.poolScaleHeightProperty,
this.poolScaleHeightControl = new PoolScaleHeightControl( model.poolScale,
model.poolBounds, model.pool.liquidYInterpolatedProperty, this, {
tandem: options.tandem.createTandem( 'poolScaleHeightControl' )
} );
Expand Down
22 changes: 9 additions & 13 deletions js/common/view/PoolScaleHeightControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import densityBuoyancyCommon from '../../densityBuoyancyCommon.js';
import Slider from '../../../../sun/js/Slider.js';
import Property from '../../../../axon/js/Property.js';
import optionize, { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
import Range from '../../../../dot/js/Range.js';
import Tandem from '../../../../tandem/js/Tandem.js';
Expand All @@ -28,6 +27,7 @@ import ArrowButton from '../../../../sun/js/buttons/ArrowButton.js';
import Orientation from '../../../../phet-core/js/Orientation.js';
import WithRequired from '../../../../phet-core/js/types/WithRequired.js';
import { Shape } from '../../../../kite/js/imports.js';
import PoolScale from '../model/PoolScale.js';

// constants
const DEFAULT_RANGE = new Range( 0, 1 );
Expand All @@ -37,7 +37,7 @@ type ScaleHeightSliderOptions = EmptySelfOptions & WithRequired<NumberControlOpt

export default class PoolScaleHeightControl extends NumberControl {

public constructor( scale: Scale, heightProperty: Property<number>,
public constructor( poolScale: PoolScale,
poolBounds: Bounds3,
liquidYInterpolatedProperty: TReadOnlyProperty<number>,
modelViewTransform: THREEModelViewTransform,
Expand All @@ -55,12 +55,8 @@ export default class PoolScaleHeightControl extends NumberControl {
mouseArea: thumbInteractionArea
} );

// This magic number accomplishes two things:
// 1. matching the liquid level exactly causes blue graphical fractals on the top of the scale
// 2. Because the P2 engine has a non-infinite stiffness, masses overlap when contacting. This means that we want to
// raise the scale up above the pool so that the block can be the same weight as when measuring with the ground scale.
const maxY = liquidYInterpolatedProperty.value;
const minY = poolBounds.minY;
const maxY = liquidYInterpolatedProperty.value;

const sliderTrackHeight = modelViewTransform.modelToViewDelta( new Vector3( SCALE_X_POSITION, maxY, poolBounds.maxZ ), new Vector3( SCALE_X_POSITION, minY, poolBounds.maxZ ) ).y;

Expand Down Expand Up @@ -94,15 +90,15 @@ export default class PoolScaleHeightControl extends NumberControl {
}
}, providedOptions );

super( new Property( '' ), heightProperty, DEFAULT_RANGE, options );
super( '', poolScale.heightProperty, DEFAULT_RANGE, options );

heightProperty.link( height => {
poolScale.heightProperty.link( height => {
const currentHeight = Utils.linear( 0, 1, minY, maxY, height );

scale.matrix.set02( SCALE_X_POSITION );
scale.matrix.set12( currentHeight + Scale.SCALE_HEIGHT / 2 );
scale.writeData();
scale.transformedEmitter.emit();
poolScale.matrix.set02( SCALE_X_POSITION );
poolScale.matrix.set12( currentHeight + Scale.SCALE_HEIGHT / 2 );
poolScale.writeData();
poolScale.transformedEmitter.emit();
} );

thumbNode.touchArea = thumbInteractionArea.copy().rect( -10, -10, 20, 50 );
Expand Down

0 comments on commit 322fd8d

Please sign in to comment.