From 2aa125974dfd6c8c96bc90ce6a107ac05ec2a461 Mon Sep 17 00:00:00 2001 From: "jiaxin.lai" Date: Sat, 29 Jun 2019 12:16:48 +0800 Subject: [PATCH] Fix collider sync bug, add shape to body before enable body (#4753) * add shape to body before enable body, when enable body will sync the body * sync node after add shape to body --- .../physics/collider/collider-component.ts | 4 +++- .../physics/detail/physics-based-component.ts | 19 +++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/cocos/3d/framework/physics/collider/collider-component.ts b/cocos/3d/framework/physics/collider/collider-component.ts index 8cd58e12a3..e3213a8abb 100644 --- a/cocos/3d/framework/physics/collider/collider-component.ts +++ b/cocos/3d/framework/physics/collider/collider-component.ts @@ -201,9 +201,9 @@ export class ColliderComponent extends PhysicsBasedComponent implements IEventTa } protected onEnable () { - super.onEnable(); if (!CC_EDITOR) { + super.onEnable(); const rigidBody = this.sharedBody.rigidBody as RigidBodyComponent | null; if (rigidBody != null) { @@ -218,6 +218,8 @@ export class ColliderComponent extends PhysicsBasedComponent implements IEventTa if (!CC_PHYSICS_BUILT_IN) { this.sharedBody.body.addCollisionCallback(this._collisionCallBack); } + // sync after add shape + this.sharedBody.syncPhysWithScene(); } } diff --git a/cocos/3d/framework/physics/detail/physics-based-component.ts b/cocos/3d/framework/physics/detail/physics-based-component.ts index e533a6d189..2f7d2feddf 100644 --- a/cocos/3d/framework/physics/detail/physics-based-component.ts +++ b/cocos/3d/framework/physics/detail/physics-based-component.ts @@ -247,8 +247,6 @@ class SharedRigidBody { private _afterStepCallback!: AfterStepCallback; - private _transformInitialized: boolean = false; - /** 是否只有Collider组件 */ private _isShapeOnly: boolean = true; @@ -316,7 +314,11 @@ class SharedRigidBody { } } - public syncPhysWithScene (node: Node) { + public syncPhysWithScene (){ + this._syncPhysWithScene(this._node); + } + + private _syncPhysWithScene (node: Node) { // sync position rotation node.getWorldMatrix(SharedRigidBody._tempMat4); node.getWorldRotation(SharedRigidBody._tempQuat); @@ -340,11 +342,8 @@ class SharedRigidBody { } private _activeBody () { - // 是否为第一次激活 - if (!this._transformInitialized) { - this._transformInitialized = true; - this.syncPhysWithScene(this._node); - } + // Sync scenes, no matter how many activations + this._syncPhysWithScene(this._node); if (this._actived) { return; @@ -383,7 +382,7 @@ class SharedRigidBody { this._body.scaleAllShapes(this._node.worldScale); vec3.copy(this._prevScale, this._node.worldScale); } - this.syncPhysWithScene(this._node); + this._syncPhysWithScene(this._node); if (!CC_PHYSICS_BUILT_IN) { if (this._body.isSleeping()) { @@ -402,7 +401,7 @@ class SharedRigidBody { // 对于只有形状组件的节点,需要将Scene中节点的Transform同步到Phyisc。 // 这是因为物理计算后可能会改变一些节点,这会导致这些子节点的Transform也发生改变。 if (this._node.hasChanged) { - this.syncPhysWithScene(this._node); + this._syncPhysWithScene(this._node); if (this._body.isSleeping()) { this._body.wakeUp();