diff --git a/cocos/3d/assets/physics/material.ts b/cocos/3d/assets/physics/material.ts index 31e2f4a988..788dc97e9c 100644 --- a/cocos/3d/assets/physics/material.ts +++ b/cocos/3d/assets/physics/material.ts @@ -5,7 +5,7 @@ import CANNON from 'cannon'; import { Asset } from '../../../assets/asset'; import { ccclass, property } from '../../../core/data/class-decorator'; -@ccclass('cc.PhysicalMaterial') +@ccclass('cc.PhysicsMaterial') export class PhysicsMaterial extends Asset { @property public _friction = -1; diff --git a/cocos/3d/framework/physics/collider-component.ts b/cocos/3d/framework/physics/collider-component.ts index abe31d6571..607a2a09d2 100644 --- a/cocos/3d/framework/physics/collider-component.ts +++ b/cocos/3d/framework/physics/collider-component.ts @@ -42,8 +42,14 @@ export class ColliderComponentBase extends PhysicsBasedComponent { } public onDisable () { - // throw new Error(`Not impl!`); - // this._body!.removeShape(this._impl); + this.sharedBody!.removeCollider(this); + } + + public destroy () { + if (this.sharedBody) { + this.sharedBody.removeCollider(this); + } + super.destroy(); } } diff --git a/cocos/3d/framework/physics/detail/physics-based-component.ts b/cocos/3d/framework/physics/detail/physics-based-component.ts index 2e634dafc6..53c58e02e0 100644 --- a/cocos/3d/framework/physics/detail/physics-based-component.ts +++ b/cocos/3d/framework/physics/detail/physics-based-component.ts @@ -183,6 +183,15 @@ class SharedRigidBody { this.body.updateBoundingRadius(); } + public removeCollider (collider: Collider) { + const ishape = this._colliders.get(collider); + if (ishape === undefined) { + return; + } + this._colliders.delete(collider); + this._removeShape(ishape); + } + public syncPhysWithScene () { vec3.copy(this._body.position, this._node.getWorldPosition()); quat.copy(this._body.quaternion, this._node.getWorldRotation()); @@ -226,10 +235,10 @@ class SharedRigidBody { } private _deactiveBody () { - this._world.removeBody(this._body); this._body.removeEventListener('collide', this._onCollidedListener); this._body.world.removeEventListener('postStep', this._onWorldPostStepListener); this._body.world.removeEventListener('beforeStep', this._onWorldBeforeStepListener); + this._world.removeBody(this._body); } private _onCollided (event: CANNON.ICollisionEvent) { @@ -251,6 +260,18 @@ class SharedRigidBody { this._syncSceneWithPhys(); } } + + private _removeShape (iShape: number) { + const body = this._body; + const shape = body.shapes[iShape]; + body.shapes.splice(iShape, 1); + body.shapeOffsets.splice(iShape, 1); + body.shapeOrientations.splice(iShape, 1); + body.updateMassProperties(); + body.updateBoundingRadius(); + body.aabbNeedsUpdate = true; + (shape as any).body = null; + } } function maxComponent (v: Vec3) { diff --git a/cocos/3d/framework/physics/world.ts b/cocos/3d/framework/physics/world.ts index ca2eac259c..41218b2e3f 100644 --- a/cocos/3d/framework/physics/world.ts +++ b/cocos/3d/framework/physics/world.ts @@ -36,6 +36,7 @@ export class RaycastResult { return getWrap(this._cannonResult.body); } } +cc.RaycastResult = RaycastResult; export class PhysicsWorld { private _cannonWorld: CANNON.World;