forked from galacean/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision.ts
58 lines (53 loc) · 2 KB
/
Collision.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
import { Vector3 } from "@galacean/engine-math";
import { ColliderShape } from "./shape";
import { ICollision } from "@galacean/engine-design";
/**
* Describes a contact point where the collision occurs.
*/
export interface ContactPoint {
/** The position of the contact point between the shapes, in world space. */
readonly position: Vector3;
/** The normal of the contacting surfaces at the contact point. The normal direction points from the second shape to the first shape. */
readonly normal: Vector3;
/** The impulse applied at the contact point, in world space. Divide by the simulation time step to get a force value. */
readonly impulse: Vector3;
/** The separation of the shapes at the contact point. A negative separation denotes a penetration. */
readonly separation: number;
}
export class Collision {
/** @internal */
_nativeCollision: ICollision;
/** The shape be collided. */
shape: ColliderShape;
/**
* Get count of contact points.
*/
get contactCount(): number {
return this._nativeCollision.contactCount;
}
/**
* Get contact points.
* @param outContacts - The result of contact points
* @returns The result of contact points
*/
getContacts(outContacts: ContactPoint[]): ContactPoint[] {
const { shape0Id, shape1Id } = this._nativeCollision;
const nativeContactPoints = this._nativeCollision.getContacts();
for (let i = 0, n = nativeContactPoints.size(); i < n; i++) {
const nativeContractPoint = nativeContactPoints.get(i);
const { position, normal, impulse, separation } = nativeContractPoint;
let factor = 1;
if (shape0Id > shape1Id) {
factor = -1;
}
const contact: ContactPoint = {
position: new Vector3(position.x, position.y, position.z),
normal: new Vector3(normal.x, normal.y, normal.z).scale(factor),
impulse: new Vector3(impulse.x, impulse.y, impulse.z).scale(factor),
separation: separation
};
outContacts.push(contact);
}
return outContacts;
}
}