Skip to content

Commit 6e3ba7e

Browse files
authored
Fix axis not effect when component changed && set connectedAnchor should not effect when automaticConnectedAnchor is true (#2516)
* fix: axis not effect when component changed * fix: set connectedAnchor should not effect when set automaticConnectedAnchor
1 parent 15e7b8b commit 6e3ba7e

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

packages/core/src/physics/joint/HingeJoint.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class HingeJoint extends Joint {
2525
private _velocity = 0;
2626

2727
/**
28-
* The anchor rotation.
28+
* The Direction of the axis around which the hingeJoint.
2929
*/
3030
get axis(): Vector3 {
3131
return this._axis;
@@ -35,7 +35,6 @@ export class HingeJoint extends Joint {
3535
const axis = this._axis;
3636
if (value !== axis) {
3737
axis.copyFrom(value);
38-
(<IHingeJoint>this._nativeJoint)?.setAxis(axis);
3938
}
4039
}
4140

@@ -143,6 +142,9 @@ export class HingeJoint extends Joint {
143142
super(entity);
144143
this._onMotorChanged = this._onMotorChanged.bind(this);
145144
this._onLimitsChanged = this._onLimitsChanged.bind(this);
145+
this._onAxisChanged = this._onAxisChanged.bind(this);
146+
//@ts-ignore
147+
this._axis._onValueChanged = this._onAxisChanged;
146148
}
147149

148150
/**
@@ -197,4 +199,14 @@ export class HingeJoint extends Joint {
197199
}
198200
}
199201
}
202+
203+
@ignoreClone
204+
private _onAxisChanged(): void {
205+
//@ts-ignore
206+
this._axis._onValueChanged = null;
207+
this._axis.normalize();
208+
(<IHingeJoint>this._nativeJoint)?.setAxis(this._axis);
209+
//@ts-ignore
210+
this._axis._onValueChanged = this._onAxisChanged;
211+
}
200212
}

packages/core/src/physics/joint/Joint.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ export abstract class Joint extends Component {
2525
private _force = Infinity;
2626
private _torque = Infinity;
2727
private _automaticConnectedAnchor = true;
28-
@ignoreClone
29-
private _updateConnectedActualAnchor: Function;
3028

3129
/**
3230
* The connected collider.
@@ -72,26 +70,13 @@ export abstract class Joint extends Component {
7270
* The connectedAnchor is automatically calculated, if you want to set it manually, please set automaticConnectedAnchor to false
7371
*/
7472
get connectedAnchor(): Vector3 {
75-
const connectedColliderAnchor = this._connectedColliderInfo.anchor;
76-
if (this._automaticConnectedAnchor) {
77-
//@ts-ignore
78-
connectedColliderAnchor._onValueChanged = null;
79-
this._calculateConnectedAnchor();
80-
//@ts-ignore
81-
connectedColliderAnchor._onValueChanged = this._updateConnectedActualAnchor;
82-
}
83-
return connectedColliderAnchor;
73+
return this._connectedColliderInfo.anchor;
8474
}
8575

8676
set connectedAnchor(value: Vector3) {
87-
if (this._automaticConnectedAnchor) {
88-
console.warn("Cannot set connectedAnchor when automaticConnectedAnchor is true.");
89-
return;
90-
}
9177
const connectedAnchor = this._connectedColliderInfo.anchor;
9278
if (value !== connectedAnchor) {
9379
connectedAnchor.copyFrom(value);
94-
this._updateActualAnchor(AnchorOwner.Connected);
9580
}
9681
}
9782

@@ -195,9 +180,9 @@ export abstract class Joint extends Component {
195180
super(entity);
196181
//@ts-ignore
197182
this._colliderInfo.anchor._onValueChanged = this._updateActualAnchor.bind(this, AnchorOwner.Self);
198-
this._updateConnectedActualAnchor = this._updateActualAnchor.bind(this, AnchorOwner.Connected);
183+
this._handleConnectedAnchorChanged = this._handleConnectedAnchorChanged.bind(this);
199184
//@ts-ignore
200-
this._connectedColliderInfo.anchor._onValueChanged = this._updateConnectedActualAnchor;
185+
this._connectedColliderInfo.anchor._onValueChanged = this._handleConnectedAnchorChanged.bind(this);
201186

202187
this._onSelfTransformChanged = this._onSelfTransformChanged.bind(this);
203188
this._onConnectedTransformChanged = this._onConnectedTransformChanged.bind(this);
@@ -250,6 +235,8 @@ export abstract class Joint extends Component {
250235
const connectedActualAnchor = connectedColliderInfo.actualAnchor;
251236
const connectedCollider = connectedColliderInfo.collider;
252237

238+
// @ts-ignore
239+
connectedAnchor._onValueChanged = null;
253240
if (connectedCollider) {
254241
const { worldPosition: connectedPos, lossyWorldScale: connectedWorldScale } = connectedCollider.entity.transform;
255242
Vector3.subtract(selfPos, connectedPos, Joint._tempVector3);
@@ -259,6 +246,18 @@ export abstract class Joint extends Component {
259246
Vector3.add(selfPos, selfActualAnchor, connectedActualAnchor);
260247
connectedAnchor.copyFrom(connectedActualAnchor);
261248
}
249+
// @ts-ignore
250+
connectedAnchor._onValueChanged = this._handleConnectedAnchorChanged;
251+
this._updateActualAnchor(AnchorOwner.Connected);
252+
}
253+
254+
@ignoreClone
255+
private _handleConnectedAnchorChanged(): void {
256+
if (this._automaticConnectedAnchor) {
257+
console.warn("Cannot set connectedAnchor when automaticConnectedAnchor is true.");
258+
} else {
259+
this._updateActualAnchor(AnchorOwner.Connected);
260+
}
262261
}
263262

264263
@ignoreClone

packages/physics-physx/src/joint/PhysXHingeJoint.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export class PhysXHingeJoint extends PhysXJoint implements IHingeJoint {
3434
const xAxis = PhysXHingeJoint._xAxis;
3535
const axisRotationQuaternion = this._axisRotationQuaternion;
3636
xAxis.set(1, 0, 0);
37-
value.normalize();
3837
const angle = Math.acos(Vector3.dot(xAxis, value));
3938
Vector3.cross(xAxis, value, xAxis);
4039
Quaternion.rotationAxisAngle(xAxis, angle, axisRotationQuaternion);

tests/src/core/physics/HingeJoint.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ describe("HingeJoint", function () {
6060
joint.automaticConnectedAnchor = true;
6161
joint.connectedCollider = boxEntity2.getComponent(DynamicCollider);
6262
joint.anchor = new Vector3(0.5, 0, 0);
63-
joint.axis = new Vector3(0, 1, 0);
64-
63+
joint.axis.x = 0;
64+
joint.axis.y = 1;
6565
expect(formatValue(joint.angle)).eq(0);
6666

6767
collider2.applyTorque(new Vector3(0, 1000, 0));

tests/src/core/physics/Joint.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ describe("Joint", function () {
9595
joint.automaticConnectedAnchor = true;
9696
joint.connectedAnchor = new Vector3(1, 1, 1);
9797
expect(consoleWarnSpy).toBeCalledTimes(1);
98+
joint.connectedAnchor.y = 3;
99+
expect(consoleWarnSpy).toBeCalledTimes(2);
100+
joint.automaticConnectedAnchor = false;
101+
joint.automaticConnectedAnchor = true;
102+
expect(joint.connectedAnchor).deep.include({ x: 4, y: 4, z: 4 });
98103

99104
// @ts-ignore
100105
engine.sceneManager.activeScene.physics._update(1 / 60);

0 commit comments

Comments
 (0)