From a26393fcc055489504b4cca0f8719d1eb554e414 Mon Sep 17 00:00:00 2001 From: Logan Date: Sun, 13 Apr 2025 23:52:07 -0600 Subject: [PATCH 1/3] test cases to exhibit collision issues --- tests/unit/src/flixel/FlxObjectTest.hx | 37 ++++++++++++++++++++--- tests/unit/src/flixel/math/FlxRectTest.hx | 4 +-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/tests/unit/src/flixel/FlxObjectTest.hx b/tests/unit/src/flixel/FlxObjectTest.hx index dc89530222..d25a42dda9 100644 --- a/tests/unit/src/flixel/FlxObjectTest.hx +++ b/tests/unit/src/flixel/FlxObjectTest.hx @@ -80,7 +80,7 @@ class FlxObjectTest extends FlxTest } @Test - function testSeprateX():Void + function testSeparateX():Void { final object1 = new FlxObject(5, 0, 10, 10); object1.last.x = 10; @@ -100,7 +100,7 @@ class FlxObjectTest extends FlxTest } @Test - function testSeprateY():Void + function testSeparateY():Void { final object1 = new FlxObject(0, 5, 10, 10); object1.last.y = 10; @@ -117,9 +117,36 @@ class FlxObjectTest extends FlxTest Assert.isFalse(FlxG.overlap(object1, object2)); Assert.isTrue(object1.y > object2.y); } + @Test + function testComputeOverlapOnBothAxisNewlyOverlapping():Void + { + final object1 = new FlxObject(10.01, -.01, 10, 10); + final object2 = new FlxObject(0, 10, 10, 10); + + object1.setPosition(9.95, .05); + + FlxAssert.areNear(-.05, FlxObject.computeOverlapX(object1, object2)); + FlxAssert.areNear(-.05, FlxObject.computeOverlapY(object1, object2)); + } + + @Test + function testSeparateOnBothAxisNewlyOverlapping():Void + { + final object1 = new FlxObject(10.01, -.01, 10, 10); + final object2 = new FlxObject(0, 10, 10, 10); + object2.immovable = true; + + object1.setPosition(9.95, .05); + + Assert.isTrue(FlxObject.separate(object1, object2)); + // X-axis resolves first and no collision + Assert.areEqual(9.95, object1.x); + // Y-axis resolves second and is stopped by collision + Assert.areEqual(0, object1.y); + } @Test - function testSeprateXFromOpposite():Void + function testSeparateXFromOpposite():Void { /* * NOTE: An odd y value on either may result in a rounding error where the second @@ -142,7 +169,7 @@ class FlxObjectTest extends FlxTest } @Test - function testSeprateYFromOpposite():Void + function testSeparateYFromOpposite():Void { /* * NOTE: An odd y value on either may result in a rounding error where the second @@ -371,7 +398,7 @@ class FlxObjectTest extends FlxTest } @Test - function testgetRotatedBounds() + function testGetRotatedBounds() { var expected = FlxRect.get(); var rect = FlxRect.get(); diff --git a/tests/unit/src/flixel/math/FlxRectTest.hx b/tests/unit/src/flixel/math/FlxRectTest.hx index 0a7c24d6ac..85af945c28 100644 --- a/tests/unit/src/flixel/math/FlxRectTest.hx +++ b/tests/unit/src/flixel/math/FlxRectTest.hx @@ -34,7 +34,7 @@ class FlxRectTest extends FlxTest } @Test - function testgetRotatedBounds() + function testGetRotatedBounds() { var pivot = FlxPoint.get(); var expected = FlxRect.get(); @@ -64,7 +64,7 @@ class FlxRectTest extends FlxTest } @Test - function testgetRotatedBoundsSelf() + function testGetRotatedBoundsSelf() { var pivot = FlxPoint.get(); var expected = FlxRect.get(); From ea8454151dd61e86dfda04b3a7c8352ec940aec1 Mon Sep 17 00:00:00 2001 From: Logan Date: Mon, 14 Apr 2025 00:09:24 -0600 Subject: [PATCH 2/3] add piecewise separate logic and clean up tests --- tests/unit/src/flixel/FlxObjectTest.hx | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/tests/unit/src/flixel/FlxObjectTest.hx b/tests/unit/src/flixel/FlxObjectTest.hx index d25a42dda9..5094fcaf08 100644 --- a/tests/unit/src/flixel/FlxObjectTest.hx +++ b/tests/unit/src/flixel/FlxObjectTest.hx @@ -117,30 +117,19 @@ class FlxObjectTest extends FlxTest Assert.isFalse(FlxG.overlap(object1, object2)); Assert.isTrue(object1.y > object2.y); } - @Test - function testComputeOverlapOnBothAxisNewlyOverlapping():Void - { - final object1 = new FlxObject(10.01, -.01, 10, 10); - final object2 = new FlxObject(0, 10, 10, 10); - - object1.setPosition(9.95, .05); - - FlxAssert.areNear(-.05, FlxObject.computeOverlapX(object1, object2)); - FlxAssert.areNear(-.05, FlxObject.computeOverlapY(object1, object2)); - } @Test function testSeparateOnBothAxisNewlyOverlapping():Void { - final object1 = new FlxObject(10.01, -.01, 10, 10); + final object1 = new FlxObject(11, -1, 10, 10); final object2 = new FlxObject(0, 10, 10, 10); object2.immovable = true; - object1.setPosition(9.95, .05); + object1.setPosition(9, 2); Assert.isTrue(FlxObject.separate(object1, object2)); // X-axis resolves first and no collision - Assert.areEqual(9.95, object1.x); + Assert.areEqual(9, object1.x); // Y-axis resolves second and is stopped by collision Assert.areEqual(0, object1.y); } From e77fc3f17cb7e2833b6ee5f94f00964b9b42f12c Mon Sep 17 00:00:00 2001 From: Logan Date: Mon, 14 Apr 2025 00:18:24 -0600 Subject: [PATCH 3/3] FlxObject separate logic now uses temp variable to allow updating axis separate piecewise --- flixel/FlxObject.hx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index bff324c41a..756d23a498 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -159,8 +159,16 @@ class FlxObject extends FlxBasic */ public static function separate(object1:FlxObject, object2:FlxObject):Bool { + var tmp1 = object1.last.copyTo(); + var tmp2 = object2.last.copyTo(); final separatedX = separateX(object1, object2); + object1.last.x = object1.x; + object2.last.x = object2.x; final separatedY = separateY(object1, object2); + object1.last.copyFrom(tmp1); + object2.last.copyFrom(tmp2); + tmp1.put(); + tmp2.put(); return separatedX || separatedY; /*