Skip to content

Commit ef80b4e

Browse files
authored
Merge pull request #1932 from INEEDSSD/LayaAir_3.2
优化: 优化Node事件派发、修复多角色控制器添加场景问题.
2 parents 1f632b8 + 9da87f0 commit ef80b4e

File tree

12 files changed

+81
-44
lines changed

12 files changed

+81
-44
lines changed

src/layaAir/laya/Physics3D/Bullet/Collider/btCollider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export class btCollider implements ICollider {
273273
* @param value 碰撞组的值。
274274
*/
275275
setCollisionGroup(value: number) {
276-
if (value != this._collisionGroup && this._btColliderShape) {
276+
if (value != this._collisionGroup && this._btColliderShape && this._physicsManager) {
277277
this._collisionGroup = value;
278278
this._physicsManager.removeCollider(this);
279279
this._physicsManager.addCollider(this);
@@ -287,7 +287,7 @@ export class btCollider implements ICollider {
287287
* @param value 碰撞掩码的值。
288288
*/
289289
setCanCollideWith(value: number) {
290-
if (value != this._canCollideWith && this._btColliderShape) {
290+
if (value != this._canCollideWith && this._btColliderShape && this._physicsManager) {
291291
this._canCollideWith = value;
292292
this._physicsManager.removeCollider(this);
293293
this._physicsManager.addCollider(this);

src/layaAir/laya/Physics3D/Bullet/btPhysicsManager.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,12 @@ export class btPhysicsManager implements IPhysicsManager {
14191419
throw "Simulation:Cannot perform this action when the physics engine is set to CollisionsOnly";
14201420
this._bt.btCollisionWorld_removeCollisionObject(this._btCollisionWorld, character._btCollider);
14211421
this._bt.btDynamicsWorld_removeAction(this._btCollisionWorld, character._btKinematicCharacter);
1422-
var characters: btCharacterCollider[] = this._characters;
1423-
characters.splice(characters.indexOf(character), 1);
1422+
1423+
let characters: btCharacterCollider[] = this._characters;
1424+
let index = characters.indexOf(character)
1425+
if (index != -1) {
1426+
characters.splice(index, 1);
1427+
}
14241428
}
14251429

14261430
// /**

src/layaAir/laya/d3/physics/CharacterController.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,20 @@ export class CharacterController extends PhysicsColliderComponent {
5050
if (Laya3D.enablePhysics && this._physicsManager && Laya3D.PhysicsCreateUtil.getPhysicsCapable(EPhysicsCapable.Physics_CharacterCollider)) {
5151
this._physicsManager = ((<Scene3D>this.owner._scene))._physicsManager;
5252
this._collider = Laya3D.PhysicsCreateUtil.createCharacterController(this._physicsManager);
53+
if (this.colliderShape) {
54+
//需要销毁重新创建,否则无法绑定到collider上
55+
this.colliderShape.destroy();
56+
}
5357
this.colliderShape = new CapsuleColliderShape(this.radius, this.height);
58+
this.colliderShape.localOffset = this._offset;
5459
this._collider.component = this;
5560
} else {
5661
console.error("CharacterController: cant enable CharacterController");
5762
}
5863
}
5964

60-
protected _onAdded(): void {
61-
super._onAdded();
65+
protected _onEnable(): void {
66+
super._onEnable();
6267
this.radius = this._radius;
6368
this.height = this._height;
6469
this.gravity = this._gravity;
@@ -94,9 +99,9 @@ export class CharacterController extends PhysicsColliderComponent {
9499

95100
set radius(value: number) {
96101
this._radius = value;
102+
this._colliderShape && ((this._colliderShape as CapsuleColliderShape).radius = value);
97103
if (this._collider && this.collider.getCapable(ECharacterCapable.Character_Radius)) {
98104
this._collider.setRadius(this._radius);
99-
this._colliderShape && ((this._colliderShape as CapsuleColliderShape).radius = value);
100105
}
101106
}
102107

@@ -110,9 +115,9 @@ export class CharacterController extends PhysicsColliderComponent {
110115

111116
set height(value: number) {
112117
this._height = value;
118+
this._colliderShape && ((this._colliderShape as CapsuleColliderShape).length = value);
113119
if (this._collider && this.collider.getCapable(ECharacterCapable.Character_Height)) {
114120
this._collider.setHeight(this._height);
115-
this._colliderShape && ((this._colliderShape as CapsuleColliderShape).length = value);
116121
}
117122
}
118123

@@ -140,9 +145,9 @@ export class CharacterController extends PhysicsColliderComponent {
140145
}
141146
set centerOffset(value: Vector3) {
142147
this._offset = value;
148+
this._colliderShape && ((this._colliderShape as CapsuleColliderShape).localOffset = value);
143149
if (this._collider && this.collider.getCapable(ECharacterCapable.Character_offset)) {
144150
this._collider.setShapelocalOffset(this._offset);
145-
this._colliderShape && ((this._colliderShape as CapsuleColliderShape).localOffset = value);
146151
}
147152
}
148153

@@ -274,6 +279,7 @@ export class CharacterController extends PhysicsColliderComponent {
274279
*/
275280
constructor() {
276281
super();
282+
this.colliderShape = new CapsuleColliderShape(this.radius, this.height);
277283
}
278284

279285
/**

src/layaAir/laya/d3/physics/PhysicsCollider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export class PhysicsCollider extends PhysicsColliderComponent {
3939
super();
4040
}
4141

42-
_onAdded(): void {
43-
super._onAdded();
42+
_onEnable(): void {
43+
super._onEnable();
4444
this.restitution = this._restitution;
4545
this.friction = this._friction;
4646
this.rollingFriction = this._rollingFriction;

src/layaAir/laya/d3/physics/PhysicsColliderComponent.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ export class PhysicsColliderComponent extends Component {
8787
protected _collider: ICollider;
8888
/**@internal */
8989
protected _eventsArray: string[];
90+
private _isColliderInit: boolean = false;
91+
92+
declare owner: Sprite3D;
9093

9194
/**
9295
* @en The collider object. Used to access the underlying physics engine's collider object and directly operate on the underlying physics engine's collision characteristics.
@@ -326,29 +329,27 @@ export class PhysicsColliderComponent extends Component {
326329
* @protected
327330
*/
328331
protected _onAdded(): void {
329-
if (!this.owner.scene) {
330-
this.owner.on(Node.EVENT_SET_ACTIVESCENE, this, this._onAdded);
331-
} else {
332-
this.initCollider();
333-
this.owner.off(Node.EVENT_SET_ACTIVESCENE, this, this._onAdded);
334-
}
335-
this.owner.off(Event._Add_Script, this, this._setEventFilter);
336-
this.owner.on(Event._Add_Script, this, this._setEventFilter);
337332
}
338333

339334
/**
340335
* @internal
341336
* @protected
342337
*/
343338
protected _onEnable(): void {
344-
(<Sprite3D>this.owner).transform.on(Event.TRANSFORM_CHANGED, this, this._onTransformChanged);
345-
this._physicsManager = ((<Scene3D>this.owner._scene))._physicsManager;
339+
if (!this._isColliderInit) {
340+
this.initCollider();
341+
this._isColliderInit = true;
342+
}
343+
this.owner.transform.on(Event.TRANSFORM_CHANGED, this, this._onTransformChanged);
344+
this._physicsManager = (<Scene3D>this.owner._scene)._physicsManager;
346345
//ILaya3D.Physics3D._bullet.btCollisionObject_setContactProcessingThreshold(this._btColliderObject, 0);
347346
this._collider && (this._collider.componentEnable = true);
348347
if (this._colliderShape) {
349348
this._physicsManager.setActiveCollider(this.collider, true);
350349
this._physicsManager.addCollider(this._collider);
351350
}
351+
this.owner.on(Event._Add_Script, this, this._setEventFilter);
352+
this._setEventFilter();
352353
}
353354

354355
/**
@@ -363,16 +364,19 @@ export class PhysicsColliderComponent extends Component {
363364
this._physicsManager.setActiveCollider(this.collider, false);
364365
}
365366
this._physicsManager = null;
367+
368+
this.owner.off(Event._Add_Script, this, this._setEventFilter);
366369
}
367370

368371
/**
369372
* @internal
370373
* @protected
371374
*/
372375
protected _onDestroy() {
373-
this._collider.destroy();
374376
this._colliderShape && this._colliderShape.destroy();
377+
this._collider && this._collider.destroy();
375378
this._collider = null;
379+
this._isColliderInit = false;
376380
this._colliderShape = null;
377381
this._physicsManager = null;
378382
}

src/layaAir/laya/d3/physics/Rigidbody3D.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ export class Rigidbody3D extends PhysicsColliderComponent {
315315
* @internal
316316
* @protected
317317
*/
318-
protected _onAdded(): void {
319-
super._onAdded();
318+
protected _onEnable(): void {
319+
super._onEnable();
320320
this.restitution = this._restitution;
321321
this.friction = this._friction;
322322
this.rollingFriction = this._rollingFriction;

src/layaAir/laya/d3/physics/constraints/ConfigurableConstraint.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export class ConfigurableConstraint extends ConstraintComponent {
310310
get distanceBounciness() {
311311
return this._distanceBounciness;
312312
}
313-
313+
314314
set distanceBounciness(value: number) {
315315
if (value < 0)
316316
return;
@@ -865,6 +865,7 @@ export class ConfigurableConstraint extends ConstraintComponent {
865865
* @protected
866866
*/
867867
protected _onEnable(): void {
868+
super._onEnable();
868869
if (this._joint)
869870
this._joint.isEnable(true);
870871
}
@@ -874,6 +875,7 @@ export class ConfigurableConstraint extends ConstraintComponent {
874875
* @protected
875876
*/
876877
protected _onDisable(): void {
878+
super._onDisable();
877879
if (this._joint)
878880
this._joint.isEnable(false);
879881
}

src/layaAir/laya/d3/physics/constraints/ConstraintComponent.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class ConstraintComponent extends Component {
2727
protected _ownColliderLocalPos: Vector3 = new Vector3();
2828
/**@internal @protected */
2929
protected _connectColliderLocalPos: Vector3 = new Vector3();
30-
30+
private _isJointInit: boolean = false;
3131
/**
3232
* @en Initializes the joint instance.
3333
* @zh 初始化关节实例。
@@ -187,12 +187,6 @@ export class ConstraintComponent extends Component {
187187
* @protected
188188
*/
189189
protected _onAdded(): void {
190-
if (!this.owner.scene) {
191-
this.owner.on(Node.EVENT_SET_ACTIVESCENE, this, this._onAdded);
192-
} else {
193-
this.initJoint();
194-
this.owner.off(Node.EVENT_SET_ACTIVESCENE, this, this._onAdded);
195-
}
196190
}
197191

198192
/**
@@ -215,12 +209,32 @@ export class ConstraintComponent extends Component {
215209
//TODO
216210
}
217211

212+
/**
213+
* @internal
214+
* @protected
215+
*/
216+
protected _onEnable(): void {
217+
if (!this._isJointInit) {
218+
this.initJoint();
219+
this._isJointInit = true;
220+
}
221+
}
222+
223+
224+
/**
225+
* @internal
226+
* @protected
227+
*/
228+
protected _onDisable(): void {
229+
}
230+
218231
/**
219232
* @internal
220233
* @protected
221234
*/
222235
protected _onDestroy() {
223-
this._joint.destroy();
236+
this._joint && this._joint.destroy();
237+
this._isJointInit = false;
224238
}
225239

226240
/**

src/layaAir/laya/d3/physics/constraints/FixedConstraint.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class FixedConstraint extends ConstraintComponent {
3333
* @protected
3434
*/
3535
protected _onEnable(): void {
36+
super._onEnable();
3637
if (this._joint)
3738
this._joint.isEnable(true);
3839
}
@@ -42,6 +43,7 @@ export class FixedConstraint extends ConstraintComponent {
4243
* @protected
4344
*/
4445
protected _onDisable(): void {
46+
super._onDisable();
4547
if (this._joint)
4648
this._joint.isEnable(false);
4749
}

src/layaAir/laya/d3/physics/constraints/HingeConstraint.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export class HingeConstraint extends ConstraintComponent {
6969
* overrid it
7070
*/
7171
protected _onEnable(): void {
72+
super._onEnable();
7273
if (this._joint)
7374
this._joint.isEnable(true);
7475
}
@@ -79,6 +80,7 @@ export class HingeConstraint extends ConstraintComponent {
7980
* overrid it
8081
*/
8182
protected _onDisable(): void {
83+
super._onDisable();
8284
if (this._joint)
8385
this._joint.isEnable(false);
8486
}

0 commit comments

Comments
 (0)