Skip to content

Commit

Permalink
Remove MassIO state keys for canMove, massShape and tag, see #300
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Aug 6, 2024
1 parent 5003e0b commit 56e3ee3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 42 deletions.
55 changes: 20 additions & 35 deletions js/common/model/Mass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,26 @@ import Property from '../../../../axon/js/Property.js';
import Matrix3, { Matrix3StateObject } from '../../../../dot/js/Matrix3.js';
import Range from '../../../../dot/js/Range.js';
import Utils from '../../../../dot/js/Utils.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import Vector2, { Vector2StateObject } from '../../../../dot/js/Vector2.js';
import Vector2Property from '../../../../dot/js/Vector2Property.js';
import Vector3 from '../../../../dot/js/Vector3.js';
import { Shape } from '../../../../kite/js/imports.js';
import EnumerationIO from '../../../../tandem/js/types/EnumerationIO.js';
import optionize, { combineOptions } from '../../../../phet-core/js/optionize.js';
import { GatedVisibleProperty, PDOMValueType } from '../../../../scenery/js/imports.js';
import PhetioObject, { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import BooleanIO from '../../../../tandem/js/types/BooleanIO.js';
import IOType from '../../../../tandem/js/types/IOType.js';
import densityBuoyancyCommon from '../../densityBuoyancyCommon.js';
import InterpolatedProperty from './InterpolatedProperty.js';
import Material, { CustomSolidMaterial, MaterialOptions } from './Material.js';
import PhysicsEngine, { BodyStateObject, PhysicsEngineBody } from './PhysicsEngine.js';
import PhysicsEngine, { PhysicsEngineBody } from './PhysicsEngine.js';
import Basin from './Basin.js';
import TReadOnlyProperty from '../../../../axon/js/TReadOnlyProperty.js';
import Multilink from '../../../../axon/js/Multilink.js';
import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import { MassShape } from './MassShape.js';
import TEmitter from '../../../../axon/js/TEmitter.js';
import MassTag, { MassTagStateObject } from './MassTag.js';
import MassTag from './MassTag.js';
import Bounds3 from '../../../../dot/js/Bounds3.js';
import BlendedVector2Property from './BlendedVector2Property.js';
import { GuardedNumberProperty, GuardedNumberPropertyOptions } from './GuardedNumberProperty.js';
Expand Down Expand Up @@ -86,18 +84,16 @@ export type MassIOStateObject = {
matrix: Matrix3StateObject;
stepMatrix: Matrix3StateObject;
originalMatrix: Matrix3StateObject;
canMove: boolean;
tag: MassTagStateObject;
massShape: string;
} & BodyStateObject;
position: Vector2StateObject;
velocity: Vector2StateObject;
force: Vector2StateObject;
};

export default abstract class Mass extends PhetioObject {

protected readonly engine: PhysicsEngine;
public readonly body: PhysicsEngineBody;

private readonly massShape: MassShape;

// Without the matrix applied (effectively in "local" model coordinates)
public readonly shapeProperty: Property<Shape>;

Expand Down Expand Up @@ -159,8 +155,8 @@ export default abstract class Mass extends PhetioObject {
// Fired when this mass's input (drag) should be interrupted.
public readonly interruptedEmitter: TEmitter;

public canMove: boolean;
public tag: MassTag;
public readonly canMove: boolean;
public readonly tag: MassTag;

public readonly nameProperty: TReadOnlyProperty<string>;

Expand Down Expand Up @@ -206,7 +202,6 @@ export default abstract class Mass extends PhetioObject {

this.engine = engine;
this.body = options.body;
this.massShape = options.massShape;

this.shapeProperty = new Property( options.shape, {
valueType: Shape
Expand Down Expand Up @@ -614,9 +609,6 @@ export default abstract class Mass extends PhetioObject {
matrix: Matrix3.Matrix3IO,
stepMatrix: Matrix3.Matrix3IO,
originalMatrix: Matrix3.Matrix3IO,
canMove: BooleanIO,
tag: MassTag.MassTagIO,
massShape: EnumerationIO( MassShape ),
position: Vector2.Vector2IO,
velocity: Vector2.Vector2IO,
force: Vector2.Vector2IO
Expand All @@ -626,41 +618,34 @@ export default abstract class Mass extends PhetioObject {
matrix: Matrix3.toStateObject( mass.matrix ),
stepMatrix: Matrix3.toStateObject( mass.stepMatrix ),
originalMatrix: Matrix3.toStateObject( mass.originalMatrix ),
canMove: mass.canMove,
tag: MassTag.MassTagIO.toStateObject( mass.tag ),
massShape: EnumerationIO( MassShape ).toStateObject( mass.massShape ),

// Applies SIZE_SCALE
position: PhysicsEngine.p2ToVector( mass.body.position ).toStateObject(),
velocity: PhysicsEngine.p2ToVector( mass.body.velocity ).toStateObject(),
force: PhysicsEngine.p2ToVector( mass.body.force ).toStateObject() // we applied forces after the step
} );
},
applyState( mass: Mass, obj: MassIOStateObject ) {
applyState( mass: Mass, stateObject: MassIOStateObject ) {

// Some of the following attributes are not public, but are settable since this IOType is declared as a static
// class member. This is preferable to making the attributes public everywhere.
const SIZE_SCALE = DensityBuoyancyCommonQueryParameters.p2SizeScale;

mass.matrix.set( Matrix3.fromStateObject( obj.matrix ) );
mass.stepMatrix.set( Matrix3.fromStateObject( obj.stepMatrix ) );
mass.originalMatrix.set( Matrix3.fromStateObject( obj.originalMatrix ) );
mass.canMove = obj.canMove;
MassTag.MassTagIO.applyState( mass.tag, obj.tag );
mass.matrix.set( Matrix3.fromStateObject( stateObject.matrix ) );
mass.stepMatrix.set( Matrix3.fromStateObject( stateObject.stepMatrix ) );
mass.originalMatrix.set( Matrix3.fromStateObject( stateObject.originalMatrix ) );

// We will ignore infinities
mass.body.position[ 0 ] = obj.position.x * SIZE_SCALE;
mass.body.position[ 1 ] = obj.position.y * SIZE_SCALE;
mass.body.position[ 0 ] = stateObject.position.x * SIZE_SCALE;
mass.body.position[ 1 ] = stateObject.position.y * SIZE_SCALE;

// TODO: Are the next 2 lines synchronizePreviousPosition? See https://github.com/phetsims/density-buoyancy-common/issues/300
mass.body.previousPosition[ 0 ] = obj.position.x * SIZE_SCALE;
mass.body.previousPosition[ 1 ] = obj.position.y * SIZE_SCALE;
mass.body.velocity[ 0 ] = stateObject.velocity.x * SIZE_SCALE;
mass.body.velocity[ 1 ] = stateObject.velocity.y * SIZE_SCALE;

mass.body.velocity[ 0 ] = obj.velocity.x * SIZE_SCALE;
mass.body.velocity[ 1 ] = obj.velocity.y * SIZE_SCALE;
mass.body.force[ 0 ] = stateObject.force.x * SIZE_SCALE;
mass.body.force[ 1 ] = stateObject.force.y * SIZE_SCALE;

mass.body.force[ 0 ] = obj.force.x * SIZE_SCALE;
mass.body.force[ 1 ] = obj.force.y * SIZE_SCALE;
PhysicsEngine.bodySynchronizePrevious( mass.body );

mass.writeData();
}
Expand Down
8 changes: 1 addition & 7 deletions js/common/model/PhysicsEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import TEmitter from '../../../../axon/js/TEmitter.js';
import TinyEmitter from '../../../../axon/js/TinyEmitter.js';
import Matrix3 from '../../../../dot/js/Matrix3.js';
import Vector2, { Vector2StateObject } from '../../../../dot/js/Vector2.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import densityBuoyancyCommon from '../../densityBuoyancyCommon.js';
import DensityBuoyancyCommonQueryParameters from '../DensityBuoyancyCommonQueryParameters.js';

Expand All @@ -43,12 +43,6 @@ const BODY_TYPE_MAPPER = {
STATIC: p2.Body.STATIC // Cannot move, for anything.
};

export type BodyStateObject = {
position: Vector2StateObject;
velocity: Vector2StateObject;
force: Vector2StateObject;
};

export default class PhysicsEngine {

// Engines typically work in fixed-time steps, this is how far we are in the
Expand Down

0 comments on commit 56e3ee3

Please sign in to comment.