Skip to content

Changed the way maximum velocity is capped, fixing 2 importants bugs: #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/org/flixel/plugin/photonstorm/FlxControlHandler.as
Original file line number Diff line number Diff line change
Expand Up @@ -1183,14 +1183,30 @@ package org.flixel.plugin.photonstorm

if (capVelocity)
{
if (entity.velocity.x > entity.maxVelocity.x)
if (entity.velocity.x != 0 && entity.velocity.y != 0)
{
entity.velocity.x = entity.maxVelocity.x;
// If both velocities are non-zero, we cap them to: maxValue * (sqrt(2) / 2)
if (Math.abs(entity.velocity.x) > (entity.maxVelocity.x * 0.70711))
{
entity.velocity.x = FlxMath.getSignOfNumber(entity.velocity.x) * entity.maxVelocity.x * 0.70711;
}

if (Math.abs(entity.velocity.y) > (entity.maxVelocity.y * 0.70711) )
{
entity.velocity.y = FlxMath.getSignOfNumber(entity.velocity.y) * entity.maxVelocity.y * 0.70711;
}
}

if (entity.velocity.y > entity.maxVelocity.y)
else
{
entity.velocity.y = entity.maxVelocity.y;
if (Math.abs(entity.velocity.x) > entity.maxVelocity.x)
{
entity.velocity.x = FlxMath.getSignOfNumber(entity.velocity.x) * entity.maxVelocity.x;
}

if (Math.abs(entity.velocity.y) > entity.maxVelocity.y)
{
entity.velocity.y = FlxMath.getSignOfNumber(entity.velocity.y) * entity.maxVelocity.y;
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/org/flixel/plugin/photonstorm/FlxMath.as
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,30 @@ package org.flixel.plugin.photonstorm
{
return ax * bx + ay * by;
}

/**
* Returns either -1 or 1 depending on the sign of the input int value. Notice: sign of 0 is 1
*
* @param val Input int value
*
* @return Sign of input (int)
*/
public static function getSignOfInt(val : int):int
{
return (val >= 0 ? 1 : -1);
}

/**
* Returns either -1 or 1 (number) depending on the sign of the input number value. Notice: sign of 0 is 1
*
* @param val Input number value
*
* @return Sign of input (number)
*/
public static function getSignOfNumber(val : Number):Number
{
return (val >= 0.0 ? 1.0 : -1.0);
}

/**
* Randomly returns either a 1 or -1
Expand Down