-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
physics-based-component.ts
413 lines (344 loc) · 11.7 KB
/
physics-based-component.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
* @category physics
* @hidden
*/
/** */
import { Component } from '../../../core/components/component';
import { Mat4, Quat, Vec3 } from '../../../core/math';
// tslint:disable-next-line:max-line-length
import { AfterStepCallback, BeforeStepCallback, PhysicsWorldBase, RigidBodyBase } from '../../api';
import { createRigidBody } from '../../instance';
import { ERigidBodyType, ETransformSource } from '../../physic-enum';
import { INode } from '../../../core/utils/interfaces';
import { TransformDirtyBit } from '../../../core/scene-graph/node-enum';
import { error } from '../../../core/platform/debug';
import { PhysicsSystem } from '../physics-system';
export class PhysicsBasedComponent extends Component {
protected get _body () {
return this._sharedBody.body;
}
protected get sharedBody () {
return this._sharedBody;
}
protected get _assertPreload (): boolean {
if (!this._isPreLoaded) {
error('Physic Error: Please make sure that the node has been added to the scene');
}
return this._isPreLoaded;
}
private _sharedBody!: SharedRigidBody;
private _isPreLoaded: boolean = false;
constructor () {
super();
}
/**
* @zh
* 设置分组值。
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
*/
public setGroup (v: number): void {
if (this._assertPreload) {
return this._body!.setGroup(v);
}
}
/**
* @zh
* 获取分组值。
* @returns 整数,范围为 2 的 0 次方 到 2 的 31 次方
*/
public getGroup (): number {
if (this._assertPreload) {
return this._body!.getGroup();
}
return 0;
}
/**
* @zh
* 添加分组值,可填要加入的 group。
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
*/
public addGroup (v: number) {
if (this._assertPreload) {
return this._body!.addGroup(v);
}
}
/**
* @zh
* 减去分组值,可填要移除的 group。
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
*/
public removeGroup (v: number) {
if (this._assertPreload) {
return this._body!.removeGroup(v);
}
}
/**
* @zh
* 获取掩码值。
* @returns 整数,范围为 2 的 0 次方 到 2 的 31 次方
*/
public getMask (): number {
if (this._assertPreload) {
return this._body!.getMask();
}
return 0;
}
/**
* @zh
* 设置掩码值。
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
*/
public setMask (v: number) {
if (this._assertPreload) {
return this._body!.setMask(v);
}
}
/**
* @zh
* 添加掩码值,可填入需要检查的 group。
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
*/
public addMask (v: number) {
if (this._assertPreload) {
return this._body!.addMask(v);
}
}
/**
* @zh
* 减去掩码值,可填入不需要检查的 group。
* @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方
*/
public removeMask (v: number) {
if (this._assertPreload) {
return this._body!.removeMask(v);
}
}
/// COMPONENT LIFECYCLE ///
protected __preload () {
if (!CC_EDITOR) {
if (this._sharedBody == null) {
const physicsBasedComponents = this.node.getComponents(PhysicsBasedComponent);
let sharedBody: SharedRigidBody | null = null;
for (const physicsBasedComponent of physicsBasedComponents) {
if (physicsBasedComponent._sharedBody) {
sharedBody = physicsBasedComponent._sharedBody;
break;
}
}
if (!sharedBody) {
// // binding collider to self or parents rigidbody
// // is exit rigidbody on the node or parents?
// const rigidbody = this._findRigidbody(this.node);
// if (rigidbody) {
// if (rigidbody.sharedBody != null) {
// sharedBody = rigidbody.sharedBody;
// } else {
// sharedBody = new SharedRigidBody(rigidbody.node, rigidbody, director._physicsSystem._world);
// }
// } else {
// sharedBody = new SharedRigidBody(this.node, null, director._physicsSystem._world);
// }
// binding collider to self rigidybody, if it exist.
const rigidbody = this.getComponent(cc.RigidBodyComponent);
sharedBody = new SharedRigidBody(this.node, rigidbody, PhysicsSystem.instance._world);
}
sharedBody!.ref();
this._sharedBody = sharedBody!;
}
this._isPreLoaded = true;
}
}
protected onEnable () {
if (!CC_EDITOR) {
this.sharedBody.enable();
}
}
protected onDisable () {
if (!CC_EDITOR) {
this.sharedBody.disable();
}
}
protected onDestroy () {
if (!CC_EDITOR) {
this._sharedBody.deref();
(this._sharedBody as any) = null;
}
}
private _findRigidbody (begin: INode) {
const rigidbody = begin.getComponent(cc.RigidBodyComponent);
if (rigidbody) {
return rigidbody;
} else {
if (begin.parent) {
if (begin.parent === begin.scene) {
return null;
} else {
return this._findRigidbody(begin.parent);
}
} else {
return null;
}
}
}
}
class SharedRigidBody {
public get isShapeOnly (): boolean { return this._isShapeOnly; }
public get body () {
return this._body;
}
/** the source to manage body transfrom */
public set transfromSource (v: ETransformSource) {
if (v === ETransformSource.SCENE) {
this._isShapeOnly = true;
} else {
this._isShapeOnly = false;
}
}
public get rigidBody () {
return this._rigidBody;
}
private static _tempMat4 = new Mat4();
private static _tempQuat = new Quat();
private static _tempVec3 = new Vec3();
private _body: RigidBodyBase;
private _refCount = 0;
private _actived = false;
private _world!: PhysicsWorldBase;
private _rigidBody: object | null;
private _node!: INode;
private _worldScale: Vec3 = new Vec3(1, 1, 1);
private _beforeStepCallback!: BeforeStepCallback;
private _afterStepCallback!: AfterStepCallback;
/** 是否只有Collider组件 */
private _isShapeOnly: boolean = true;
constructor (node: INode, rigidBody: object | null, world: PhysicsWorldBase) {
this._body = createRigidBody({
name: node.name,
});
this._node = node;
this._rigidBody = rigidBody;
this._world = world;
this._body.setUserData(this._rigidBody);
this._beforeStepCallback = this._beforeStep.bind(this);
if (!CC_PHYSICS_BUILT_IN) {
this._afterStepCallback = this._afterStep.bind(this);
if (this._rigidBody) {
this._isShapeOnly = false;
} else {
this._isShapeOnly = true;
this._body.setUseGravity(false);
}
}
}
public ref () {
++this._refCount;
}
public deref () {
--this._refCount;
if (!this._refCount) {
this.destroy();
}
}
public enable () {
this._activeBody();
}
public disable () {
this._deactiveBody();
}
public destroy () {
// before destroy make sure body is deactive
this._deactiveBody();
this._body.setUserData(null);
(this._body as any) = null;
(this._beforeStepCallback as any) = null;
if (!CC_PHYSICS_BUILT_IN) {
(this._afterStepCallback as any) = null;
}
(this._world as any) = null;
(this._node as any) = null;
if (this._rigidBody) {
(this._rigidBody as any) = null;
}
}
public syncPhysWithScene () {
this._syncPhysWithScene(this._node);
}
private _syncPhysWithScene (node: INode) {
// sync position rotation
node.getWorldMatrix(SharedRigidBody._tempMat4);
node.getWorldRotation(SharedRigidBody._tempQuat);
this._body.translateAndRotate(SharedRigidBody._tempMat4, SharedRigidBody._tempQuat);
}
/**
* Push the rigidbody's transform information back to node.
*/
private _syncSceneWithPhys () {
if (!this._node) {
return;
}
this._body.getPosition(SharedRigidBody._tempVec3);
this._node.setWorldPosition(SharedRigidBody._tempVec3);
if (!this._body.getFreezeRotation()) {
this._body.getRotation(SharedRigidBody._tempQuat);
this._node.setWorldRotation(SharedRigidBody._tempQuat);
}
}
private _activeBody () {
// Sync scenes, no matter how many activations
this._syncPhysWithScene(this._node);
if (this._actived) {
return;
}
this._actived = true;
this._body.setWorld(this._world);
this._world.addBeforeStep(this._beforeStepCallback);
if (!CC_PHYSICS_BUILT_IN) {
this._world.addAfterStep(this._afterStepCallback);
this._body.wakeUp();
}
}
private _deactiveBody () {
if (!this._actived) {
return;
}
this._actived = false;
this._world.removeBeforeStep(this._beforeStepCallback);
if (!CC_PHYSICS_BUILT_IN) {
this._world.removeAfterStep(this._afterStepCallback);
this._body.sleep();
}
this._body.setWorld(null);
}
private _beforeStep () {
// 开始物理计算之前,用户脚本或引擎功能有可能改变节点的Transform,所以需要判断并进行更新
if (this._node.hasChangedFlags) {
// scale 进行单独判断,因为目前的物理系统处理后不会改变scale的属性
if (this._node.hasChangedFlags & TransformDirtyBit.SCALE) {
this._body.scaleAllShapes(this._node.worldScale);
}
this._syncPhysWithScene(this._node);
if (!CC_PHYSICS_BUILT_IN) {
if (this._body.isSleeping()) {
this._body.wakeUp();
}
}
}
}
private _afterStep () {
if (!CC_PHYSICS_BUILT_IN) {
// 物理计算之后,除了只有形状组件的节点,其它有刚体组件的节点,并且刚体类型为DYNAMIC的,需要将计算结果同步到Scene中
if (!this._isShapeOnly && this._body.getType() === ERigidBodyType.DYNAMIC) {
this._syncSceneWithPhys();
} else {
// 对于只有形状组件的节点,需要将Scene中节点的Transform同步到Phyisc。
// 这是因为物理计算后可能会改变一些节点,这会导致这些子节点的Transform也发生改变。
if (this._node.hasChangedFlags) {
this._syncPhysWithScene(this._node);
if (this._body.isSleeping()) {
this._body.wakeUp();
}
}
}
}
}
}