From febd4971d08377a286be95ac7f44afa1dd348fa7 Mon Sep 17 00:00:00 2001 From: "jiaxin.lai" Date: Tue, 31 Mar 2020 16:53:11 +0800 Subject: [PATCH] Fix cannon body aabb update (#6450) * fix cannon body aabb update * tweaks --- cocos/physics/cannon/cannon-shared-body.ts | 12 ++++-------- cocos/physics/cannon/cannon-util.ts | 2 +- cocos/physics/cannon/shapes/cannon-shape.ts | 12 ++++++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cocos/physics/cannon/cannon-shared-body.ts b/cocos/physics/cannon/cannon-shared-body.ts index b32bd8a592..88755e3cbe 100644 --- a/cocos/physics/cannon/cannon-shared-body.ts +++ b/cocos/physics/cannon/cannon-shared-body.ts @@ -9,6 +9,7 @@ import { TransformBit } from '../../core/scene-graph/node-enum'; import { Node } from '../../core'; import { CollisionEventType } from '../framework/physics-interface'; import { CannonRigidBody } from './cannon-rigid-body'; +import { commitShapeUpdates } from './cannon-util'; const v3_0 = new Vec3(); const quat_0 = new Quat(); @@ -63,9 +64,7 @@ export class CannonSharedBody { } } else { if (this.index >= 0) { - // TODO: 待查,组件的 enabledInHierarchy 为什么还是 true const isRemove = (this.shapes.length == 0 && this.wrappedBody == null) || - (this.shapes.length == 0 && this.wrappedBody != null && !this.wrappedBody.rigidBody.enabledInHierarchy) || (this.shapes.length == 0 && this.wrappedBody != null && !this.wrappedBody.isEnabled) if (isRemove) { @@ -115,18 +114,15 @@ export class CannonSharedBody { syncSceneToPhysics () { if (this.node.hasChangedFlags) { - + if (this.body.isSleeping()) this.body.wakeUp(); Vec3.copy(this.body.position, this.node.worldPosition); Quat.copy(this.body.quaternion, this.node.worldRotation); - + this.body.aabbNeedsUpdate = true; if (this.node.hasChangedFlags & TransformBit.SCALE) { for (let i = 0; i < this.shapes.length; i++) { this.shapes[i].setScale(this.node.worldScale); } - } - - if (this.body.isSleeping()) { - this.body.wakeUp(); + commitShapeUpdates(this.body); } } } diff --git a/cocos/physics/cannon/cannon-util.ts b/cocos/physics/cannon/cannon-util.ts index 028cc05f2b..725b71c6cb 100644 --- a/cocos/physics/cannon/cannon-util.ts +++ b/cocos/physics/cannon/cannon-util.ts @@ -22,7 +22,7 @@ export function fillRaycastResult (result: PhysicsRayResult, cannonResult: CANNO } export function commitShapeUpdates (body: CANNON.Body) { + body.aabbNeedsUpdate = true; body.updateMassProperties(); body.updateBoundingRadius(); - body.aabbNeedsUpdate = true; } \ No newline at end of file diff --git a/cocos/physics/cannon/shapes/cannon-shape.ts b/cocos/physics/cannon/shapes/cannon-shape.ts index cd55286057..2cee74eee9 100644 --- a/cocos/physics/cannon/shapes/cannon-shape.ts +++ b/cocos/physics/cannon/shapes/cannon-shape.ts @@ -55,9 +55,7 @@ export class CannonShape implements IBaseShape { } setCenter (v: IVec3Like) { - const lpos = this._offset as IVec3Like; - Vec3.copy(lpos, v); - Vec3.multiply(lpos, lpos, this._collider.node.worldScale); + this._setCenter(v); if (this._index >= 0) { commitShapeUpdates(this._body); } @@ -157,7 +155,7 @@ export class CannonShape implements IBaseShape { * @param scale */ setScale (scale: IVec3Like) { - this.setCenter(this._collider.center); + this._setCenter(this._collider.center); } setIndex (index: number) { @@ -169,6 +167,12 @@ export class CannonShape implements IBaseShape { this._orient = Orient; } + private _setCenter (v: IVec3Like) { + const lpos = this._offset as IVec3Like; + Vec3.copy(lpos, v); + Vec3.multiply(lpos, lpos, this._collider.node.worldScale); + } + private onTrigger (event: CANNON.ITriggeredEvent) { TriggerEventObject.type = event.event; const self = getWrap(event.selfShape);