/
ColliderShape.ts
100 lines (84 loc) · 2.56 KB
/
ColliderShape.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Matrix4 } from "../../math/Matrix4";
import { Ray } from "../../math/Ray";
import { Vector3 } from "../../math/Vector3";
export type HitInfo = { intersectPoint?: Vector3; distance: number; collider?: any };
export enum ColliderShapeType {
None,
Box,
Capsule,
Sphere,
Mesh,
}
/**
* Shape of collider body, Base class of collider shape
* @group Collider
*/
export class ColliderShape {
private _center: Vector3;
private _size: Vector3;
private _halfSize: Vector3;
protected _shapeType: ColliderShapeType = ColliderShapeType.None;
protected static v3_help_0: Vector3;
protected static helpMatrix: Matrix4;
protected static helpRay: Ray;
constructor() {
ColliderShape.v3_help_0 ||= new Vector3();
ColliderShape.helpMatrix ||= new Matrix4();
ColliderShape.helpRay ||= new Ray();
this._center = new Vector3();
this._size = new Vector3();
this._halfSize = new Vector3();
}
public get shapeType() {
return this._shapeType;
}
/**
* Set the position and size of collision objects
* @param ct The position of the collision object in the local space of the object.
* @param sz The size of the collision body in the X, Y, and Z directions.
* @returns
*/
public setFromCenterAndSize(ct?: Vector3, sz?: Vector3): this {
ct && this._center.copy(ct);
sz && this._size.copy(sz);
return this;
}
/**
* The position of the collision object in the local space of the object.
*/
public get center(): Vector3 {
return this._center;
}
public set center(value: Vector3) {
this._center.copy(value);
}
/**
*
* The size of the collision body in the X, Y, and Z directions.
* @returns Vector3
*/
public get size(): Vector3 {
return this._size;
}
public set size(value: Vector3) {
this._size.copy(value);
this._halfSize.copy(value).multiplyScalar(0.5);
}
/**
* Half the size of the collision body.
*/
public get halfSize(): Vector3 {
return this._halfSize;
}
/**
* Ray pickup.Emit a ray from a designated location to detect objects colliding with the ray.
* @param ray emit ray
* @param fromMatrix matrix
* @returns Pick result intersect: whether to collide;
* IntersectPoint: collision point;
* Distance: The distance from the origin of the ray to the collision point.
*/
public rayPick(ray: Ray, fromMatrix: Matrix4): HitInfo {
return null;
}
}