-
-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix rotation error when add joint #2526
Changes from 5 commits
178709f
e9464a0
9fcb056
6503cbf
372fe9d
5399a6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,9 +10,9 @@ import { PhysXJoint } from "./PhysXJoint"; | |||||||||||||||||||||||||||||||||||||||||||||||
export class PhysXHingeJoint extends PhysXJoint implements IHingeJoint { | ||||||||||||||||||||||||||||||||||||||||||||||||
protected static _xAxis = new Vector3(1, 0, 0); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
private _anchor: Vector3; | ||||||||||||||||||||||||||||||||||||||||||||||||
private _connectedAnchor: Vector3; | ||||||||||||||||||||||||||||||||||||||||||||||||
private _axis: Vector3; | ||||||||||||||||||||||||||||||||||||||||||||||||
private _axisRotationQuaternion = new Quaternion(); | ||||||||||||||||||||||||||||||||||||||||||||||||
private _connectedAxisRotationQuaternion = new Quaternion(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
constructor(physXPhysics: PhysXPhysics, collider: PhysXCollider) { | ||||||||||||||||||||||||||||||||||||||||||||||||
super(physXPhysics); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -27,27 +27,38 @@ export class PhysXHingeJoint extends PhysXJoint implements IHingeJoint { | |||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
override setRotation(value: Quaternion): void { | ||||||||||||||||||||||||||||||||||||||||||||||||
this._rotation.copyFrom(value); | ||||||||||||||||||||||||||||||||||||||||||||||||
this._axis && this.setAxis(this._axis); | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||
* {@inheritDoc IHingeJoint.setAxis } | ||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||
setAxis(value: Vector3): void { | ||||||||||||||||||||||||||||||||||||||||||||||||
this._axis = value; | ||||||||||||||||||||||||||||||||||||||||||||||||
const xAxis = PhysXHingeJoint._xAxis; | ||||||||||||||||||||||||||||||||||||||||||||||||
const axisRotationQuaternion = this._axisRotationQuaternion; | ||||||||||||||||||||||||||||||||||||||||||||||||
xAxis.set(1, 0, 0); | ||||||||||||||||||||||||||||||||||||||||||||||||
const angle = Math.acos(Vector3.dot(xAxis, value)); | ||||||||||||||||||||||||||||||||||||||||||||||||
Vector3.cross(xAxis, value, xAxis); | ||||||||||||||||||||||||||||||||||||||||||||||||
Quaternion.rotationAxisAngle(xAxis, angle, axisRotationQuaternion); | ||||||||||||||||||||||||||||||||||||||||||||||||
this._setLocalPose(0, this._anchor, axisRotationQuaternion); | ||||||||||||||||||||||||||||||||||||||||||||||||
this._setLocalPose(1, this._connectedAnchor, axisRotationQuaternion); | ||||||||||||||||||||||||||||||||||||||||||||||||
const connectedAxisRotationQuaternion = this._connectedAxisRotationQuaternion; | ||||||||||||||||||||||||||||||||||||||||||||||||
Quaternion.multiply(this._rotation, axisRotationQuaternion, connectedAxisRotationQuaternion); | ||||||||||||||||||||||||||||||||||||||||||||||||
this._setLocalPose(1, this._connectedAnchor, connectedAxisRotationQuaternion); | ||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+39
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Clamp the domain of const angle = Math.acos(
- Vector3.dot(xAxis, value)
+ Math.min(1, Math.max(-1, Vector3.dot(xAxis, value)))
); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
override setAnchor(value: Vector3): void { | ||||||||||||||||||||||||||||||||||||||||||||||||
this._setLocalPose(0, value, this._axisRotationQuaternion); | ||||||||||||||||||||||||||||||||||||||||||||||||
this._anchor = value; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||
* {@inheritDoc IJoint.setConnectedAnchor } | ||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||
override setConnectedAnchor(value: Vector3): void { | ||||||||||||||||||||||||||||||||||||||||||||||||
this._setLocalPose(1, value, this._axisRotationQuaternion); | ||||||||||||||||||||||||||||||||||||||||||||||||
this._setLocalPose(1, value, this._connectedAxisRotationQuaternion); | ||||||||||||||||||||||||||||||||||||||||||||||||
this._connectedAnchor = value; | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check for null
prefabContext
.If
_prefabContextMap
lacks an entry for the entity,prefabContext
could beundefined
, causing a runtime error at line 146. Consider adding a fallback or error log.📝 Committable suggestion