Skip to content

Commit

Permalink
fix: bbox check uses bodyA twice (#79)
Browse files Browse the repository at this point in the history
* test: add test for early return in checkCollision

This makes sure that `System.checkCollision` returns early when bbox does not overlap.

* fix: assign bodyB to bboxB
  • Loading branch information
RedPhoenixQ committed Jul 15, 2024
1 parent aa2fa3b commit 4826ed0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/system.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ describe("GIVEN System", () => {
expect(collisions).toBe(2);
});

it("THEN bodies with non-intersecting bbox don't check collisions", () => {
const { System } = require("../src");

const physics = new System();

const a = physics.createBox({ x: 10, y: 10 }, 100, 100);
const b = physics.createBox({ x: 300, y: 300 }, 100, 100);

// Set values in response that should not be changed because of early return
physics.response.overlap = 0xdeadbeaf;

const didCollide = physics.checkCollision(a, b);

expect(didCollide).toBe(false);
expect(physics.response.overlap).toBe(0xdeadbeaf);
});

describe("WHEN raycast is called", () => {
it("THEN works correctly on Ellipse", () => {
const { System } = require(".");
Expand Down
2 changes: 1 addition & 1 deletion src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class System<TBody extends Body = Body> extends BaseSystem<TBody> {
response = this.response,
): boolean {
const { bbox: bboxA } = bodyA;
const { bbox: bboxB } = bodyA;
const { bbox: bboxB } = bodyB;
// assess the bodies real aabb without padding
if (
!canInteract(bodyA, bodyB) ||
Expand Down

0 comments on commit 4826ed0

Please sign in to comment.