Skip to content

Commit

Permalink
fix: onCollision does not fire if collision canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Feb 16, 2024
1 parent c2f99a8 commit 413d34d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/engine/Collision/CollisionSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ export class CollisionSystem extends System {

// Record contacts for start/end
for (const contact of contacts) {
if (contact.isCanceled()) {
continue;
}
// Process composite ids, things with the same composite id are treated as the same collider for start/end
const index = contact.id.indexOf('|');
if (index > 0) {
Expand Down
29 changes: 29 additions & 0 deletions src/spec/CollisionSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,33 @@ describe('A Collision', () => {

clock.run(5, 1000);
});

it('should not fire onCollisionStart if the collision has been canceled', () => {
const block1 = new ex.Actor({x: 200, y: 200, width: 50, height: 50, color: ex.Color.Red.clone()});
block1.body.collisionType = ex.CollisionType.Active;
block1.vel.x = 100;

const block2 = new ex.Actor({x: 400, y: 200, width: 50, height: 50, color: ex.Color.DarkGray.clone()});
block2.collider.useCompositeCollider([
ex.Shape.Box(50, 50),
ex.Shape.Box(50, 50)
]);
block2.body.collisionType = ex.CollisionType.Fixed;
block2.vel.x = -100;


block1.onCollisionStart = jasmine.createSpy('onCollisionStart');
block2.onPreCollisionResolve = (self, other, side, contact) => {
contact.cancel();
};
spyOn(block2, 'onPreCollisionResolve').and.callThrough();

engine.add(block1);
engine.add(block2);

clock.run(1, 1000);

expect(block2.onPreCollisionResolve).toHaveBeenCalled();
expect(block1.onCollisionStart).not.toHaveBeenCalled();
});
});

0 comments on commit 413d34d

Please sign in to comment.