Skip to content

Commit

Permalink
Move FlxU.computeVelocity() to FlxMath
Browse files Browse the repository at this point in the history
Also includes some slight canges to the arguments (instead of
automatically retrieving the time elapsed from FlxG, users are required
to include that information as an argument.)

Also updated any references that were still using the old version.

    FlxU.computeVelocity() -> FlxMath.computeVelocity()
  • Loading branch information
IQAndreas committed May 13, 2014
1 parent 0aef45f commit 01aff09
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
7 changes: 3 additions & 4 deletions src/flixel/FlxObject.as
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package flixel
import flash.display.Graphics;

import flixel.tile.FlxTilemap;
import flixel.util.FlxU;
import flixel.util.FlxMath;
import flixel.util.FlxPath;
import flixel.util.FlxRect;
Expand Down Expand Up @@ -384,18 +383,18 @@ package flixel
var delta:Number;
var velocityDelta:Number;

velocityDelta = (FlxU.computeVelocity(angularVelocity,angularAcceleration,angularDrag,maxAngular) - angularVelocity)/2;
velocityDelta = (FlxMath.computeVelocity(FlxG.elapsed, angularVelocity,angularAcceleration,angularDrag,maxAngular) - angularVelocity)/2;
angularVelocity += velocityDelta;
angle += angularVelocity*FlxG.elapsed;
angularVelocity += velocityDelta;

velocityDelta = (FlxU.computeVelocity(velocity.x,acceleration.x,drag.x,maxVelocity.x) - velocity.x)/2;
velocityDelta = (FlxMath.computeVelocity(FlxG.elapsed, velocity.x,acceleration.x,drag.x,maxVelocity.x) - velocity.x)/2;
velocity.x += velocityDelta;
delta = velocity.x*FlxG.elapsed;
velocity.x += velocityDelta;
x += delta;

velocityDelta = (FlxU.computeVelocity(velocity.y,acceleration.y,drag.y,maxVelocity.y) - velocity.y)/2;
velocityDelta = (FlxMath.computeVelocity(FlxG.elapsed, velocity.y,acceleration.y,drag.y,maxVelocity.y) - velocity.y)/2;
velocity.y += velocityDelta;
delta = velocity.y*FlxG.elapsed;
velocity.y += velocityDelta;
Expand Down
34 changes: 34 additions & 0 deletions src/flixel/util/FlxMath.as
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,39 @@ package flixel.util
var lowerBound:Number = (Value<Min)?Min:Value;
return (lowerBound>Max)?Max:lowerBound;
}


/**
* A tween-like function that takes a starting velocity
* and some other factors and returns an altered velocity.
*
* @param TimeElapsed The amount of time in seconds that passed since last frame.
* @param Velocity Any component of velocity (e.g. 20).
* @param Acceleration Rate at which the velocity is changing.
* @param Drag Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
* @param Max An optional absolute value cap for the velocity.
*
* @return The altered Velocity value.
*/
static public function computeVelocity(TimeElapsed:Number, Velocity:Number, Acceleration:Number=0, Drag:Number=0, Max:Number=NaN):Number
{
if(Acceleration != 0)
Velocity += Acceleration*TimeElapsed;
else if(Drag != 0)
{
var drag:Number = Drag*TimeElapsed;
if(Velocity - drag > 0)
Velocity = Velocity - drag;
else if(Velocity + drag < 0)
Velocity += drag;
else
Velocity = 0;
}
if((Velocity != 0) && (!isNaN(Max)))
{
Velocity = FlxMath.clamp(Velocity, -Max, Max);
}
return Velocity;
}
}
}
57 changes: 22 additions & 35 deletions src/flixel/util/FlxU.as
Original file line number Diff line number Diff line change
Expand Up @@ -300,41 +300,6 @@ package flixel.util
return getDefinitionByName(Name) as Class;
}

/**
* A tween-like function that takes a starting velocity
* and some other factors and returns an altered velocity.
*
* @param Velocity Any component of velocity (e.g. 20).
* @param Acceleration Rate at which the velocity is changing.
* @param Drag Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
* @param Max An absolute value cap for the velocity.
*
* @return The altered Velocity value.
*/
static public function computeVelocity(Velocity:Number, Acceleration:Number=0, Drag:Number=0, Max:Number=10000):Number
{
if(Acceleration != 0)
Velocity += Acceleration*FlxG.elapsed;
else if(Drag != 0)
{
var drag:Number = Drag*FlxG.elapsed;
if(Velocity - drag > 0)
Velocity = Velocity - drag;
else if(Velocity + drag < 0)
Velocity += drag;
else
Velocity = 0;
}
if((Velocity != 0) && (Max != 10000))
{
if(Velocity > Max)
Velocity = Max;
else if(Velocity < -Max)
Velocity = -Max;
}
return Velocity;
}


/* --- Deprecated members in Flixel v2.57 --- */
/* To be removed after developers have had time to adjust to the new changes. */
Expand Down Expand Up @@ -576,6 +541,28 @@ package flixel.util
return FlxMath.clamp(Value, Min, Max);
}


/**
* A tween-like function that takes a starting velocity
* and some other factors and returns an altered velocity.
*
* @deprecated This property is deprecated. Use <code>FlxMath.clamp()</code> instead.
* Note the difference in arguments!
*
* @param Velocity Any component of velocity (e.g. 20).
* @param Acceleration Rate at which the velocity is changing.
* @param Drag Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
* @param Max An absolute value cap for the velocity.
*
* @return The altered Velocity value.
*/
static public function computeVelocity(Velocity:Number, Acceleration:Number=0, Drag:Number=0, Max:Number=NaN):Number
{
FlxG.warnDeprecated('FlxU.computeVelocity()', 'FlxMath.computeVelocity()');
return FlxMath.computeVelocity(FlxG.elapsed, Velocity, Acceleration, Drag, Max);
}


/**
* Just grabs the current "ticks" or time in milliseconds that has passed since Flash Player started up.
* Useful for finding out how long it takes to execute specific blocks of code.
Expand Down

0 comments on commit 01aff09

Please sign in to comment.