From 7e71494806a67fcf947bee2dfe5eb6f22651f054 Mon Sep 17 00:00:00 2001 From: "jiaxin.lai" Date: Fri, 19 Apr 2019 15:25:38 +0800 Subject: [PATCH] Fix collider & rigid body component enabled property (#4249) --- .../framework/physics/collider-component.ts | 2 + .../physics/detail/physics-based-component.ts | 51 +++++++++++++------ .../framework/physics/rigid-body-component.ts | 20 +++----- 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/cocos/3d/framework/physics/collider-component.ts b/cocos/3d/framework/physics/collider-component.ts index 3f26d89e04..69de4169dd 100644 --- a/cocos/3d/framework/physics/collider-component.ts +++ b/cocos/3d/framework/physics/collider-component.ts @@ -74,6 +74,7 @@ export class ColliderComponentBase extends PhysicsBasedComponent { public onEnable () { if (this.sharedBody) { + super.onEnable(); this.sharedBody.body.addShape(this._shapeBase!); } } @@ -81,6 +82,7 @@ export class ColliderComponentBase extends PhysicsBasedComponent { public onDisable () { if (this.sharedBody) { this.sharedBody.body.removeShape(this._shapeBase!); + // collider只需要从刚体中移除shape即可, 因为可能存在有rigidBody组件 } } diff --git a/cocos/3d/framework/physics/detail/physics-based-component.ts b/cocos/3d/framework/physics/detail/physics-based-component.ts index 0e2f255e04..f5c19a927b 100644 --- a/cocos/3d/framework/physics/detail/physics-based-component.ts +++ b/cocos/3d/framework/physics/detail/physics-based-component.ts @@ -4,7 +4,7 @@ import { Quat, Vec3 } from '../../../../core/value-types'; import { quat, vec3 } from '../../../../core/vmath'; import { Node } from '../../../../scene-graph/node'; import { AfterStepCallback, BeforeStepCallback, ICollisionCallback, ICollisionEvent, PhysicsWorldBase, RigidBodyBase } from '../../../physics/api'; -import { createRigidBody} from '../../../physics/instance'; +import { createRigidBody } from '../../../physics/instance'; import { ERigidBodyType, ETransformSource } from '../../../physics/physic-enum'; import { stringfyQuat, stringfyVec3 } from '../../../physics/util'; @@ -27,6 +27,15 @@ export class PhysicsBasedComponent extends Component { this._refSharedBody(); } + public onEnable () { + // collider和rigidbody都需要激活刚体 + this.sharedBody!.enable(); + } + + public onDisable () { + this.sharedBody!.disable(); + } + public destroy () { if (this._sharedBody) { this._sharedBody.deref(); @@ -105,16 +114,18 @@ class SharedRigidBody { } public ref () { - if (!this._refCount) { - this._activeBody(); - } + // if (!this._refCount) { + // this._activeBody(); + // } ++this._refCount; } public deref () { --this._refCount; if (!this._refCount) { - this._deactiveBody(); + // this._deactiveBody(); + // TODO : destroy this body + // this.destroy(); } } @@ -160,15 +171,21 @@ class SharedRigidBody { } private _activeBody () { + // 是否为第一次激活 if (!this._transformInitialized) { - return; + this._transformInitialized = true; + this.syncPhysWithScene(this._node); } + if (this._actived) { return; } + this._actived = true; this._body.setWorld(this._world); + // this._world.addBeforeStep(this._beforeStepCallback); this._world.addAfterStep(this._afterStepCallback); + this._body.wakeUp(); } private _deactiveBody () { @@ -176,8 +193,9 @@ class SharedRigidBody { return; } this._actived = false; - this._world.removeAfterStep(this._afterStepCallback); this._world.removeBeforeStep(this._beforeStepCallback); + this._world.removeAfterStep(this._afterStepCallback); + this._body.sleep(); this._body.setWorld(null); } @@ -198,16 +216,17 @@ class SharedRigidBody { // `Rotation: ${stringfyQuat(this._node.getWorldRotation())} `); // }; - if (!this._transformInitialized) { - // d(`Initialize`); - this._transformInitialized = true; + // if (!this._transformInitialized) { + // // d(`Initialize`); + // this._transformInitialized = true; + // this.syncPhysWithScene(this._node); + // this._activeBody(); + // } else { + // } + + // 开始物理计算之前,用户脚本或引擎功能有可能改变节点的Transform,所以需要判断并进行更新 + if (this._node.hasChanged) { this.syncPhysWithScene(this._node); - this._activeBody(); - } else { - // 开始物理计算之前,用户脚本或引擎功能有可能改变节点的Transform,所以需要判断并进行更新 - if (this._node.hasChanged) { - this.syncPhysWithScene(this._node); - } } // if (!this.body.isPhysicsManagedTransform() && this._node.hasChanged) { diff --git a/cocos/3d/framework/physics/rigid-body-component.ts b/cocos/3d/framework/physics/rigid-body-component.ts index 95f65cf554..325740195f 100644 --- a/cocos/3d/framework/physics/rigid-body-component.ts +++ b/cocos/3d/framework/physics/rigid-body-component.ts @@ -69,18 +69,6 @@ export class RigidBodyComponent extends PhysicsBasedComponent { } } - public onEnable () { - if (this.sharedBody) { - this.sharedBody.body.wakeUp(); - } - } - - public onDisable () { - if (this.sharedBody) { - this.sharedBody.body.sleep(); - } - } - @property({ type: PhysicsMaterial, }) @@ -200,6 +188,14 @@ export class RigidBodyComponent extends PhysicsBasedComponent { this._body!.applyImpulse(impulse, position); } + public wakeUp () { + this._body!.wakeUp(); + } + + public sleep () { + this._body!.sleep(); + } + public setCollisionFilter (group: number, mask: number) { this._body!.setCollisionFilter(group, mask); }