engine icon indicating copy to clipboard operation
engine copied to clipboard

Extend `RigidBodyComponent` to include `useGravity` and `gravity`

Open MushAsterion opened this issue 1 year ago • 4 comments

  • [ ] Requires to update Ammo.js to latest version. (which is not a breaking change)
    • [x] Requires exposure of setFlags (and optionally getFlags for counterpart) from Ammo.btRigidBody to work

Allows a better management of gravity for rigid bodies.

Changes to pc public API:

  • BODYFLAG_GRAVITY_WORLD_ENABLE: constant to enable world gravity on body
  • BODYFLAG_GRAVITY_WORLD_DISABLE: constant to disable world gravity on body
  • BODYFLAG_GRAVITY_GYROSCOPIC_EXPLICIT: constant to enable explicit gyroscopic forces
  • BODYFLAG_GRAVITY_GYROSCOPIC_IMPLICIT_WORLD: constant to enable implicit world gyroscopic forces
  • BODYFLAG_GRAVITY_GYROSCOPIC_EXPLICIT_BODY: constant to enable implicit body gyroscopic forces
  • BODYFLAG_GRAVITY_GYROSCOPIC_ENABLE: constant to enable all gyroscopic forces

Changes to pc.RigidBodyComponent public API:

  • useGravity: boolean, defining whether or not the body should use gravity.
  • gravity: Vec3 to represent gravity applying only to this entity. Defaults to null (use world gravity).

I confirm I have read the contributing guidelines and signed the Contributor License Agreement.

MushAsterion avatar Mar 17 '23 10:03 MushAsterion

Could this API be simplified to:

RigidBodyComponent#gravity (Vec3)

If set to null, simply use world gravity.

willeastcott avatar Mar 17 '23 13:03 willeastcott

Could this API be simplified to:

RigidBodyComponent#gravity (Vec3)

If set to null, simply use world gravity.

Unfortunately Vec3 don't have events on modification which means it will need to loop to every single rigid bodies and make sure their gravity has not changed before simulating the next step, which also add a for loop in RigidBodySystem#update. Honestly I'm not sure it would be good for performance. RigidBodySystem is having it at a Vec3 but it's only one check so not a very high impact compared to RigidBodyComponent that could be present in large number. And I'm not sure adding a set event on Vec3 would be good for performance as well...

MushAsterion avatar Mar 17 '23 14:03 MushAsterion

I don't get how this is different to CameraComponent#clearColor for example. You can't do camera.clearColor.r = 1;. Instead, you can only do camera.clearColor = new pc.Color(1, 0, 0);. It's just a limitation of the entity-component system that you can't set individual properties on vectors/colors/quaternions/etc.

willeastcott avatar Mar 17 '23 15:03 willeastcott

I don't get how this is different to CameraComponent#clearColor for example. You can't do camera.clearColor.r = 1;. Instead, you can only do camera.clearColor = new pc.Color(1, 0, 0);. It's just a limitation of the entity-component system that you can't set individual properties on vectors/colors/quaternions/etc.

Oh alright, I have to admit I don't use the whole API so I have not seen all the variation but yeah it makes the code much more simple, I was just afraid about the clarity to users that they should not edit the base value. It's updated then!

MushAsterion avatar Mar 17 '23 16:03 MushAsterion