Skip to content

Commit

Permalink
Show apex as green dot, see #132
Browse files Browse the repository at this point in the history
  • Loading branch information
andrealin committed Aug 10, 2017
1 parent 9a67d72 commit 46be1cb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions js/common/model/Tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ define( function( require ) {
var currentTrajectory = this.trajectories.get( i );
var point = currentTrajectory.getNearestPoint( this.positionProperty.get().x, this.positionProperty.get().y );
var pointIsReadable = point &&
( point.position.y === 0 || Util.toFixedNumber( point.time * 1000, 0 ) % TIME_PER_SHOWN_DOT === 0 );
( point.apex || point.position.y === 0 || Util.toFixedNumber( point.time * 1000, 0 ) % TIME_PER_SHOWN_DOT === 0 );
if ( pointIsReadable && point.position.distance( this.positionProperty.get() ) <= SENSING_RADIUS ) {
this.dataPointProperty.set( point );
return;
Expand All @@ -88,7 +88,7 @@ define( function( require ) {

// point can be read by tracer if it exists, it is on the ground, or it is the right timestep
var pointIsReadable = point &&
( point.position.y === 0 || Util.toFixedNumber( point.time * 1000, 0 ) % TIME_PER_SHOWN_DOT === 0 );
( point.apex || point.position.y === 0 || Util.toFixedNumber( point.time * 1000, 0 ) % TIME_PER_SHOWN_DOT === 0 );
if ( pointIsReadable && point.position.distance( this.positionProperty.get() ) <= SENSING_RADIUS ) {
this.dataPointProperty.set( point );
}
Expand Down
30 changes: 30 additions & 0 deletions js/common/model/Trajectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ define( function( require ) {
var NumberProperty = require( 'AXON/NumberProperty' );
var ObservableArray = require( 'AXON/ObservableArray' );
var Vector2 = require( 'DOT/Vector2' );
var Util = require( 'DOT/Util' );

/**
* @param {ProjectileMotionModel} model
Expand Down Expand Up @@ -157,6 +158,35 @@ define( function( require ) {
var gravity = this.projectileMotionModel.gravityProperty.get();

var newDragForce = Vector2.dirtyFromPool().set( newVelocity ).multiplyScalar( 0.5 * airDensity * area * this.dragCoefficient * newVelocity.magnitude() );

if ( previousPoint.velocity.y >= 0 && newVelocity.y < 0 ) { // passed apex
var dtToApex = Util.linear( previousPoint.velocity.y, newVelocity.y, 0, dt, 0 );
var apexX = Util.linear( 0, dt, previousPoint.position.x, newX, dtToApex );
var apexY = Util.linear( 0, dt, previousPoint.position.y, newY, dtToApex );
var apexVelocityX = Util.linear( 0, dt, previousPoint.velocity.x, newVelocity.x, dtToApex );
var apexVelocityY = Util.linear( 0, dt, previousPoint.velocity.y, newVelocity.y, dtToApex );
var apexDragX = Util.linear( 0, dt, previousPoint.dragForce.x, newDragForce.x, dtToApex );
var apexDragY = Util.linear( 0, dt, previousPoint.dragForce.y, newDragForce.y, dtToApex );

var apexPoint = new DataPoint(
previousPoint.time + dtToApex,
Vector2.createFromPool( apexX, apexY ),
airDensity,
Vector2.createFromPool( apexVelocityX, apexVelocityY ), // velocity
Vector2.createFromPool( -apexDragX / this.mass, -gravity - apexDragY / this.mass ), // acceleration
Vector2.createFromPool( apexDragX, apexDragY ), // drag force
-gravity * this.mass
);

// add this special property to just the apex point collected for a trajectory
apexPoint.apex = true;

// push it
this.dataPoints.push( apexPoint );
this.projectileMotionModel.tracer.updateDataIfWithinRange( apexPoint );
this.projectileMotionModel.updateDavidIfWithinRange( apexPoint.position );

}

// Has reached ground or below
if ( newY <= 0 ) {
Expand Down
14 changes: 13 additions & 1 deletion js/common/view/TrajectoryNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ define( function( require ) {
var Shape = require( 'KITE/Shape' );
var Vector2 = require( 'DOT/Vector2' );
var Util = require( 'DOT/Util' );
var Circle = require( 'SCENERY/nodes/Circle' );

// constants
var MAX_TRAJECTORY_COUNT = ProjectileMotionConstants.MAX_NUMBER_OF_TRAJECTORIES;
Expand All @@ -35,6 +36,8 @@ define( function( require ) {
var DOTS_MAX_OPACITY = 0.5;
var TIME_PER_SHOWN_DOT = ProjectileMotionConstants.TIME_PER_SHOWN_DOT; // milliseconds

var DOT_GREEN = 'rgb( 50, 255, 50 )';

/**
* @param {VectorVisibilityProperties} vectorVisibilityProperties - Properties that determine which vectors are shown,
* only needed to pass down to ProjectileNode
Expand Down Expand Up @@ -64,7 +67,10 @@ define( function( require ) {

this.addChild( projectileObjectViewsLayer );
this.addChild( pathsLayer );
this.addChild( dotsPath );

var dotsLayer = new Node();
dotsLayer.addChild( dotsPath );
this.addChild( dotsLayer );
this.addChild( projectileNodesLayer );

var viewLastPosition = null;
Expand Down Expand Up @@ -94,6 +100,12 @@ define( function( require ) {
dotsShape.moveTo( viewAddedPosition.x + DOT_RADIUS, viewAddedPosition.y )
.circle( viewAddedPosition.x, viewAddedPosition.y, DOT_RADIUS );
}

// draw green dot if apex
if ( addedPoint.apex ) {
var apexDot = new Circle( DOT_RADIUS, { x: viewAddedPosition.x, y: viewAddedPosition.y, fill: DOT_GREEN, stroke: DOT_GREEN } );
dotsLayer.addChild( apexDot );
}
}

// view listens to whether a datapoint has been added in the model
Expand Down

0 comments on commit 46be1cb

Please sign in to comment.