Skip to content

Commit

Permalink
reduced allocations for drag vectors, see #191
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Jan 29, 2019
1 parent f0851ed commit c6deed6
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions js/common/model/EnergyChunkDistributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ define( function( require ) {
updateVelocitiesNew: function( slices, energyChunks, energyChunkForces, dt ) {

var maxEnergy = 0;
var dragForce = Vector2.dirtyFromPool();

for ( var sliceIndex = 0; sliceIndex < slices.length; sliceIndex++ ) {
for ( var ecIndex = 0; ecIndex < slices[ sliceIndex ].energyChunkList.length; ecIndex++ ) {
Expand All @@ -480,14 +481,13 @@ define( function( require ) {
// calculate drag force using standard drag equation
var velocityMagnitudeSquared = velocity.magnitudeSquared();
var dragMagnitude = 0.5 * FLUID_DENSITY * DRAG_COEFFICIENT * ENERGY_CHUNK_CROSS_SECTIONAL_AREA * velocityMagnitudeSquared;
var dragForce = ZERO_VECTOR;
if ( dragMagnitude > 0 ) {
dragForce = Vector2.createFromPool(
-velocity.x,
-velocity.y
);
dragForce.setXY( -velocity.x, -velocity.y );
dragForce.setMagnitude( dragMagnitude );
}
else {
dragForce.setXY( 0, 0 );
}
assert && assert( !_.isNaN( dragForce.x ) && !_.isNaN( dragForce.y ), 'drag force contains NaN value' );

// update velocity based on the sum of forces acting on the particle
Expand All @@ -499,14 +499,12 @@ define( function( require ) {
if ( totalParticleEnergy > maxEnergy ) {
maxEnergy = totalParticleEnergy;
}

// free allocations
if ( dragForce !== ZERO_VECTOR ) {
dragForce.freeToPool();
}
}
}

// free allocations
dragForce.freeToPool();

return maxEnergy;
},

Expand Down

0 comments on commit c6deed6

Please sign in to comment.