Skip to content

Commit

Permalink
add LightSpot.intersectsProjectionScreenProperty, use it to set visib…
Browse files Browse the repository at this point in the history
…ility of LightSpotNode and therefore filter out tool jump points, #426
  • Loading branch information
pixelzoom committed Apr 13, 2022
1 parent c3fa96c commit 8569d01
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
17 changes: 15 additions & 2 deletions js/common/model/LightSpot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import PhetioObject, { PhetioObjectOptions } from '../../../../tandem/js/PhetioO
import optionize from '../../../../phet-core/js/optionize.js';
import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import PickOptional from '../../../../phet-core/js/types/PickOptional.js';
import BooleanIO from '../../../../tandem/js/types/BooleanIO.js';

type PositionAndDiameter = {
position: Vector2; // position of the light spot's center, in the vertical plane of the projection screen
Expand All @@ -40,12 +41,15 @@ export default class LightSpot extends PhetioObject {
// Intensity of the light spot, in the range [0,1], 0 if there is no light spot hitting the projection screen
public readonly intensityProperty: IReadOnlyProperty<number>;

// Position of the center of the light spot, which may not be on the screen,
// Position of the center of the light spot, which may not be on the screen
public readonly positionProperty: IReadOnlyProperty<Vector2>;

// Diameter of the light spot in the y dimension,
// Diameter of the light spot in the y dimension
public readonly diameterProperty: IReadOnlyProperty<number>;

// Whether the light spot intersects the projection screen
public readonly intersectsProjectionScreenProperty: IReadOnlyProperty<boolean>;

constructor( optic: Optic,
projectionScreen: ProjectionScreen,
lightObjectPositionProperty: IReadOnlyProperty<Vector2>,
Expand Down Expand Up @@ -107,6 +111,15 @@ export default class LightSpot extends PhetioObject {
phetioType: DerivedProperty.DerivedPropertyIO( NullableIO( NumberIO ) ),
phetioDocumentation: 'intensity of the light spot, in the range [0,1]'
} );

this.intersectsProjectionScreenProperty = new DerivedProperty(
[ this.positionProperty, this.diameterProperty, projectionScreen.positionProperty ],
( position: Vector2, diameter: number, projectionScreenPosition: Vector2 ) =>
position.y >= projectionScreenPosition.y - projectionScreen.height / 2 - diameter / 2 &&
position.y <= projectionScreenPosition.y + projectionScreen.height / 2 + diameter / 2, {
tandem: options.tandem.createTandem( 'intersectsProjectionScreenProperty' ),
phetioType: DerivedProperty.DerivedPropertyIO( BooleanIO )
} );
}

public override dispose(): void {
Expand Down
11 changes: 8 additions & 3 deletions js/common/model/ProjectionScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default class ProjectionScreen extends PhetioObject {
// Shape of the screen, relative to positionProperty
public readonly screenShape: Shape;

// Height of the screen at its centerX.
public readonly height: number;

// line that vertically bisects the screen, relative to positionProperty
private readonly bisectorLine: Shape;

Expand Down Expand Up @@ -69,11 +72,13 @@ export default class ProjectionScreen extends PhetioObject {
.lineTo( -SCREEN_WIDTH / 2, -SCREEN_FAR_HEIGHT / 2 )
.close();

// Height at the centerX of the screen, the average of its near and far perspective heights
this.height = ( SCREEN_NEAR_HEIGHT + SCREEN_FAR_HEIGHT ) / 2;

// Described from top to bottom, in model coordinates.
const averageScreenHeight = ( SCREEN_NEAR_HEIGHT + SCREEN_FAR_HEIGHT ) / 2;
this.bisectorLine = new Shape()
.moveTo( 0, averageScreenHeight / 2 )
.lineTo( 0, -averageScreenHeight / 2 );
.moveTo( 0, this.height / 2 )
.lineTo( 0, -this.height / 2 );

this.resetProjectionScreen = () => {
this.positionProperty.reset();
Expand Down
20 changes: 14 additions & 6 deletions js/common/view/LightSceneNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ export default class LightSceneNode extends GOSceneNode {
// LightSpot associated with the first light
const lightSpot1NodeTandem = providedOptions.tandem.createTandem( 'lightSpot1Node' );
const lightSpot1Node = new LightSpotNode( scene.lightSpot1, scene.projectionScreen, modelViewTransform, {
visibleProperty: DerivedProperty.and( [ lightPropagationEnabledProperty, scene.opticalImage1.visibleProperty ], {
visibleProperty: DerivedProperty.and( [
scene.lightSpot1.intersectsProjectionScreenProperty,
lightPropagationEnabledProperty,
scene.opticalImage1.visibleProperty
], {
tandem: lightSpot1NodeTandem.createTandem( 'visibleProperty' ),
phetioType: DerivedProperty.DerivedPropertyIO( BooleanIO )
} ),
Expand All @@ -146,11 +150,15 @@ export default class LightSceneNode extends GOSceneNode {
// LightSpot associated with the second light
const lightSpot2NodeTandem = providedOptions.tandem.createTandem( 'lightSpot2Node' );
const lightSpot2Node = new LightSpotNode( scene.lightSpot2, scene.projectionScreen, modelViewTransform, {
visibleProperty: DerivedProperty.and(
[ lightPropagationEnabledProperty, scene.opticalImage2.visibleProperty, visibleProperties.secondPointVisibleProperty ], {
tandem: lightSpot2NodeTandem.createTandem( 'visibleProperty' ),
phetioType: DerivedProperty.DerivedPropertyIO( BooleanIO )
} ),
visibleProperty: DerivedProperty.and( [
scene.lightSpot2.intersectsProjectionScreenProperty,
lightPropagationEnabledProperty,
scene.opticalImage2.visibleProperty,
visibleProperties.secondPointVisibleProperty
], {
tandem: lightSpot2NodeTandem.createTandem( 'visibleProperty' ),
phetioType: DerivedProperty.DerivedPropertyIO( BooleanIO )
} ),
tandem: lightSpot2NodeTandem
} );
this.opticalImagesLayer.addChild( lightSpot2Node );
Expand Down

0 comments on commit 8569d01

Please sign in to comment.