Skip to content
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

Extend RigidBodyComponent to include useGravity and gravity #5163

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ea7a094
Added gravity flags
MushAsterion Mar 17, 2023
a954a59
Implemented `useGravity` and gravity functions
MushAsterion Mar 17, 2023
772cbd5
Moved `gravity` into getter/setter
MushAsterion Mar 17, 2023
4794ba9
Added `gravity`/`useGravity` on body creation
MushAsterion Mar 17, 2023
6316c92
Fixed missing assertion on body creation
MushAsterion Mar 17, 2023
9f25941
Added useGravity and gravity on component cloning
MushAsterion Mar 17, 2023
42803c7
Fixed tests not having Ammo referenced
MushAsterion Mar 17, 2023
ffc1e94
Fixed check for Ammo being defined
MushAsterion Mar 17, 2023
02a5b8e
Merge branch 'playcanvas:main' into useGravity
MushAsterion Mar 18, 2023
0619a61
Merge branch 'playcanvas:main' into useGravity
MushAsterion Mar 22, 2023
07e0047
Merge branch 'playcanvas:main' into useGravity
MushAsterion Mar 25, 2023
98082f9
Merge branch 'playcanvas:main' into useGravity
MushAsterion Apr 2, 2023
1aa9ec7
Merge branch 'playcanvas:main' into useGravity
MushAsterion Apr 8, 2023
6c32b4b
Merge branch 'playcanvas:main' into useGravity
MushAsterion Apr 13, 2023
4ba355f
Merge branch 'playcanvas:main' into useGravity
MushAsterion Apr 17, 2023
6c53480
Merge branch 'playcanvas:main' into useGravity
MushAsterion Apr 18, 2023
3e1d572
Add back Debug
MushAsterion Apr 18, 2023
6d7a3eb
Merge branch 'playcanvas:main' into useGravity
MushAsterion Apr 26, 2023
91242b1
Merge branch 'playcanvas:main' into useGravity
MushAsterion Apr 29, 2023
4651795
Merge branch 'playcanvas:main' into useGravity
MushAsterion May 10, 2023
b745ee3
Merge branch 'playcanvas:main' into useGravity
MushAsterion May 12, 2023
04aec10
Merge branch 'playcanvas:main' into useGravity
MushAsterion May 28, 2023
cc4b4be
Merge branch 'playcanvas:main' into useGravity
MushAsterion Jun 20, 2023
a2cdde2
Merge branch 'playcanvas:main' into useGravity
MushAsterion Jun 28, 2023
5ccd89a
Merge branch 'playcanvas:main' into useGravity
MushAsterion Jul 24, 2023
3530bac
Merge branch 'playcanvas:main' into useGravity
MushAsterion Jul 28, 2023
57b4f90
Merge branch 'playcanvas:main' into useGravity
MushAsterion Jul 31, 2023
80dc2c8
Merge branch 'playcanvas:main' into useGravity
MushAsterion Aug 10, 2023
eeaf601
Merge branch 'playcanvas:main' into useGravity
MushAsterion Aug 17, 2023
5d4061c
Merge branch 'playcanvas:main' into useGravity
MushAsterion Oct 16, 2023
29a0cd6
Merge branch 'playcanvas:main' into useGravity
MushAsterion Oct 31, 2023
7ceeacd
Merge branch 'playcanvas:main' into useGravity
MushAsterion Feb 14, 2024
f3f1b20
Merge branch 'main' into useGravity
kpal81xd Mar 1, 2024
9cae81f
Merge branch 'playcanvas:main' into useGravity
MushAsterion Mar 29, 2024
77711ae
Merge branch 'playcanvas:main' into useGravity
MushAsterion May 11, 2024
bb265fe
Merge branch 'playcanvas:main' into useGravity
MushAsterion May 29, 2024
0dd0fb1
Merge branch 'playcanvas:main' into useGravity
MushAsterion Jun 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions src/framework/components/rigid-body/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}.
*
Expand Down
8 changes: 8 additions & 0 deletions src/framework/components/rigid-body/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
MushAsterion marked this conversation as resolved.
Show resolved Hide resolved

// groups
export const BODYGROUP_NONE = 0;
export const BODYGROUP_DEFAULT = 1;
Expand Down