From ea7a09463d685e44c2e7683d64b57bbd9ab7173b Mon Sep 17 00:00:00 2001 From: Lucas Petitjean <28061629+MushAsterion@users.noreply.github.com> Date: Fri, 17 Mar 2023 10:32:07 +0000 Subject: [PATCH 1/9] Added gravity flags --- src/framework/components/rigid-body/constants.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/framework/components/rigid-body/constants.js b/src/framework/components/rigid-body/constants.js index 4a7c294b3da..b12ca64a83a 100644 --- a/src/framework/components/rigid-body/constants.js +++ b/src/framework/components/rigid-body/constants.js @@ -32,6 +32,14 @@ export const BODYSTATE_WANTS_DEACTIVATION = 3; export const BODYSTATE_DISABLE_DEACTIVATION = 4; export const BODYSTATE_DISABLE_SIMULATION = 5; +// Gravity flags +export const BODYFLAG_GRAVITY_WORLD_ENABLE = 0; +export const BODYFLAG_GRAVITY_WORLD_DISABLE = 1; +export const BODYFLAG_GRAVITY_GYROSCOPIC_EXPLICIT = 2; +export const BODYFLAG_GRAVITY_GYROSCOPIC_IMPLICIT_WORLD = 4; +export const BODYFLAG_GRAVITY_GYROSCOPIC_IMPLICIT_BODY = 8; +export const BODYFLAG_GRAVITY_GYROSCOPIC_ENABLE = BODYFLAG_GRAVITY_GYROSCOPIC_IMPLICIT_BODY; + // groups export const BODYGROUP_NONE = 0; export const BODYGROUP_DEFAULT = 1; From a954a59c8bf378d85eb4f3bdb6b5fba8aad81fb6 Mon Sep 17 00:00:00 2001 From: Lucas Petitjean <28061629+MushAsterion@users.noreply.github.com> Date: Fri, 17 Mar 2023 10:40:15 +0000 Subject: [PATCH 2/9] Implemented `useGravity` and gravity functions --- .../components/rigid-body/component.js | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/src/framework/components/rigid-body/component.js b/src/framework/components/rigid-body/component.js index c2ef485fb2c..8fc8fe65fde 100644 --- a/src/framework/components/rigid-body/component.js +++ b/src/framework/components/rigid-body/component.js @@ -5,6 +5,7 @@ import { Vec3 } from '../../../core/math/vec3.js'; import { BODYFLAG_KINEMATIC_OBJECT, BODYTYPE_STATIC, + BODYFLAG_GRAVITY_WORLD_ENABLE, BODYFLAG_GRAVITY_WORLD_DISABLE, BODYGROUP_DYNAMIC, BODYGROUP_KINEMATIC, BODYGROUP_STATIC, BODYMASK_ALL, BODYMASK_NOT_STATIC, BODYSTATE_ACTIVE_TAG, BODYSTATE_DISABLE_DEACTIVATION, BODYSTATE_DISABLE_SIMULATION, @@ -101,6 +102,22 @@ class RigidBodyComponent extends Component { /** @private */ _type = BODYTYPE_STATIC; + /** + * Whether the RigidBody component uses gravity. + * + * @type {boolean} + * @private + */ + _useGravity = true; + + /** + * The current gravity of this body. + * + * @type {null|Vec3} + * @private + */ + _gravity = null; + /** * Create a new RigidBodyComponent instance. * @@ -482,6 +499,41 @@ class RigidBodyComponent extends Component { return this._type; } + /** + * Whether this rigid body should use gravity. + * + * @type {boolean} + */ + set useGravity(value) { + if (!!value === this._useGravity) { + return; + } + + Debug.assert(Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent.useGravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); + + this._useGravity = !this._useGravity; + + if (this._body) { + this._body.setFlags(this._useGravity && !this._gravity ? BODYFLAG_GRAVITY_WORLD_ENABLE : BODYFLAG_GRAVITY_WORLD_DISABLE); + + if (this._useGravity) { + if (this._gravity) { + _ammoVec1.setValue(this._gravity.x, this._gravity.y, this._gravity.z); + this._body.setGravity(_ammoVec1); + } else { + this._body.setGravity(this.system.dynamicsWorld.getGravity()); + } + } else { + _ammoVec1.setValue(0, 0, 0); + this._body.setGravity(_ammoVec1); + } + } + } + + get useGravity() { + return this._useGravity; + } + /** * If the Entity has a Collision shape attached then create a rigid body using this shape. This * method destroys the existing body. @@ -821,6 +873,76 @@ class RigidBodyComponent extends Component { } } + /** + * Set the gravity for this rigid body. This function has two valid signatures: + * you can either pass a 3D vector or 3 numbers. + * + * @param {Vec3|number} x - 3-dimensional vector holding gravity or X-axis gravity. + * @param {number} [y] - Y-axis gravity. + * @param {number} [z] - Z-axis gravity. + * @example + * // Set via 3 numbers + * this.entity.rigidbody.setGravity(0, -9.81, 0); + * @example + * // Set via vector + * var gravity = new pc.Vec3(0, -9.81, 0); + * this.entity.rigidbody.setGravity(gravity); + */ + setGravity(x, y, z) { + Debug.assert(Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent#setGravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); + + if (!this._gravity) { + this._gravity = new Vec3(); + } + + if (x instanceof Vec3) { + this._gravity.copy(x); + } else { + this._gravity.set(x, y, z); + } + + if (this._body && this._useGravity) { + this._body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); + + _ammoVec1.setValue(this._gravity.x, this._gravity.y, this._gravity.z); + this._body.setGravity(_ammoVec1); + } + } + + /** + * Reset the gravity for this rigid body. Remove any gravity assigned by {@link RigidBodyComponent#setGravity} + * and uses world gravity back. + */ + resetGravity() { + this._gravity = null; + + if (this._body && this._useGravity && Ammo.btRigidBody.prototype.setFlags) { + this._body.setFlags(BODYFLAG_GRAVITY_WORLD_ENABLE); + this._body.setGravity(this.system.dynamicsWorld.getGravity()); + } + } + + /** + * Get the gravity that affects this rigid body. The gravity is returned as a + * {@link Vec3}. The value returned by this function should be considered read-only. In order + * to set the gravity of the rigid body, use {@link RigidBodyComponent#setGravity}. + * + * @returns {Vec3} The gravity of the rigid body. + * @example + * var gravity = this.entity.rigidbody.getGravity(); + * gravity.x = 10; + * this.entity.rigidbody.setGravity(gravity); + */ + getGravity() { + if (this._gravity) { + return this._gravity; + } else if (this._useGravity) { + return this.system.gravity; + } + + return new Vec3(0, 0, 0); + } + /** * Returns true if the rigid body is of type {@link BODYTYPE_STATIC}. * From 772cbd5329bbe20c2bc7f99a07031c1a01d343c5 Mon Sep 17 00:00:00 2001 From: Lucas Petitjean <28061629+MushAsterion@users.noreply.github.com> Date: Fri, 17 Mar 2023 16:06:33 +0000 Subject: [PATCH 3/9] Moved `gravity` into getter/setter --- .../components/rigid-body/component.js | 101 ++++++------------ 1 file changed, 31 insertions(+), 70 deletions(-) diff --git a/src/framework/components/rigid-body/component.js b/src/framework/components/rigid-body/component.js index 8fc8fe65fde..e47db4d74c1 100644 --- a/src/framework/components/rigid-body/component.js +++ b/src/framework/components/rigid-body/component.js @@ -534,6 +534,28 @@ class RigidBodyComponent extends Component { return this._useGravity; } + /** + * The gravity that affects this rigid body. Defaults to null to use world gravity. + * + * @type {Vec3} + */ + set gravity(value) { + Debug.assert(Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent#gravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); + + this._gravity = value; + + if (this._body && this._useGravity) { + this._body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); + + _ammoVec1.setValue(value.x, value.y, value.z); + this._body.setGravity(_ammoVec1); + } + } + + get gravity() { + return this._gravity; + } + /** * If the Entity has a Collision shape attached then create a rigid body using this shape. This * method destroys the existing body. @@ -583,6 +605,15 @@ class RigidBodyComponent extends Component { body.setActivationState(BODYSTATE_DISABLE_DEACTIVATION); } + if (this._useGravity && this._gravity) { + this._body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); + + _ammoVec1.setValue(this._gravity.x, this._gravity.y, this._gravity.z); + this._body.setGravity(_ammoVec1); + } else if (!this._useGravity) { + this._body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); + } + body.entity = entity; this.body = body; @@ -873,76 +904,6 @@ class RigidBodyComponent extends Component { } } - /** - * Set the gravity for this rigid body. This function has two valid signatures: - * you can either pass a 3D vector or 3 numbers. - * - * @param {Vec3|number} x - 3-dimensional vector holding gravity or X-axis gravity. - * @param {number} [y] - Y-axis gravity. - * @param {number} [z] - Z-axis gravity. - * @example - * // Set via 3 numbers - * this.entity.rigidbody.setGravity(0, -9.81, 0); - * @example - * // Set via vector - * var gravity = new pc.Vec3(0, -9.81, 0); - * this.entity.rigidbody.setGravity(gravity); - */ - setGravity(x, y, z) { - Debug.assert(Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent#setGravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); - - if (!this._gravity) { - this._gravity = new Vec3(); - } - - if (x instanceof Vec3) { - this._gravity.copy(x); - } else { - this._gravity.set(x, y, z); - } - - if (this._body && this._useGravity) { - this._body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); - - _ammoVec1.setValue(this._gravity.x, this._gravity.y, this._gravity.z); - this._body.setGravity(_ammoVec1); - } - } - - /** - * Reset the gravity for this rigid body. Remove any gravity assigned by {@link RigidBodyComponent#setGravity} - * and uses world gravity back. - */ - resetGravity() { - this._gravity = null; - - if (this._body && this._useGravity && Ammo.btRigidBody.prototype.setFlags) { - this._body.setFlags(BODYFLAG_GRAVITY_WORLD_ENABLE); - this._body.setGravity(this.system.dynamicsWorld.getGravity()); - } - } - - /** - * Get the gravity that affects this rigid body. The gravity is returned as a - * {@link Vec3}. The value returned by this function should be considered read-only. In order - * to set the gravity of the rigid body, use {@link RigidBodyComponent#setGravity}. - * - * @returns {Vec3} The gravity of the rigid body. - * @example - * var gravity = this.entity.rigidbody.getGravity(); - * gravity.x = 10; - * this.entity.rigidbody.setGravity(gravity); - */ - getGravity() { - if (this._gravity) { - return this._gravity; - } else if (this._useGravity) { - return this.system.gravity; - } - - return new Vec3(0, 0, 0); - } - /** * Returns true if the rigid body is of type {@link BODYTYPE_STATIC}. * From 4794ba9ae5f5170071f02012a2e2599fa8da1235 Mon Sep 17 00:00:00 2001 From: Lucas Petitjean <28061629+MushAsterion@users.noreply.github.com> Date: Fri, 17 Mar 2023 16:14:43 +0000 Subject: [PATCH 4/9] Added `gravity`/`useGravity` on body creation --- src/framework/components/rigid-body/component.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/framework/components/rigid-body/component.js b/src/framework/components/rigid-body/component.js index e47db4d74c1..59c22d85cdd 100644 --- a/src/framework/components/rigid-body/component.js +++ b/src/framework/components/rigid-body/component.js @@ -606,12 +606,12 @@ class RigidBodyComponent extends Component { } if (this._useGravity && this._gravity) { - this._body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); + body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); _ammoVec1.setValue(this._gravity.x, this._gravity.y, this._gravity.z); - this._body.setGravity(_ammoVec1); + body.setGravity(_ammoVec1); } else if (!this._useGravity) { - this._body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); + body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); } body.entity = entity; From 6316c922584cf239f5d596e795514f88a23e63fe Mon Sep 17 00:00:00 2001 From: Lucas Petitjean <28061629+MushAsterion@users.noreply.github.com> Date: Fri, 17 Mar 2023 17:18:09 +0000 Subject: [PATCH 5/9] Fixed missing assertion on body creation --- src/framework/components/rigid-body/component.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/framework/components/rigid-body/component.js b/src/framework/components/rigid-body/component.js index 59c22d85cdd..ab4a5deb491 100644 --- a/src/framework/components/rigid-body/component.js +++ b/src/framework/components/rigid-body/component.js @@ -606,11 +606,13 @@ class RigidBodyComponent extends Component { } if (this._useGravity && this._gravity) { + Debug.assert(Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent#createBody: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); _ammoVec1.setValue(this._gravity.x, this._gravity.y, this._gravity.z); body.setGravity(_ammoVec1); } else if (!this._useGravity) { + Debug.assert(Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent#createBody: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); body.setFlags(BODYFLAG_GRAVITY_WORLD_DISABLE); } From 9f259411e033d12b7690945c841bd3f12e0191b0 Mon Sep 17 00:00:00 2001 From: Lucas Petitjean <28061629+MushAsterion@users.noreply.github.com> Date: Fri, 17 Mar 2023 17:20:10 +0000 Subject: [PATCH 6/9] Added useGravity and gravity on component cloning --- src/framework/components/rigid-body/system.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/framework/components/rigid-body/system.js b/src/framework/components/rigid-body/system.js index d809ea9a229..bebd162b9b7 100644 --- a/src/framework/components/rigid-body/system.js +++ b/src/framework/components/rigid-body/system.js @@ -372,7 +372,9 @@ class RigidBodyComponentSystem extends ComponentSystem { 'restitution', 'type', 'group', - 'mask' + 'mask', + 'useGravity', + 'gravity' ]; for (const property of props) { @@ -404,7 +406,9 @@ class RigidBodyComponentSystem extends ComponentSystem { restitution: rigidbody.restitution, type: rigidbody.type, group: rigidbody.group, - mask: rigidbody.mask + mask: rigidbody.mask, + useGravity: rigidbody.useGravity, + gravity: rigidbody.gravity }; return this.addComponent(clone, data); From 42803c7e428e7d385680397ff0442eb8bb3bd6da Mon Sep 17 00:00:00 2001 From: Lucas Petitjean <28061629+MushAsterion@users.noreply.github.com> Date: Fri, 17 Mar 2023 17:26:02 +0000 Subject: [PATCH 7/9] Fixed tests not having Ammo referenced --- src/framework/components/rigid-body/component.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/framework/components/rigid-body/component.js b/src/framework/components/rigid-body/component.js index ab4a5deb491..eeb909a59ce 100644 --- a/src/framework/components/rigid-body/component.js +++ b/src/framework/components/rigid-body/component.js @@ -509,7 +509,7 @@ class RigidBodyComponent extends Component { return; } - Debug.assert(Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent.useGravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); + Debug.assert(Ammo && Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent.useGravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); this._useGravity = !this._useGravity; @@ -540,7 +540,7 @@ class RigidBodyComponent extends Component { * @type {Vec3} */ set gravity(value) { - Debug.assert(Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent#gravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); + Debug.assert(Ammo && Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent#gravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); this._gravity = value; From ffc1e947037030c0b8e7bd3dca22b316c38171a8 Mon Sep 17 00:00:00 2001 From: Lucas Petitjean <28061629+MushAsterion@users.noreply.github.com> Date: Fri, 17 Mar 2023 17:27:14 +0000 Subject: [PATCH 8/9] Fixed check for Ammo being defined --- src/framework/components/rigid-body/component.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/framework/components/rigid-body/component.js b/src/framework/components/rigid-body/component.js index eeb909a59ce..2f8e322ffeb 100644 --- a/src/framework/components/rigid-body/component.js +++ b/src/framework/components/rigid-body/component.js @@ -509,7 +509,7 @@ class RigidBodyComponent extends Component { return; } - Debug.assert(Ammo && Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent.useGravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); + Debug.assert(typeof Ammo !== 'undefined' && Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent.useGravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); this._useGravity = !this._useGravity; @@ -540,7 +540,7 @@ class RigidBodyComponent extends Component { * @type {Vec3} */ set gravity(value) { - Debug.assert(Ammo && Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent#gravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); + Debug.assert(typeof Ammo !== 'undefined' && Ammo.btRigidBody.prototype.setFlags, 'pc.RigidBodyComponent#gravity: Your version of ammo.js does not expose Ammo.btRigidBody#setFlags. Update it to latest.'); this._gravity = value; From 3e1d572e9fcd0e9a949e1bb7b553255b4a465689 Mon Sep 17 00:00:00 2001 From: Lucas Petitjean <28061629+MushAsterion@users.noreply.github.com> Date: Tue, 18 Apr 2023 20:13:25 +0200 Subject: [PATCH 9/9] Add back Debug --- src/framework/components/rigid-body/component.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/framework/components/rigid-body/component.js b/src/framework/components/rigid-body/component.js index b731afcb829..5f0ea76235c 100644 --- a/src/framework/components/rigid-body/component.js +++ b/src/framework/components/rigid-body/component.js @@ -1,3 +1,5 @@ +import { Debug } from '../../../core/debug.js'; + import { Quat } from '../../../core/math/quat.js'; import { Vec3 } from '../../../core/math/vec3.js';