Skip to content

Commit

Permalink
fix: SearchAllColliders with filter
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Feb 13, 2024
1 parent a35218b commit bfb72af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/engine/Collision/Detection/DynamicTreeCollisionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ export class DynamicTreeCollisionProcessor implements CollisionProcessor {
if (options?.filter) {
if (options.filter(hit)) {
results.push(hit);
if (!searchAllColliders) {
// returning true exits the search
return true;
}
}
} else {
results.push(hit);
}
if (!searchAllColliders) {
// returning true exits the search
return true;
if (!searchAllColliders) {
// returning true exits the search
return true;
}
}
}
return false;
Expand Down
24 changes: 24 additions & 0 deletions src/spec/PhysicsWorldSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,28 @@ describe('A physics world', () => {
expect(hits[0].distance).toBe(275);
expect(hits[0].point).toEqual(ex.vec(275, 0));
});

it('can rayCast with filter and search all colliders false, returns 1 hit', () => {
const sut = TestUtils.engine();
const actor1 = new ex.Actor({x: 100, y: 0, width: 50, height: 50});
sut.currentScene.add(actor1);
const actor2 = new ex.Actor({x: 200, y: 0, width: 50, height: 50});
sut.currentScene.add(actor2);
const actor3 = new ex.Actor({x: 300, y: 0, width: 50, height: 50, collisionGroup: new ex.CollisionGroup('test', 0b1, ~0b1)});
sut.currentScene.add(actor3);

const ray = new ex.Ray(ex.vec(0, 0), ex.Vector.Right);
const hits = sut.currentScene.physics.rayCast(ray, {
searchAllColliders: false,
filter: (hit) => {
return hit.body.group.name === 'test';
}
});

expect(hits.length).toBe(1);
expect(hits[0].body).toEqual(actor3.body);
expect(hits[0].collider).toEqual(actor3.collider.get());
expect(hits[0].distance).toBe(275);
expect(hits[0].point).toEqual(ex.vec(275, 0));
});
});

0 comments on commit bfb72af

Please sign in to comment.