mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
fix phys comp removal error (#3943)
* collider removal * bug fix * fix name
This commit is contained in:
committed by
Weiliang Wang
parent
6f1ca348e6
commit
8f9e5534d1
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -36,6 +36,7 @@ export class RaycastResult {
|
||||
return getWrap<Node>(this._cannonResult.body);
|
||||
}
|
||||
}
|
||||
cc.RaycastResult = RaycastResult;
|
||||
|
||||
export class PhysicsWorld {
|
||||
private _cannonWorld: CANNON.World;
|
||||
|
||||
Reference in New Issue
Block a user