Skip to content

Commit dc33683

Browse files
committed
✨ fix: update intersection logic
1 parent c331003 commit dc33683

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

packages/chili-core/src/math/ray.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ export class Ray {
2424
this.direction = n;
2525
}
2626

27-
intersect(right: Ray): XYZ | undefined {
28-
if (this.direction.isParallelTo(right.direction)) return undefined;
27+
intersect(right: Ray, tolerance = 1e-6): XYZ | undefined {
28+
if (this.direction.isParallelTo(right.direction, tolerance)) return undefined;
2929
const result = this.nearestTo(right);
3030
const vec = result.sub(right.location);
31-
return vec.isParallelTo(right.direction) ? result : undefined;
31+
if (vec.length() < tolerance) return result;
32+
33+
return vec.isParallelTo(right.direction, tolerance) ? result : undefined;
3234
}
3335

3436
distanceTo(right: Ray): number {

packages/chili-core/test/ray.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ describe("test ray", () => {
2626
});
2727

2828
test("test intersect", () => {
29-
let r1 = new Ray(XYZ.zero, XYZ.unitX);
30-
let r2 = new Ray(XYZ.unitX.add(XYZ.unitY), XYZ.unitY);
29+
const r1 = new Ray(XYZ.zero, XYZ.unitX);
30+
const r11 = new Ray(XYZ.zero, XYZ.unitY);
31+
const r2 = new Ray(XYZ.unitX.add(XYZ.unitY), XYZ.unitY);
3132
expect(r1.intersect(r2)).toStrictEqual(XYZ.unitX);
33+
expect(r1.intersect(r11)).toStrictEqual(XYZ.zero);
3234

3335
const r3 = new Ray(XYZ.zero, XYZ.unitX);
3436
const r4 = new Ray(XYZ.unitY, XYZ.unitX);

0 commit comments

Comments
 (0)