Skip to content

Commit

Permalink
fix: Invalidate polygon cache on resize (#1529)
Browse files Browse the repository at this point in the history
  • Loading branch information
spydon committed Apr 10, 2022
1 parent bdb360f commit 11bf75d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
3 changes: 2 additions & 1 deletion packages/flame/lib/src/geometry/polygon_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class PolygonComponent extends ShapeComponent {
final position = absoluteTopLeftPosition;
if (!_cachedGlobalVertices.isCacheValid<dynamic>(<dynamic>[
position,
size,
scale,
angle,
])) {
Expand All @@ -176,7 +177,7 @@ class PolygonComponent extends ShapeComponent {
}
_cachedGlobalVertices.updateCache<dynamic>(
_globalVertices,
<dynamic>[position.clone(), scale.clone(), angle],
<dynamic>[position.clone(), size.clone(), scale.clone(), angle],
);
}
return _cachedGlobalVertices.value!;
Expand Down
65 changes: 34 additions & 31 deletions packages/flame/test/collisions/collision_callback_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,7 @@ void main() {
},
);

// Reproduces #1478, it creates many blocks lined up on the diagonal with one
// empty space (one block-unit) between them. Then it moves another block so
// that it starts colliding with one of the blocks, but for some reason the
// collision is deemed to end in the same tick as it starts, which shouldn't
// be possible. If you change the amount of blocks that are generated, this
// will happen on different blocks (even though the blocks haven't moved).
// Reproduced #1478
withCollidables.test(
'collision callbacks with many hitboxes added',
(game) async {
Expand All @@ -343,31 +338,6 @@ void main() {
}
await game.ensureAddAll(blocks);

// All
// amount = 33 - 2 bad
// 10: [200.0,210.0]
// 21: [420.0,430.0]

// amount = 100 - 6 bad
// 10: [200.0,210.0]
// 21: [420.0,430.0]
// 33: [660.0,670.0]
// 66: [1320.0,1330.0]
// 77: [1540.0,1550.0]
// 88: [1760.0,1770.0]

// amount = 1000 - 80 bad
// 11: [220.0,230.0]
// 24: [480.0,490.0]
// 36: [720.0,730.0]
// 49: [980.0,990.0]
// 60: [1200.0,1210.0]
// ...
// 949: [18980.0,18990.0]
// 962: [19240.0,19250.0]
// 974: [19480.0,19490.0]
// 987: [19740.0,19750.0]

for (var i = 0; i < blocks.length; i++) {
player.position = Vector2.all(-10000);
game.update(0);
Expand Down Expand Up @@ -407,4 +377,37 @@ void main() {
expect(player.endCounter, 0);
},
);

// Reproduced #1478
withCollidables.test(
'collision callbacks with changed game size',
(game) async {
final block = TestBlock(Vector2.all(20), Vector2.all(10))
..anchor = Anchor.center;
await game.ensureAddAll([ScreenHitbox(), block]);

game.update(0);
expect(block.startCounter, 0);
expect(block.onCollisionCounter, 0);
expect(block.endCounter, 0);
block.position.x = game.size.x;

game.update(0);
expect(block.startCounter, 1);
expect(block.onCollisionCounter, 1);
expect(block.endCounter, 0);
game.onGameResize(game.size * 2);

game.update(0);
expect(block.startCounter, 1);
expect(block.onCollisionCounter, 1);
expect(block.endCounter, 1);
block.position.y = game.size.y;

game.update(0);
expect(block.startCounter, 2);
expect(block.onCollisionCounter, 2);
expect(block.endCounter, 1);
},
);
}

0 comments on commit 11bf75d

Please sign in to comment.