Skip to content

Commit

Permalink
#139 add min velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonLi8 committed Aug 4, 2020
1 parent 2d87aaa commit 6cdd1dd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
11 changes: 9 additions & 2 deletions js/common/model/Ball.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import BooleanProperty from '../../../../axon/js/BooleanProperty.js';
import DerivedProperty from '../../../../axon/js/DerivedProperty.js';
import NumberProperty from '../../../../axon/js/NumberProperty.js';
import Utils from '../../../../dot/js/Utils.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import AssertUtils from '../../../../phetcommon/js/AssertUtils.js';
import collisionLab from '../../collisionLab.js';
Expand Down Expand Up @@ -370,15 +371,21 @@ class Ball {
*
* @param {number} xVelocity - in m/s.
*/
set xVelocity( xVelocity ) { this.xVelocityProperty.value = xVelocity; }
set xVelocity( xVelocity ) {
if ( Utils.equalsEpsilon( xVelocity, 0, 1E-6 ) ) { xVelocity = 0; }
this.xVelocityProperty.value = xVelocity;
}

/**
* Sets the vertical velocity of the Ball, in m/s.
* @public
*
* @param {number} yVelocity - in m/s.
*/
set yVelocity( yVelocity ) { this.yVelocityProperty.value = yVelocity; }
set yVelocity( yVelocity ) {
if ( Utils.equalsEpsilon( yVelocity, 0, 1E-6 ) ) { yVelocity = 0; }
this.yVelocityProperty.value = yVelocity;
}
}

collisionLab.register( 'Ball', Ball );
Expand Down
21 changes: 16 additions & 5 deletions js/common/model/CollisionEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,18 @@ class CollisionEngine {
* @param {number} dt - time-delta in seconds
* @param {number} elapsedTime - the total elapsed elapsedTime of the simulation, in seconds.
*/
step( dt, elapsedTime ) {
async step( dt, elapsedTime ) {
assert && assert( typeof dt === 'number', `invalid dt: ${dt}` );
assert && assert( typeof elapsedTime === 'number' && elapsedTime >= 0, `invalid elapsedTime: ${elapsedTime}` );

// Reset potential collisions that were detected in the previous step call.
this.potentialCollisions = [];

// await CollisionLabUtils.sleep( 1 );
// First detect all potential collisions that occur within this time-step at once.
this.detectBallToBallCollisions( dt );
this.detectBallToBorderCollisions( dt );

// console.log( 'here', this.potentialCollisions)
if ( !this.potentialCollisions.length ) {

// If there are no collisions detected within the given time-step, the Balls are in uniform motion for the rest of
Expand All @@ -104,20 +105,25 @@ class CollisionEngine {
const nextCollisions = dt >= 0 ?
CollisionLabUtils.getMinValuesOf( this.potentialCollisions, _.property( 'collisionTime' ) ) :
CollisionLabUtils.getMaxValuesOf( this.potentialCollisions, _.property( 'collisionTime' ) );

// console.log( 'next', nextCollisions )
// Compute the total elapsed-time of the simulation when next detected collision will occurred.
// TODO: potential naming confusions here. CollisionTime is used in IntroBallSystem to reference elapsed time, but CollisionTime is used in Collision to reference delta-time.
const elapsedTimeOfCollision = elapsedTime - dt + nextCollisions[ 0 ].collisionTime;

// Progress forwards to the exact point of contact of the nextCollision.
this.ballSystem.stepUniformMotion( nextCollisions[ 0 ].collisionTime, elapsedTimeOfCollision );
// await CollisionLabUtils.sleep( 1 );

for ( let i = 0; i < nextCollisions.length; i++ ) {
const collision = nextCollisions[ i ];
// console.log( 'handle', collision)
// await CollisionLabUtils.sleep( 1 );

nextCollisions.forEach( collision => {
// Handle the response for the Ball Collision depending on the type of collision.
collision.collidingObject instanceof Ball ?
this.handleBallToBallCollision( collision.ball, collision.collidingObject, elapsedTimeOfCollision ) :
this.handleBallToBorderCollision( collision.ball, dt );
} );
}

// Recursively call step() with a smaller step-size to re-detect all potential collisions afterwards.
return this.step( dt - nextCollisions[ 0 ].collisionTime, elapsedTime ); // return for TCO supported browsers.
Expand Down Expand Up @@ -167,6 +173,7 @@ class CollisionEngine {

// The minimum root of the quadratic is when the Balls will first collide.
const collisionTime = possibleRoots ? Math.min( ...possibleRoots ) : null;
// console.log( 'trying, ', ball1.index, ball2.index, deltaR.magnitudeSquared - sumOfRadiiSquared, deltaV, possibleRoots, Number.isFinite( collisionTime ) && collisionTime >= 0 && collisionTime <= Math.abs( dt ) )

// If the quadratic root is finite and the collisionTime is within the current time-step period, the collision
// is detected and should be registered.
Expand All @@ -175,6 +182,10 @@ class CollisionEngine {
// Register the collision and encapsulate information in a Collision instance.
this.potentialCollisions.push( new Collision( ball1, ball2, collisionTime * Math.sign( dt ) ) );
}
if ( Utils.equalsEpsilon( deltaR.magnitudeSquared - sumOfRadiiSquared, 0, 1E-13 ) && !deltaV.equals( Vector2.ZERO ) ) {
// console.log( 'manuel detection')
this.potentialCollisions.push( new Collision( ball1, ball2, 0 ) );
}
} );
}

Expand Down
13 changes: 10 additions & 3 deletions js/explore1D/model/Explore1DBallSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ import BallSystem from '../../common/model/BallSystem.js';
import Explore1DPlayArea from './Explore1DPlayArea.js';

// constants
// const EXPLORE_1D_INITIAL_BALL_STATES = [
// new BallState( new Vector2( -1.0, 0 ), new Vector2( 1.00, 0 ), 0.5 ),
// new BallState( new Vector2( 0.00, 0 ), new Vector2( -0.5, 0 ), 1.5 ),
// new BallState( new Vector2( 1.00, 0 ), new Vector2( -0.5, 0 ), 1.0 ),
// new BallState( new Vector2( 1.50, 0 ), new Vector2( 1.10, 0 ), 1.0 )
// ];
const EXPLORE_1D_INITIAL_BALL_STATES = [
new BallState( new Vector2( -1.0, 0 ), new Vector2( 1.00, 0 ), 0.5 ),
new BallState( new Vector2( 0.00, 0 ), new Vector2( -0.5, 0 ), 1.5 ),
new BallState( new Vector2( 1.00, 0 ), new Vector2( -0.5, 0 ), 1.0 ),
new BallState( new Vector2( -0.46, 0 ), new Vector2( -0.13, 0 ), 0.5 ),
new BallState( new Vector2( -0.09, 0 ), new Vector2( -0.13, 0 ), 1.5 ),
new BallState( new Vector2( 1.00, 0 ), new Vector2( -2.25, 0 ), 1.0 ),
new BallState( new Vector2( 1.50, 0 ), new Vector2( 1.10, 0 ), 1.0 )
];


class Explore1DBallSystem extends BallSystem {

/**
Expand Down

0 comments on commit 6cdd1dd

Please sign in to comment.