forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplane.js
More file actions
64 lines (55 loc) · 2.06 KB
/
Copy pathplane.js
File metadata and controls
64 lines (55 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Vec3 } from '../math/vec3.js';
const tmpVecA = new Vec3();
/**
* @private
* @class
* @name Plane
* @classdesc An infinite plane.
* @description Create an infinite plane.
* @param {Vec3} [point] - Point position on the plane. The constructor takes a reference of this parameter.
* @param {Vec3} [normal] - Normal of the plane. The constructor takes a reference of this parameter.
*/
class Plane {
constructor(point = new Vec3(), normal = new Vec3(0, 0, 1)) {
this.normal = normal;
this.point = point;
}
/**
* @private
* @function
* @name Plane#intersectsLine
* @description Test if the plane intersects between two points.
* @param {Vec3} start - Start position of line.
* @param {Vec3} end - End position of line.
* @param {Vec3} [point] - If there is an intersection, the intersection point will be copied into here.
* @returns {boolean} True if there is an intersection.
*/
intersectsLine(start, end, point) {
const d = -this.normal.dot(this.point);
const d0 = this.normal.dot(start) + d;
const d1 = this.normal.dot(end) + d;
const t = d0 / (d0 - d1);
const intersects = t >= 0 && t <= 1;
if (intersects && point)
point.lerp(start, end, t);
return intersects;
}
/**
* @private
* @function
* @name Plane#intersectsRay
* @description Test if a ray intersects with the infinite plane.
* @param {Ray} ray - Ray to test against (direction must be normalized).
* @param {Vec3} [point] - If there is an intersection, the intersection point will be copied into here.
* @returns {boolean} True if there is an intersection.
*/
intersectsRay(ray, point) {
const pointToOrigin = tmpVecA.sub2(this.point, ray.origin);
const t = this.normal.dot(pointToOrigin) / this.normal.dot(ray.direction);
const intersects = t >= 0;
if (intersects && point)
point.copy(ray.direction).mulScalar(t).add(ray.origin);
return intersects;
}
}
export { Plane };