Skip to content

Commit

Permalink
Added path to the state, see #335
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jun 10, 2020
1 parent a7d2e89 commit 1c920f7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
34 changes: 32 additions & 2 deletions js/common/model/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import Vector2 from '../../../../dot/js/Vector2.js';
import Vector2IO from '../../../../dot/js/Vector2IO.js';
import Vector2Property from '../../../../dot/js/Vector2Property.js';
import merge from '../../../../phet-core/js/merge.js';
import PhetioObject from '../../../../tandem/js/PhetioObject.js';
import ArrayIO from '../../../../tandem/js/types/ArrayIO.js';
import BooleanIO from '../../../../tandem/js/types/BooleanIO.js';
import NumberIO from '../../../../tandem/js/types/NumberIO.js';
import gravityAndOrbits from '../../gravityAndOrbits.js';
import gravityAndOrbitsStrings from '../../gravityAndOrbitsStrings.js';
import BodyIO from './BodyIO.js';
import BodyState from './BodyState.js';
import GravityAndOrbitsBodies from './GravityAndOrbitsBodies.js';
import RewindableProperty from './RewindableProperty.js';
Expand All @@ -36,7 +39,7 @@ const starString = gravityAndOrbitsStrings.star;
// reduce Vector2 allocation by reusing this Vector2 in collidesWith computation
const tempVector = new Vector2( 0, 0 );

class Body {
class Body extends PhetioObject {

/**
* @param {GravityAndOrbitsBodies} type - one of GravityAndOrbitsBodies, used for object identification
Expand All @@ -63,9 +66,13 @@ class Body {
orbitalCenter: new Vector2( 0, 0 ), // orbital center for the body
maxPathLength: 1400000000, // max path length for the body in km (should only be used if the body is too close to the center)
pathLengthLimit: 6000, // limit on the number of points in the path
rotationPeriod: null // period of body rotation, in seconds - null rotation period will prevent rotation
rotationPeriod: null, // period of body rotation, in seconds - null rotation period will prevent rotation
phetioType: BodyIO,
tandem: tandem
}, options );

super( options );

// Keep track of the time at the beginning of a time step, for interpolation
this.previousPosition = new Vector2( 0, 0 );

Expand Down Expand Up @@ -293,6 +300,29 @@ class Body {
return this.diameterProperty.get() / 2;
}

/**
* @public (phet-io)
*/
toStateObject() {
return {
pathLength: this.pathLength,
modelPathLength: this.modelPathLength,
path: ArrayIO( Vector2IO ).toStateObject( this.path )
};
}

/**
* @param stateObject
* @public (phet-io)
*/
setStateObject( stateObject ) {
this.pathLength = stateObject.pathLength;
this.modelPathLength = stateObject.modelPathLength;
this.path = ArrayIO( Vector2IO ).fromStateObject( stateObject.path );
this.clearedEmitter.emit( this.type );
this.path.forEach( pathPoint => this.pointAddedEmitter.emit( pathPoint, this.type ) );
}

/**
* Create an immutable representation of this body for use in the physics engine
* use copy() for Vector2 so that the properties don't get mutated
Expand Down
49 changes: 49 additions & 0 deletions js/common/model/BodyIO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020, University of Colorado Boulder

import ObjectIO from '../../../../tandem/js/types/ObjectIO.js';
import gravityAndOrbits from '../../gravityAndOrbits.js';
import Body from './Body.js';

/**
* IO type for Body
*
* @author Sam Reid (PhET Interactive Simulations)
*/
class BodyIO extends ObjectIO {

/**
* @param {Body} body
* @returns {Object}
* @public
*/
static toStateObject( body ) {
return body.toStateObject();
}

/**
* @param {Object} stateObject
* @returns {Object}
* @public
*/
static fromStateObject( stateObject ) {
return stateObject;
}

/**
* Used to set the value when loading a state
* @param {Body} body
* @param {Object} fromStateObject
* @public
*/
static setValue( body, fromStateObject ) {
body.setStateObject( fromStateObject );
}
}

BodyIO.documentation = 'Represents a physical body in the simulation';
BodyIO.validator = { isValidValue: b => b instanceof Body };
BodyIO.typeName = 'BodyIO';
ObjectIO.validateSubtype( BodyIO );

gravityAndOrbits.register( 'BodyIO', BodyIO );
export default BodyIO;

0 comments on commit 1c920f7

Please sign in to comment.