mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Fix collider sync bug, add shape to body before enable body (#4753)
* add shape to body before enable body, when enable body will sync the body * sync node after add shape to body
This commit is contained in:
@@ -201,9 +201,9 @@ export class ColliderComponent extends PhysicsBasedComponent implements IEventTa
|
||||
}
|
||||
|
||||
protected onEnable () {
|
||||
super.onEnable();
|
||||
|
||||
if (!CC_EDITOR) {
|
||||
super.onEnable();
|
||||
|
||||
const rigidBody = this.sharedBody.rigidBody as RigidBodyComponent | null;
|
||||
if (rigidBody != null) {
|
||||
@@ -218,6 +218,8 @@ export class ColliderComponent extends PhysicsBasedComponent implements IEventTa
|
||||
if (!CC_PHYSICS_BUILT_IN) {
|
||||
this.sharedBody.body.addCollisionCallback(this._collisionCallBack);
|
||||
}
|
||||
// sync after add shape
|
||||
this.sharedBody.syncPhysWithScene();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -247,8 +247,6 @@ class SharedRigidBody {
|
||||
|
||||
private _afterStepCallback!: AfterStepCallback;
|
||||
|
||||
private _transformInitialized: boolean = false;
|
||||
|
||||
/** 是否只有Collider组件 */
|
||||
private _isShapeOnly: boolean = true;
|
||||
|
||||
@@ -316,7 +314,11 @@ class SharedRigidBody {
|
||||
}
|
||||
}
|
||||
|
||||
public syncPhysWithScene (node: Node) {
|
||||
public syncPhysWithScene (){
|
||||
this._syncPhysWithScene(this._node);
|
||||
}
|
||||
|
||||
private _syncPhysWithScene (node: Node) {
|
||||
// sync position rotation
|
||||
node.getWorldMatrix(SharedRigidBody._tempMat4);
|
||||
node.getWorldRotation(SharedRigidBody._tempQuat);
|
||||
@@ -340,11 +342,8 @@ class SharedRigidBody {
|
||||
}
|
||||
|
||||
private _activeBody () {
|
||||
// 是否为第一次激活
|
||||
if (!this._transformInitialized) {
|
||||
this._transformInitialized = true;
|
||||
this.syncPhysWithScene(this._node);
|
||||
}
|
||||
// Sync scenes, no matter how many activations
|
||||
this._syncPhysWithScene(this._node);
|
||||
|
||||
if (this._actived) {
|
||||
return;
|
||||
@@ -383,7 +382,7 @@ class SharedRigidBody {
|
||||
this._body.scaleAllShapes(this._node.worldScale);
|
||||
vec3.copy(this._prevScale, this._node.worldScale);
|
||||
}
|
||||
this.syncPhysWithScene(this._node);
|
||||
this._syncPhysWithScene(this._node);
|
||||
|
||||
if (!CC_PHYSICS_BUILT_IN) {
|
||||
if (this._body.isSleeping()) {
|
||||
@@ -402,7 +401,7 @@ class SharedRigidBody {
|
||||
// 对于只有形状组件的节点,需要将Scene中节点的Transform同步到Phyisc。
|
||||
// 这是因为物理计算后可能会改变一些节点,这会导致这些子节点的Transform也发生改变。
|
||||
if (this._node.hasChanged) {
|
||||
this.syncPhysWithScene(this._node);
|
||||
this._syncPhysWithScene(this._node);
|
||||
|
||||
if (this._body.isSleeping()) {
|
||||
this._body.wakeUp();
|
||||
|
||||
Reference in New Issue
Block a user