forked from BabylonJS/Babylon.js
/
physicsBody.ts
208 lines (188 loc) · 4.99 KB
/
physicsBody.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import type { IPhysicsEnginePlugin, MassProperties } from "./IPhysicsEnginePlugin";
import type { PhysicsShape } from "./physicsShape";
import type { Vector3 } from "../../Maths/math.vector";
import { Quaternion } from "../../Maths/math.vector";
import type { Scene } from "../../scene";
import type { PhysicsEngine } from "./physicsEngine";
import type { Mesh, TransformNode } from "../../Meshes";
/**
*
*/
/** @internal */
export class PhysicsBody {
/** @internal */
public _pluginData: any = undefined;
/**
*
*/
public _pluginDataInstances: Array<any> = [];
private _physicsPlugin: IPhysicsEnginePlugin;
/**
*
*/
node: TransformNode;
/**
*
* @param scene
* @returns
*/
constructor(node: TransformNode, scene: Scene) {
if (!scene) {
return;
}
const physicsEngine = scene.getPhysicsEngine() as PhysicsEngine;
if (!physicsEngine) {
throw new Error("No Physics Engine available.");
}
if (physicsEngine.getPluginVersion() != 2) {
throw new Error("Plugin version is incorrect. Expected version 2.");
}
const physicsPlugin = physicsEngine.getPhysicsPlugin();
if (!physicsPlugin) {
throw new Error("No Physics Plugin available.");
}
this._physicsPlugin = physicsPlugin as IPhysicsEnginePlugin;
if (!node.rotationQuaternion) {
node.rotationQuaternion = Quaternion.FromEulerAngles(node.rotation.x, node.rotation.y, node.rotation.z);
}
// instances?
const m = node as Mesh;
if (m.hasThinInstances) {
this._physicsPlugin.initBodyInstances(this, m);
} else {
// single instance
this._physicsPlugin.initBody(this, node.position, node.rotationQuaternion);
}
this.node = node;
physicsEngine.addBody(this);
}
/**
*
* @param shape
*/
public setShape(shape: PhysicsShape): void {
this._physicsPlugin.setShape(this, shape);
}
/**
*
* @returns
*/
public getShape(): PhysicsShape | undefined {
return this._physicsPlugin.getShape(this);
}
/**
*
* @param group
*/
public setFilterGroup(group: number): void {
this._physicsPlugin.setFilterGroup(this, group);
}
/**
*
* @returns
*/
public getFilterGroup(): number {
return this._physicsPlugin.getFilterGroup(this);
}
/**
*
* @param eventMask
*/
public setEventMask(eventMask: number): void {
this._physicsPlugin.setEventMask(this, eventMask);
}
/**
*
* @returns
*/
public getEventMask(): number {
return this._physicsPlugin.getEventMask(this);
}
/**
*
* @param massProps
*/
public setMassProperties(massProps: MassProperties): void {
this._physicsPlugin.setMassProperties(this, massProps);
}
/**
*
* @returns
*/
public getMassProperties(): MassProperties | undefined {
return this._physicsPlugin.getMassProperties(this);
}
/**
*
* @param damping
*/
public setLinearDamping(damping: number): void {
this._physicsPlugin.setLinearDamping(this, damping);
}
/**
*
* @returns
*/
public getLinearDamping(): number {
return this._physicsPlugin.getLinearDamping(this);
}
/**
*
* @param damping
*/
public setAngularDamping(damping: number): void {
this._physicsPlugin.setAngularDamping(this, damping);
}
/**
*
* @returns
*/
public getAngularDamping(): number {
return this._physicsPlugin.getAngularDamping(this);
}
/**
*
* @param linVel
*/
public setLinearVelocity(linVel: Vector3): void {
this._physicsPlugin.setLinearVelocity(this, linVel);
}
/**
*
* @returns
*/
public getLinearVelocityToRef(linVel: Vector3): void {
return this._physicsPlugin.getLinearVelocityToRef(this, linVel);
}
/**
*
* @param angVel
*/
public setAngularVelocity(angVel: Vector3): void {
this._physicsPlugin.setAngularVelocity(this, angVel);
}
/**
*
* @returns
*/
public getAngularVelocityToRef(angVel: Vector3): void {
return this._physicsPlugin.getAngularVelocityToRef(this, angVel);
}
/**
*
* @param location
* @param impulse
*/
public applyImpulse(location: Vector3, impulse: Vector3): void {
this._physicsPlugin.applyImpulse(this, location, impulse);
}
public getGeometry(): {} {
return this._physicsPlugin.getBodyGeometry(this);
}
/**
*
*/
public dispose() {
this._physicsPlugin.disposeBody(this);
}
}