mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Fix collider & rigid body component enabled property (#4249)
This commit is contained in:
@@ -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组件
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user