From cf180d57d900adbb7ef9f3d8db21512d4a5df5a3 Mon Sep 17 00:00:00 2001 From: Leslie Leigh Date: Wed, 2 Jan 2019 16:51:01 +0800 Subject: [PATCH] do not run phy system in editor;support center of phy shape;fix setWorldPosition() bug (#3690) * * do not run phy system in editor * support center of phy shape * fix setWorldPosition() bug * add interface pullTransform() for sync dynamic phy body's transform --- cocos/3d/framework/physics/body.ts | 64 +++++++++++++++---- .../framework/physics/collider-component.ts | 4 +- cocos/3d/framework/physics/physics-system.ts | 3 + .../framework/physics/rigid-body-component.ts | 6 ++ cocos/scene-graph/node.js | 2 +- 5 files changed, 63 insertions(+), 16 deletions(-) diff --git a/cocos/3d/framework/physics/body.ts b/cocos/3d/framework/physics/body.ts index 93203c8691..ee6baedf2a 100644 --- a/cocos/3d/framework/physics/body.ts +++ b/cocos/3d/framework/physics/body.ts @@ -4,6 +4,7 @@ import { vec3 } from '../../../core/vmath'; import Node from '../../../scene-graph/node'; import { PhysicsMaterial as PhysicsMaterial } from '../../assets/physics/material'; import { setWrap, getWrap } from './util'; +import { Quat } from '../../../core/value-types'; export enum DataFlow { PUSHING, @@ -108,13 +109,6 @@ export class PhysicsBody { throw new Error(`shape not found.`); } - public setCenter(shape: PhysicsShape, center: Vec3) { - const iShape = this._cannonBody.shapes.indexOf(shape._getCannonShape()); - if (iShape >= 0) { - this._cannonBody.shapeOffsets[iShape].set(center.x, center.y, center.z); - } - } - get velocity() { return this._cannonBody.velocity; } @@ -233,6 +227,26 @@ export class PhysicsBody { this._cannonBody.position.set(position.x, position.y, position.z); } + public setWorldScale(scale: Vec3) { + this._pullScale(scale); + } + + public setWorldRotation(rotation: Quat) { + this._cannonBody.quaternion.set(rotation.x, rotation.y, rotation.z, rotation.w); + } + + public pullTransform() { + if (!this._node) { + return; + } + // @ts-ignore + this.setWorldPosition(this._node.getWorldPosition()); + // @ts-ignore + this.setWorldScale(this._node.getWorldScale()); + // @ts-ignore + this.setWorldRotation(this._node.getWorldRotation()); + } + /** * Is this body currently in contact with the specified body? * @param {CannonBody} body The body to test against. @@ -278,16 +292,30 @@ export class PhysicsBody { return; } - // @ts-nocheck - this._node.getWorldPosition(this._cannonBody.position); - // @ts-nocheck - this._node.getWorldRotation(this._cannonBody.quaternion); - const scale = this._node.getWorldScale(); + this.pullTransform(); + } + + private _pullScale(scale: cc.Vec3) { let shapeUpdated = false; this._shapes.forEach((shape) => { + let calcShapeOffset = false; + if (shape._centerChanged) { + shape._centerChanged = false; + calcShapeOffset = true; + } + if (!vec3.exactEquals(scale, shape.scale)) { shape.scale = scale; shapeUpdated = true; + calcShapeOffset = true; + } + + if (calcShapeOffset) { + const iShape = this._cannonBody.shapes.findIndex((cannonShape) => cannonShape === shape._getCannonShape()); + if (iShape >= 0) { + const shapeOffset = this._cannonBody.shapeOffsets[iShape]; + vec3.multiply(shapeOffset, shape.center, scale); + } } }); if (shapeUpdated) { @@ -333,6 +361,18 @@ export class PhysicsShape { private _scale: cc.Vec3 = new cc.Vec3(1.0, 1.0, 1.0); + private _center: cc.Vec3 = new cc.Vec3(0, 0, 0); + + public _centerChanged = true; + + public get center() { + return this._center; + } + + public set center(value) { + vec3.copy(this._center, value); + } + constructor(private _cannonShape: CANNON.Shape) { setWrap(this._cannonShape, this); } diff --git a/cocos/3d/framework/physics/collider-component.ts b/cocos/3d/framework/physics/collider-component.ts index 06e6d1d527..132ecd0508 100644 --- a/cocos/3d/framework/physics/collider-component.ts +++ b/cocos/3d/framework/physics/collider-component.ts @@ -49,9 +49,7 @@ export class ColliderComponentBase extends PhysicsBasedComponent { set center(value: Vec3) { this._center = new Vec3(value.x, value.y, value.z); - if (this._body) { - this._body.setCenter(this._shape, this._center); - } + this._shape.center = value; } } diff --git a/cocos/3d/framework/physics/physics-system.ts b/cocos/3d/framework/physics/physics-system.ts index 1a818e85f0..1908089ace 100644 --- a/cocos/3d/framework/physics/physics-system.ts +++ b/cocos/3d/framework/physics/physics-system.ts @@ -8,6 +8,9 @@ export class PhysicsSystem { } public update(deltaTime: number) { + if (CC_EDITOR) { + return; + } this._world.step(deltaTime); } diff --git a/cocos/3d/framework/physics/rigid-body-component.ts b/cocos/3d/framework/physics/rigid-body-component.ts index 0d5b1f8249..37309ba6aa 100644 --- a/cocos/3d/framework/physics/rigid-body-component.ts +++ b/cocos/3d/framework/physics/rigid-body-component.ts @@ -99,6 +99,12 @@ export class RigidBodyComponent extends PhysicsBasedComponent { this._body.angularDamping = value; } } + + public pullTransform() { + if (this._body) { + this._body.pullTransform(); + } + } } const NonRigidBodyProperties = { diff --git a/cocos/scene-graph/node.js b/cocos/scene-graph/node.js index 3e756491bd..4a081d8586 100644 --- a/cocos/scene-graph/node.js +++ b/cocos/scene-graph/node.js @@ -304,7 +304,7 @@ class Node extends BaseNode { parent.updateWorldTransform(); vec3.sub(local, this._pos, parent._pos); vec3.transformQuat(local, local, quat.conjugate(q_a, parent._rot)); - vec3.div(cc.v3(), local, parent._scale); + vec3.div(local, local, parent._scale); } else { vec3.copy(this._lpos, this._pos); }