/
rigid-body-component.ts
540 lines (479 loc) · 14.4 KB
/
rigid-body-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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/****************************************************************************
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
import { IRigidBody } from '../../spec/I-rigid-body';
import { createRigidBody } from '../instance';
const {
ccclass,
disallowMultiple,
executeInEditMode,
executionOrder,
menu,
property,
} = cc._decorator;
const Vec3 = cc.Vec3;
/**
* !#en
* RigidBody is the basic object that make up the physical world, and it can make a node physically affected and react.
* !#zh
* 刚体是组成物理世界的基本对象,可以让一个节点受到物理影响并产生反应。该组件在使用 Builtin 物理引擎时无效。
* @class RigidBody3D
* @extends Component
*/
@ccclass('cc.RigidBody3D')
@executionOrder(99)
@menu('i18n:MAIN_MENU.component.physics/Rigid Body 3D')
@executeInEditMode
@disallowMultiple
export class RigidBody3D extends cc.Component {
/// PUBLIC PROPERTY GETTER\SETTER ///
/**
* !#en
* Whether sleep is allowed.
* !#zh
* 是否允许休眠。
* @property {boolean} allowSleep
*/
public get allowSleep (): boolean {
return this._allowSleep;
}
public set allowSleep (v: boolean) {
this._allowSleep = v;
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.allowSleep = v;
}
}
/**
* !#en
* The mass of the rigidbody.
* !#zh
* 刚体的质量。
* @property {number} mass
*/
@property({
displayOrder: 0
})
public get mass () {
return this._mass;
}
public set mass (value) {
this._mass = value;
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.mass = value;
}
}
/**
* !#en
* Used to reduce the linear rate of rigidbody. The larger the value, the slower the rigidbody moves.
* !#zh
* 线性阻尼,用于减小刚体的线性速率,值越大物体移动越慢。
* @property {number} linearDamping
*/
@property({
displayOrder: 1
})
public get linearDamping () {
return this._linearDamping;
}
public set linearDamping (value) {
this._linearDamping = value;
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.linearDamping = value;
}
}
/**
* !#en
* Used to reduce the rotation rate of rigidbody. The larger the value, the slower the rigidbody rotates.
* !#zh
* 角阻尼,用于减小刚体的旋转速率,值越大刚体旋转越慢。
* @property {number} angularDamping
*/
@property({
displayOrder: 2
})
public get angularDamping () {
return this._angularDamping;
}
public set angularDamping (value) {
this._angularDamping = value;
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.angularDamping = value;
}
}
/**
* !#en
* If enabled, the developer controls the displacement and rotation of the rigidbody, not the physics engine.
* !#zh
* 是否由开发者来控制刚体的位移和旋转,而不是受物理引擎的影响。
* @property {boolean} isKinematic
*/
@property({
displayOrder: 3
})
public get isKinematic () {
return this._isKinematic;
}
public set isKinematic (value) {
this._isKinematic = value;
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.isKinematic = value;
}
}
/**
* !#en
* If enabled, the rigidbody is affected by gravity.
* !#zh
* 如果开启,刚体会受到重力影响。
* @property {boolean} useGravity
*/
@property({
displayOrder: 4
})
public get useGravity () {
return this._useGravity;
}
public set useGravity (value) {
this._useGravity = value;
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.useGravity = value;
}
}
/**
* !#en
* If enabled, the rigidbody will be fixed without rotation during a collision.
* !#zh
* 如果开启,发生碰撞时会固定刚体不产生旋转。
* @property {boolean} fixedRotation
*/
@property({
displayOrder: 5
})
public get fixedRotation () {
return this._fixedRotation;
}
public set fixedRotation (value) {
this._fixedRotation = value;
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.fixedRotation = value;
}
}
/**
* !#en
* It can affect the linear velocity change of the rigidbody in each axis. The larger the value, the faster the rigidbody moves.
* !#zh
* 线性因子,可影响刚体在每个轴向的线性速度变化,值越大刚体移动越快。
* @property {Vec3} linearFactor
*/
@property({
displayOrder: 6
})
public get linearFactor (): cc.Vec3 {
return this._linearFactor;
}
public set linearFactor (value: cc.Vec3) {
Vec3.copy(this._linearFactor, value);
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.linearFactor = this._linearFactor;
}
}
/**
* !#en
* It can affect the rotation speed change of the rigidbody in each axis. The larger the value, the faster the rigidbody rotates.
* !#zh
* 旋转因子,可影响刚体在每个轴向的旋转速度变化,值越大刚体旋转越快。
* @property {Vec3} angularFactor
*/
@property({
displayOrder: 7
})
public get angularFactor () {
return this._angularFactor;
}
public set angularFactor (value: cc.Vec3) {
Vec3.copy(this._angularFactor, value);
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.angularFactor = this._angularFactor;
}
}
/**
* !#en
* The rigidbody is awake.
* !#zh
* 刚体是否为唤醒的状态。
* @property {boolean} isAwake
* @readonly
*/
public get isAwake (): boolean {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
return this._body.isAwake;
}
return false;
}
/**
* !#en
* The rigidbody can enter hibernation.
* !#zh
* 刚体是否为可进入休眠的状态。
* @property {boolean} isSleepy
* @readonly
*/
public get isSleepy (): boolean {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
return this._body.isSleepy;
}
return false;
}
/**
* !#en
* The rigidbody is sleeping.
* !#zh
* 刚体是否为正在休眠的状态。
* @property {boolean} isSleeping
* @readonly
*/
public get isSleeping (): boolean {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
return this._body.isSleeping;
}
return false;
}
/**
* !#en
* Get the rigidbody object inside the physics engine.
* !#zh
* 获得物理引擎内部刚体对象。
* @property {IRigidBody} rigidBody
* @readonly
*/
public get rigidBody () {
return this._body;
}
private _body!: IRigidBody;
/// PRIVATE PROPERTY ///
// @property
private _allowSleep: boolean = true;
@property
private _mass: number = 10;
@property
private _linearDamping: number = 0.1;
@property
private _angularDamping: number = 0.1;
@property
private _fixedRotation: boolean = false;
@property
private _isKinematic: boolean = false;
@property
private _useGravity: boolean = true;
@property
private _linearFactor: cc.Vec3 = new Vec3(1, 1, 1);
@property
private _angularFactor: cc.Vec3 = new Vec3(1, 1, 1);
protected get _assertOnload (): boolean {
const r = this._isOnLoadCalled == 0;
if (r) { cc.error('Physics Error: Please make sure that the node has been added to the scene'); }
return !r;
}
constructor () {
super();
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body = createRigidBody();
}
}
/// COMPONENT LIFECYCLE ///
protected __preload () {
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.__preload!(this);
}
}
protected onEnable () {
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.onEnable!();
}
}
protected onDisable () {
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.onDisable!();
}
}
protected onDestroy () {
if (!CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.onDestroy!();
}
}
/// PUBLIC METHOD ///
/**
* !#en
* A force is applied to a rigid body at a point in world space.
* !#zh
* 在世界空间中的某点上对刚体施加一个作用力。
* @method applyForce
* @param {Vec3} force
* @param {Vec3} relativePoint The point of action, relative to the center of the rigid body.
*/
public applyForce (force: cc.Vec3, relativePoint?: cc.Vec3) {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.applyForce(force, relativePoint);
}
}
/**
* !#en
* Apply a force on the rigid body at a point in local space.
* !#zh
* 在本地空间中的某点上对刚体施加一个作用力。
* @method applyLocalForce
* @param {Vec3} force
* @param {Vec3} localPoint Point of application
*/
public applyLocalForce (force: cc.Vec3, localPoint?: cc.Vec3) {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.applyLocalForce(force, localPoint);
}
}
/**
* !#en
* Apply an impulse to a rigid body at a point in world space.
* !#zh
* 在世界空间的某点上对刚体施加一个冲量。
* @method applyImpulse
* @param {Vec3} impulse
* @param {Vec3} relativePoint The point of action, relative to the center of the rigid body.
*/
public applyImpulse (impulse: cc.Vec3, relativePoint?: cc.Vec3) {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.applyImpulse(impulse, relativePoint);
}
}
/**
* !#en
* Apply an impulse to the rigid body at a point in local space.
* !#zh
* 在本地空间的某点上对刚体施加一个冲量。
* @method applyLocalImpulse
* @param {Vec3} impulse
* @param {Vec3} localPoint Point of application
*/
public applyLocalImpulse (impulse: cc.Vec3, localPoint?: cc.Vec3) {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.applyLocalImpulse(impulse, localPoint);
}
}
/**
* !#en
* Apply a torque to the rigid body.
* !#zh
* 对刚体施加扭转力。
* @method applyTorque
* @param {Vec3} torque
*/
public applyTorque (torque: cc.Vec3) {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.applyTorque(torque);
}
}
/**
* !#en
* Apply a local torque to the rigid body.
* !#zh
* 对刚体施加本地扭转力。
* @method applyLocalTorque
* @param {Vec3} torque
*/
public applyLocalTorque (torque: cc.Vec3) {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.applyLocalTorque(torque);
}
}
/**
* !#en
* Awaken the rigid body.
* !#zh
* 唤醒刚体。
* @method wakeUp
*/
public wakeUp () {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.wakeUp();
}
}
/**
* !#en
* Dormant rigid body.
* !#zh
* 休眠刚体。
* @method sleep
*/
public sleep () {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.sleep();
}
}
/**
* !#en
* Get linear velocity.
* !#zh
* 获取线性速度。
* @method getLinearVelocity
* @param {Vec3} out
*/
public getLinearVelocity (out: cc.Vec3) {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.getLinearVelocity(out);
}
}
/**
* !#en
* Set linear speed.
* !#zh
* 设置线性速度。
* @method setLinearVelocity
* @param {Vec3} value
*/
public setLinearVelocity (value: cc.Vec3): void {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.setLinearVelocity(value);
}
}
/**
* !#en
* Gets the rotation speed.
* !#zh
* 获取旋转速度。
* @method getAngularVelocity
* @param {Vec3} out
*/
public getAngularVelocity (out: cc.Vec3) {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.getAngularVelocity(out);
}
}
/**
* !#en
* Set rotation speed.
* !#zh
* 设置旋转速度。
* @method setAngularVelocity
* @param {Vec3} value
*/
public setAngularVelocity (value: cc.Vec3): void {
if (this._assertOnload && !CC_EDITOR && !CC_PHYSICS_BUILTIN) {
this._body.setAngularVelocity(value);
}
}
}