Skip to content

Commit

Permalink
calculate the density range based on the provided cube data, #268
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Kauzmann <[email protected]>
  • Loading branch information
zepumph committed Jul 25, 2024
1 parent ff65ccb commit 90e65c2
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions js/common/model/CompareBlockSetModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import PhysicsEngine from './PhysicsEngine.js';
import densityBuoyancyCommon from '../../densityBuoyancyCommon.js';
import { MaterialSchema } from './Mass.js';

// This hard coded range is a bit arbitrary, but it lends itself to better colors than the provided range in the options.
const COLOR_DENSITY_RANGE = new Range( 10, 10000 );

assert && assert( BlockSet.enumeration.values.length === 3, 'This class is very hard coded for the three "SAME" values of BlockSet' );

// Public API for specifying a cube in the BlockSet. A cube will exist in all BlockSet values.
Expand Down Expand Up @@ -85,6 +82,7 @@ export default class CompareBlockSetModel extends BlockSetModel<BlockSet> {
public constructor( providedOptions: CompareBlockSetModelOptions ) {

const options = optionize<CompareBlockSetModelOptions, SelfOptions, OptionizeParent>()( {
// TODO: use RangeWithValue? https://github.com/phetsims/density-buoyancy-common/issues/123
sameMassValue: 5,
sameMassRange: new Range( 1, 10 ),
sameVolumeValue: 0.005,
Expand Down Expand Up @@ -132,7 +130,36 @@ export default class CompareBlockSetModel extends BlockSetModel<BlockSet> {
units: 'kg/m^3'
} );

const getCubeOptions = ( cubeOptions: StrictCubeOptionsNoAvailableMaterials ) => combineOptions<StrictCubeOptionsNoAvailableMaterials>( {}, options.sharedCubeOptions, cubeOptions );

// Calculate the total possible density values for all blocks on the screen. This keeps the density range the same
// for all block sets.
const fullDensityRange = options.sameDensityRange.copy();

const updateRange = ( density: number ) => {
if ( density < fullDensityRange.min ) {
fullDensityRange.min = density;
}
if ( density > fullDensityRange.max ) {
fullDensityRange.max = density;
}
};

options.cubesData.forEach( cubeData => {
updateRange( options.sameMassRange.min / cubeData.sameMassVolume );
updateRange( options.sameMassRange.max / cubeData.sameMassVolume );
updateRange( cubeData.sameVolumeMass / options.sameVolumeRange.min );
updateRange( cubeData.sameVolumeMass / options.sameVolumeRange.max );
} );

const getCubeOptions = ( cubeOptions: StrictCubeOptionsNoAvailableMaterials ) => {
return combineOptions<StrictCubeOptionsNoAvailableMaterials>( {
customMaterialOptions: {
densityPropertyOptions: {
range: fullDensityRange
}
}
}, options.sharedCubeOptions, cubeOptions );
};

// Create one mass for each cubeData/blockSet combo, based on the provided blockSet
const createMasses = ( model: BlockSetModel<BlockSet>, blockSet: BlockSet ) => {
Expand Down Expand Up @@ -280,11 +307,10 @@ export default class CompareBlockSetModel extends BlockSetModel<BlockSet> {

const cube = Cube.createWithMass( engine, initialMaterial, position, mass, options );

Multilink.multilink( [ baseColorProperty, cube.materialProperty.densityProperty ], ( color, density ) => {
Multilink.multilink( [ baseColorProperty, cube.materialProperty.densityProperty, cube.materialProperty.customMaterial.densityProperty.rangeProperty ], ( color, density, range ) => {

// Calculate the lightness of the material based on its density, but keep a consistent range independent of the available ranges for all usages.
// TODO: Why not just use the lightness based on the actual densityProperty.range here? https://github.com/phetsims/density-buoyancy-common/issues/268
const lightness = Material.getNormalizedLightness( density, COLOR_DENSITY_RANGE ); // 0-1
const lightness = Material.getNormalizedLightness( density, range ); // 0-1

// Modify the color brightness based on the lightness.
const modifier = 0.1;
Expand Down

0 comments on commit 90e65c2

Please sign in to comment.