Skip to content

Commit

Permalink
Update FlxPoint pivotRadians to proper order of operations (#2700)
Browse files Browse the repository at this point in the history
* Update FlxPoint pivotRadians to proper order of operations

* restore previous whitespace. update doc comments

* fix typo

* clockwise4life

Co-authored-by: George Kurelic <[email protected]>
  • Loading branch information
MondayHopscotch and Geokureli authored Dec 15, 2022
1 parent d682f15 commit 9080aee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion flixel/math/FlxPoint.hx
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ import openfl.geom.Point;
*/
public function pivotRadians(pivot:FlxPoint, radians:Float):FlxPoint
{
_point1.copyFrom(pivot).subtractPoint(this);
_point1.copyFrom(this).subtractPoint(pivot);
_point1.radians += radians;
set(_point1.x + pivot.x, _point1.y + pivot.y);
pivot.putWeak();
Expand Down
31 changes: 27 additions & 4 deletions tests/unit/src/flixel/math/FlxPointTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,34 @@ class FlxPointTest extends FlxTest
assertPointEquals(point1, 1, 2);
}

@Test
function testPivotDegrees() {
// Pivot around point in same quadrant
point1.set(10, 10);
point2.set(5, 5);
assertPointNearlyEquals(point1.pivotDegrees(point2, 180), 0, 0);

// pivot around origin
point1.set(1, 10);
point2.set();
assertPointNearlyEquals(point1.pivotDegrees(point2, 90), -10, 1);

// pivot around point in different quadrant
point1.set(10, 10);
point2.set(-1, -1);
assertPointNearlyEquals(point1.pivotDegrees(point2, 45), -1, 14.55);
}

function assertPointEquals(p:FlxPoint, x:Float, y:Float, ?msg:String, ?info:PosInfos)
{
if (msg == null)
msg = 'Expected (x: $x | y: $y) but was $p';

Assert.isTrue(x == p.x && y == p.y, msg, info);
assertPointNearlyEquals(p, x, y, 0.0, msg, info);
}

function assertPointNearlyEquals(p:FlxPoint, x:Float, y:Float, tolerance:Float = .01, ?msg:String, ?info:PosInfos)
{
if (msg == null)
msg = 'Expected (x: $x | y: $y) but was $p';

Assert.isTrue(Math.abs(x - p.x) <= tolerance && Math.abs(y -p.y) <= tolerance, msg, info);
}
}

0 comments on commit 9080aee

Please sign in to comment.