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 all 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
87 changes: 87 additions & 0 deletions src/framework/components/rigid-body/component.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Debug } from '../../../core/debug.js';

import { Quat } from '../../../core/math/quat.js';
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 @@ -161,6 +164,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 @@ -572,6 +591,63 @@ 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(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;

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;
}

/**
* The gravity that affects this rigid body. Defaults to null to use world gravity.
*
* @type {Vec3}
*/
set gravity(value) {
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;

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.
Expand Down Expand Up @@ -625,6 +701,17 @@ class RigidBodyComponent extends Component {
body.setActivationState(BODYSTATE_DISABLE_DEACTIVATION);
}

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);
}

body.entity = entity;

this.body = body;
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 @@ -35,6 +35,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
8 changes: 6 additions & 2 deletions src/framework/components/rigid-body/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ class RigidBodyComponentSystem extends ComponentSystem {
'restitution',
'type',
'group',
'mask'
'mask',
'useGravity',
'gravity'
];

for (const property of props) {
Expand Down Expand Up @@ -450,7 +452,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);
Expand Down