Skip to content

Commit f5fc836

Browse files
Piecewise axis separate (#3418)
* test cases to exhibit collision issues * add piecewise separate logic and clean up tests * FlxObject separate logic now uses temp variable to allow updating axis separate piecewise
1 parent e356bd2 commit f5fc836

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

flixel/FlxObject.hx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,16 @@ class FlxObject extends FlxBasic
159159
*/
160160
public static function separate(object1:FlxObject, object2:FlxObject):Bool
161161
{
162+
var tmp1 = object1.last.copyTo();
163+
var tmp2 = object2.last.copyTo();
162164
final separatedX = separateX(object1, object2);
165+
object1.last.x = object1.x;
166+
object2.last.x = object2.x;
163167
final separatedY = separateY(object1, object2);
168+
object1.last.copyFrom(tmp1);
169+
object2.last.copyFrom(tmp2);
170+
tmp1.put();
171+
tmp2.put();
164172
return separatedX || separatedY;
165173

166174
/*

tests/unit/src/flixel/FlxObjectTest.hx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class FlxObjectTest extends FlxTest
8080
}
8181

8282
@Test
83-
function testSeprateX():Void
83+
function testSeparateX():Void
8484
{
8585
final object1 = new FlxObject(5, 0, 10, 10);
8686
object1.last.x = 10;
@@ -100,7 +100,7 @@ class FlxObjectTest extends FlxTest
100100
}
101101

102102
@Test
103-
function testSeprateY():Void
103+
function testSeparateY():Void
104104
{
105105
final object1 = new FlxObject(0, 5, 10, 10);
106106
object1.last.y = 10;
@@ -119,7 +119,23 @@ class FlxObjectTest extends FlxTest
119119
}
120120

121121
@Test
122-
function testSeprateXFromOpposite():Void
122+
function testSeparateOnBothAxisNewlyOverlapping():Void
123+
{
124+
final object1 = new FlxObject(11, -1, 10, 10);
125+
final object2 = new FlxObject(0, 10, 10, 10);
126+
object2.immovable = true;
127+
128+
object1.setPosition(9, 2);
129+
130+
Assert.isTrue(FlxObject.separate(object1, object2));
131+
// X-axis resolves first and no collision
132+
Assert.areEqual(9, object1.x);
133+
// Y-axis resolves second and is stopped by collision
134+
Assert.areEqual(0, object1.y);
135+
}
136+
137+
@Test
138+
function testSeparateXFromOpposite():Void
123139
{
124140
/*
125141
* NOTE: An odd y value on either may result in a rounding error where the second
@@ -142,7 +158,7 @@ class FlxObjectTest extends FlxTest
142158
}
143159

144160
@Test
145-
function testSeprateYFromOpposite():Void
161+
function testSeparateYFromOpposite():Void
146162
{
147163
/*
148164
* NOTE: An odd y value on either may result in a rounding error where the second
@@ -371,7 +387,7 @@ class FlxObjectTest extends FlxTest
371387
}
372388

373389
@Test
374-
function testgetRotatedBounds()
390+
function testGetRotatedBounds()
375391
{
376392
var expected = FlxRect.get();
377393
var rect = FlxRect.get();

tests/unit/src/flixel/math/FlxRectTest.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class FlxRectTest extends FlxTest
3434
}
3535

3636
@Test
37-
function testgetRotatedBounds()
37+
function testGetRotatedBounds()
3838
{
3939
var pivot = FlxPoint.get();
4040
var expected = FlxRect.get();
@@ -64,7 +64,7 @@ class FlxRectTest extends FlxTest
6464
}
6565

6666
@Test
67-
function testgetRotatedBoundsSelf()
67+
function testGetRotatedBoundsSelf()
6868
{
6969
var pivot = FlxPoint.get();
7070
var expected = FlxRect.get();

0 commit comments

Comments
 (0)