From 10ad1a2c4d41f1255540f3639583a10f48151dba Mon Sep 17 00:00:00 2001 From: Alexapp Date: Thu, 12 Jun 2025 01:02:36 +0300 Subject: [PATCH 1/2] Add get position and rotation properties --- .../components/rigid-body/component.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/framework/components/rigid-body/component.js b/src/framework/components/rigid-body/component.js index 41c8d44a8a3..2fc240250f7 100644 --- a/src/framework/components/rigid-body/component.js +++ b/src/framework/components/rigid-body/component.js @@ -173,6 +173,12 @@ class RigidBodyComponent extends Component { /** @private */ _type = BODYTYPE_STATIC; + /** @private */ + _position = new Vec3(); + + /** @private */ + _rotation = new Quat(); + /** @ignore */ static onLibraryLoaded() { // Lazily create shared variable @@ -196,6 +202,42 @@ class RigidBodyComponent extends Component { _ammoQuat = null; } + /** + * The position of the rigidbody. + * + * @returns {Vec3} the position of the rigidbody. + */ + get position() { + + if (this._body && this._type === BODYTYPE_DYNAMIC) { + + const currentTransform = this._body.getWorldTransform(); + const currentPosition = currentTransform.getOrigin(); + + return this._position.set(currentPosition.x(), currentPosition.y(), currentPosition.z()); + } + + return this.entity.getPosition(); + } + + /** + * The rotation of the rigidbody. + * + * @returns {Quat} the rotation of the rigidbody. + */ + get rotation() { + + if (this._body && this._type === BODYTYPE_DYNAMIC) { + + const currentTransform = this._body.getWorldTransform(); + const currentRotation = currentTransform.getRotation(); + + return this._rotation.set(currentRotation.x(), currentRotation.y(), currentRotation.z(), currentRotation.w()); + } + + return this.entity.getRotation(); + } + /** * Sets the rate at which a body loses angular velocity over time. * From e2f163530d8eaa69854bee63548723688038a1ec Mon Sep 17 00:00:00 2001 From: Alexapp Date: Thu, 12 Jun 2025 01:32:55 +0300 Subject: [PATCH 2/2] Added offset support --- .../components/rigid-body/component.js | 62 +++++++++++++------ 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/src/framework/components/rigid-body/component.js b/src/framework/components/rigid-body/component.js index 2fc240250f7..b25bfb870ba 100644 --- a/src/framework/components/rigid-body/component.js +++ b/src/framework/components/rigid-body/component.js @@ -208,16 +208,8 @@ class RigidBodyComponent extends Component { * @returns {Vec3} the position of the rigidbody. */ get position() { - - if (this._body && this._type === BODYTYPE_DYNAMIC) { - - const currentTransform = this._body.getWorldTransform(); - const currentPosition = currentTransform.getOrigin(); - - return this._position.set(currentPosition.x(), currentPosition.y(), currentPosition.z()); - } - - return this.entity.getPosition(); + this.getPositionAndRotation(this._position, this._rotation); + return this._position; } /** @@ -226,16 +218,8 @@ class RigidBodyComponent extends Component { * @returns {Quat} the rotation of the rigidbody. */ get rotation() { - - if (this._body && this._type === BODYTYPE_DYNAMIC) { - - const currentTransform = this._body.getWorldTransform(); - const currentRotation = currentTransform.getRotation(); - - return this._rotation.set(currentRotation.x(), currentRotation.y(), currentRotation.z(), currentRotation.w()); - } - - return this.entity.getRotation(); + this.getPositionAndRotation(this._position, this._rotation); + return this._rotation; } /** @@ -1108,6 +1092,44 @@ class RigidBodyComponent extends Component { } } + /** + * Returns the position and rotation of the rigidbody + * + * @param {Vec3} goalPos - The out position of the rigidbody. + * @param {Quat} goalRot - The out rotation of the rigidbody. + */ + getPositionAndRotation(goalPos, goalRot) { + + if (this._body && this._type === BODYTYPE_DYNAMIC) { + + const t = this._body.getWorldTransform(); + const p = t.getOrigin(); + const q = t.getRotation(); + + const component = this.entity.collision; + if (component && component._hasOffset) { + const lo = component.data.linearOffset; + const ao = component.data.angularOffset; + + // Un-rotate the angular offset and then use the new rotation to + // un-translate the linear offset in local space + // Order of operations matter here + const invertedAo = _quat2.copy(ao).invert(); + + goalRot.set(q.x(), q.y(), q.z(), q.w()).mul(invertedAo); + goalRot.transformVector(lo, _vec3); + goalPos.set(p.x() - _vec3.x, p.y() - _vec3.y, p.z() - _vec3.z); + + } else { + goalPos.set(p.x(), p.y(), p.z()); + goalRot.set(q.x(), q.y(), q.z(), q.w()); + } + } else { + goalPos.copy(this.entity.getPosition()); + goalRot.copy(this.entity.getRotation()); + } + } + /** * Sets an entity's transform to match that of the world transformation matrix of a dynamic * rigid body's motion state.