Skip to content

Dynamic body without gravity #34

Open
@vincentfretin

Description

@vincentfretin

I just did something quite complex to create a dynamic body without gravity, and then apply gravity when I grab it so it falls when I release it.

boxEl.setAttribute("physx-body", "type: dynamic");
boxEl.setAttribute("toggle-physics", "");
boxEl.addEventListener("componentinitialized", (evt) => {
  if (evt.detail.name !== "physx-body") {
    return;
  }
  boxEl.components["physx-body"].physxRegisteredPromise.then(() => {
    boxEl.components["physx-body"].toggleGravity();
  });
});
boxEl.addEventListener(
  "pickup",
  () => {
    boxEl.components["physx-body"].toggleGravity();
  },
  { once: true }
);

The pickup event is emitted by grab-magnet-target component (from handy-controls) that toggles the grabbed state if using toggle-physics component that set the kinematic flag

physx/src/physics.js

Lines 953 to 958 in a1271b4

events: {
stateadded: function(e) {
if (e.detail === 'grabbed') {
this.rigidBody.setRigidBodyFlag(PhysX.PxRigidBodyFlag.eKINEMATIC, true)
}
},

Instead of componentinitialized listener and using internal physxRegisteredPromise variable, we should probably add an applyGravity property to physx-body.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions