From 4f1c5d80111768db6c268feabda498707eaf7569 Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Thu, 16 Jan 2025 21:33:00 +0800 Subject: [PATCH 1/4] fix: axis not effect when component changed --- packages/core/src/physics/joint/HingeJoint.ts | 15 +++++++++++++-- .../physics-physx/src/joint/PhysXHingeJoint.ts | 1 - tests/src/core/physics/HingeJoint.test.ts | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/core/src/physics/joint/HingeJoint.ts b/packages/core/src/physics/joint/HingeJoint.ts index 31054b52f1..74f65da048 100644 --- a/packages/core/src/physics/joint/HingeJoint.ts +++ b/packages/core/src/physics/joint/HingeJoint.ts @@ -25,7 +25,7 @@ export class HingeJoint extends Joint { private _velocity = 0; /** - * The anchor rotation. + * The Direction of the axis around which the hingeJoint. */ get axis(): Vector3 { return this._axis; @@ -35,7 +35,6 @@ export class HingeJoint extends Joint { const axis = this._axis; if (value !== axis) { axis.copyFrom(value); - (this._nativeJoint)?.setAxis(axis); } } @@ -143,6 +142,8 @@ export class HingeJoint extends Joint { super(entity); this._onMotorChanged = this._onMotorChanged.bind(this); this._onLimitsChanged = this._onLimitsChanged.bind(this); + //@ts-ignore + this._axis._onValueChanged = this._setAxis.bind(this); } /** @@ -197,4 +198,14 @@ export class HingeJoint extends Joint { } } } + + @ignoreClone + private _setAxis(): void { + //@ts-ignore + this._axis._onValueChanged = null; + this._axis.normalize(); + (this._nativeJoint)?.setAxis(this._axis); + //@ts-ignore + this._axis._onValueChanged = this._setAxis.bind(this); + } } diff --git a/packages/physics-physx/src/joint/PhysXHingeJoint.ts b/packages/physics-physx/src/joint/PhysXHingeJoint.ts index 7addf168dc..77f71edc8a 100644 --- a/packages/physics-physx/src/joint/PhysXHingeJoint.ts +++ b/packages/physics-physx/src/joint/PhysXHingeJoint.ts @@ -34,7 +34,6 @@ export class PhysXHingeJoint extends PhysXJoint implements IHingeJoint { const xAxis = PhysXHingeJoint._xAxis; const axisRotationQuaternion = this._axisRotationQuaternion; xAxis.set(1, 0, 0); - value.normalize(); const angle = Math.acos(Vector3.dot(xAxis, value)); Vector3.cross(xAxis, value, xAxis); Quaternion.rotationAxisAngle(xAxis, angle, axisRotationQuaternion); diff --git a/tests/src/core/physics/HingeJoint.test.ts b/tests/src/core/physics/HingeJoint.test.ts index d6f1ad12d8..4e55ab8e14 100644 --- a/tests/src/core/physics/HingeJoint.test.ts +++ b/tests/src/core/physics/HingeJoint.test.ts @@ -60,8 +60,8 @@ describe("HingeJoint", function () { joint.automaticConnectedAnchor = true; joint.connectedCollider = boxEntity2.getComponent(DynamicCollider); joint.anchor = new Vector3(0.5, 0, 0); - joint.axis = new Vector3(0, 1, 0); - + joint.axis.x = 0; + joint.axis.y = 1; expect(formatValue(joint.angle)).eq(0); collider2.applyTorque(new Vector3(0, 1000, 0)); From c4f60ecae157e92c24c7f436e3675c99a3d2a947 Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Thu, 16 Jan 2025 23:49:44 +0800 Subject: [PATCH 2/4] fix: set connectedAnchor should not effect when set automaticConnectedAnchor --- packages/core/src/physics/joint/HingeJoint.ts | 7 +++-- packages/core/src/physics/joint/Joint.ts | 31 ++++++++++++------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/core/src/physics/joint/HingeJoint.ts b/packages/core/src/physics/joint/HingeJoint.ts index 74f65da048..99fe911bb6 100644 --- a/packages/core/src/physics/joint/HingeJoint.ts +++ b/packages/core/src/physics/joint/HingeJoint.ts @@ -142,8 +142,9 @@ export class HingeJoint extends Joint { super(entity); this._onMotorChanged = this._onMotorChanged.bind(this); this._onLimitsChanged = this._onLimitsChanged.bind(this); + this._onAxisChanged = this._onAxisChanged.bind(this); //@ts-ignore - this._axis._onValueChanged = this._setAxis.bind(this); + this._axis._onValueChanged = this._onAxisChanged; } /** @@ -200,12 +201,12 @@ export class HingeJoint extends Joint { } @ignoreClone - private _setAxis(): void { + private _onAxisChanged(): void { //@ts-ignore this._axis._onValueChanged = null; this._axis.normalize(); (this._nativeJoint)?.setAxis(this._axis); //@ts-ignore - this._axis._onValueChanged = this._setAxis.bind(this); + this._axis._onValueChanged = this._onAxisChanged; } } diff --git a/packages/core/src/physics/joint/Joint.ts b/packages/core/src/physics/joint/Joint.ts index fbc9c3f4f7..450202f3b3 100644 --- a/packages/core/src/physics/joint/Joint.ts +++ b/packages/core/src/physics/joint/Joint.ts @@ -25,8 +25,6 @@ export abstract class Joint extends Component { private _force = Infinity; private _torque = Infinity; private _automaticConnectedAnchor = true; - @ignoreClone - private _updateConnectedActualAnchor: Function; /** * The connected collider. @@ -76,22 +74,17 @@ export abstract class Joint extends Component { if (this._automaticConnectedAnchor) { //@ts-ignore connectedColliderAnchor._onValueChanged = null; - this._calculateConnectedAnchor(); + this._calculateConnectedAnchor(false); //@ts-ignore - connectedColliderAnchor._onValueChanged = this._updateConnectedActualAnchor; + connectedColliderAnchor._onValueChanged = this._handleConnectedAnchorChanged; } return connectedColliderAnchor; } set connectedAnchor(value: Vector3) { - if (this._automaticConnectedAnchor) { - console.warn("Cannot set connectedAnchor when automaticConnectedAnchor is true."); - return; - } const connectedAnchor = this._connectedColliderInfo.anchor; if (value !== connectedAnchor) { connectedAnchor.copyFrom(value); - this._updateActualAnchor(AnchorOwner.Connected); } } @@ -195,9 +188,9 @@ export abstract class Joint extends Component { super(entity); //@ts-ignore this._colliderInfo.anchor._onValueChanged = this._updateActualAnchor.bind(this, AnchorOwner.Self); - this._updateConnectedActualAnchor = this._updateActualAnchor.bind(this, AnchorOwner.Connected); + this._handleConnectedAnchorChanged = this._handleConnectedAnchorChanged.bind(this); //@ts-ignore - this._connectedColliderInfo.anchor._onValueChanged = this._updateConnectedActualAnchor; + this._connectedColliderInfo.anchor._onValueChanged = this._handleConnectedAnchorChanged.bind(this); this._onSelfTransformChanged = this._onSelfTransformChanged.bind(this); this._onConnectedTransformChanged = this._onConnectedTransformChanged.bind(this); @@ -241,7 +234,7 @@ export abstract class Joint extends Component { } } - private _calculateConnectedAnchor(): void { + private _calculateConnectedAnchor(syncNative: boolean = true): void { const colliderInfo = this._colliderInfo; const connectedColliderInfo = this._connectedColliderInfo; const { worldPosition: selfPos } = this.entity.transform; @@ -250,6 +243,8 @@ export abstract class Joint extends Component { const connectedActualAnchor = connectedColliderInfo.actualAnchor; const connectedCollider = connectedColliderInfo.collider; + // @ts-ignore + connectedAnchor._onValueChanged = null; if (connectedCollider) { const { worldPosition: connectedPos, lossyWorldScale: connectedWorldScale } = connectedCollider.entity.transform; Vector3.subtract(selfPos, connectedPos, Joint._tempVector3); @@ -259,6 +254,18 @@ export abstract class Joint extends Component { Vector3.add(selfPos, selfActualAnchor, connectedActualAnchor); connectedAnchor.copyFrom(connectedActualAnchor); } + // @ts-ignore + connectedAnchor._onValueChanged = this._handleConnectedAnchorChanged; + syncNative && this._updateActualAnchor(AnchorOwner.Connected); + } + + @ignoreClone + private _handleConnectedAnchorChanged(): void { + if (this._automaticConnectedAnchor) { + console.warn("Cannot set connectedAnchor when automaticConnectedAnchor is true."); + } else { + this._updateActualAnchor(AnchorOwner.Connected); + } } @ignoreClone From 02f961a6899d381448aa99be712e678ebc5e9f5c Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Thu, 16 Jan 2025 23:56:32 +0800 Subject: [PATCH 3/4] feat: add ut --- tests/src/core/physics/Joint.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/src/core/physics/Joint.test.ts b/tests/src/core/physics/Joint.test.ts index 0d5c59c4f5..de57d77650 100644 --- a/tests/src/core/physics/Joint.test.ts +++ b/tests/src/core/physics/Joint.test.ts @@ -95,6 +95,9 @@ describe("Joint", function () { joint.automaticConnectedAnchor = true; joint.connectedAnchor = new Vector3(1, 1, 1); expect(consoleWarnSpy).toBeCalledTimes(1); + joint.connectedAnchor.y = 3; + expect(consoleWarnSpy).toBeCalledTimes(2); + expect(joint.connectedAnchor).deep.include({ x: 4, y: 4, z: 4 }); // @ts-ignore engine.sceneManager.activeScene.physics._update(1 / 60); From 11201a7de2b82edf9f5d064d6ca0d43eebde090e Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Fri, 17 Jan 2025 14:47:51 +0800 Subject: [PATCH 4/4] refactor: opt code --- packages/core/src/physics/joint/Joint.ts | 14 +++----------- tests/src/core/physics/Joint.test.ts | 2 ++ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/packages/core/src/physics/joint/Joint.ts b/packages/core/src/physics/joint/Joint.ts index 450202f3b3..ae7d90b866 100644 --- a/packages/core/src/physics/joint/Joint.ts +++ b/packages/core/src/physics/joint/Joint.ts @@ -70,15 +70,7 @@ export abstract class Joint extends Component { * The connectedAnchor is automatically calculated, if you want to set it manually, please set automaticConnectedAnchor to false */ get connectedAnchor(): Vector3 { - const connectedColliderAnchor = this._connectedColliderInfo.anchor; - if (this._automaticConnectedAnchor) { - //@ts-ignore - connectedColliderAnchor._onValueChanged = null; - this._calculateConnectedAnchor(false); - //@ts-ignore - connectedColliderAnchor._onValueChanged = this._handleConnectedAnchorChanged; - } - return connectedColliderAnchor; + return this._connectedColliderInfo.anchor; } set connectedAnchor(value: Vector3) { @@ -234,7 +226,7 @@ export abstract class Joint extends Component { } } - private _calculateConnectedAnchor(syncNative: boolean = true): void { + private _calculateConnectedAnchor(): void { const colliderInfo = this._colliderInfo; const connectedColliderInfo = this._connectedColliderInfo; const { worldPosition: selfPos } = this.entity.transform; @@ -256,7 +248,7 @@ export abstract class Joint extends Component { } // @ts-ignore connectedAnchor._onValueChanged = this._handleConnectedAnchorChanged; - syncNative && this._updateActualAnchor(AnchorOwner.Connected); + this._updateActualAnchor(AnchorOwner.Connected); } @ignoreClone diff --git a/tests/src/core/physics/Joint.test.ts b/tests/src/core/physics/Joint.test.ts index de57d77650..e6746fec91 100644 --- a/tests/src/core/physics/Joint.test.ts +++ b/tests/src/core/physics/Joint.test.ts @@ -97,6 +97,8 @@ describe("Joint", function () { expect(consoleWarnSpy).toBeCalledTimes(1); joint.connectedAnchor.y = 3; expect(consoleWarnSpy).toBeCalledTimes(2); + joint.automaticConnectedAnchor = false; + joint.automaticConnectedAnchor = true; expect(joint.connectedAnchor).deep.include({ x: 4, y: 4, z: 4 }); // @ts-ignore