How to disable PhysicsBody?

Hi freinds,

I created a box and sphere and added Physics Aggregate to both.

Then i add a constraint between box and a sphere.

I want to reset rotation, so I want to disable box physicbody and reset rotation and enable box physicbody again.

How to do this?
Is there better way?

Thank you.

1 Like

For Havok engine

body.disablePrestep = false;

Also consider replacing PhysicsAggregate with PhysicsBody and PhysicShape instead.

After setting body.disablePrestep = false and changing the rotation, the mesh resets because of the physics.

My code:

if (pressResetRotationKey){
          bodyMesh.disablePrestep = false;
          bodyMesh.rotation=new Vector3(0,0,0);
          bodyMesh.disablePrestep = true;
}

Please don’t ask me to provide a Playground example, because the codebase is quite large.

I think you should probably leave disablePrestep set to false for at least one full frame, as the synchronization (with the physics engine) likely happens each frame.

2 Likes

Not worked.

I found another method that works well, but the dispose() function doesn’t seem to work.

My InitMotor() function create motor with constraints and … with imports different parts of the model using separate ImportMeshAsync calls (for example: body, wheels, etc.). After that, I use the convertToMesh() function to merge the meshes into a single one for easier manipulation and physics.

const convertToMesh = (model) => {
  const meshes = model.meshes.filter(mesh => mesh instanceof Mesh && mesh !== model.meshes[0]); // Exclude root
  meshes.forEach(m => m.setParent(null)); // Ensure no parent hierarchy interferes
  const mesh = Mesh.MergeMeshes(meshes, true, true, undefined, false, true);
  return mesh
}

When I try to reset the motor (after it flips or gets stuck), I call dispose() on the merged mesh before reinitializing. However, it seems like the original meshes from the import are not actually disposed, and they remain in the scene or memory.

if (restartGame) {
        // not work
        bodyMesh.dispose();

        // create motor again
        await initialMotor(bodyMesh.position.x);
}

Please answer :folded_hands:.

What didn’t work? Try disablePrestep = false; and never set it to true. See my link if you really want to reset it back to True. You can’t just set disablePrestep, change transformation, reset disablePrestep, as @qq2315137135 said and as I mention in the link: prestep must be set for an entire frame.

Your issues are going to be difficult to debug without some kind of playground.

“Why isn’t it doing exactly as I want?”

“Because it’s doing exactly as you said.”

“But I told Babylon to do exactly as I want.”

Sometimes it’s very difficult to distinguish between a Babylon bug, a user error, or our own misunderstanding of what you’re intending. A playground helps greatly to delineate. Plus you’ll sometimes solve your own problem by having something simplified to play around with.

On the practical side, helping those who are trying to help you can lead to faster responses from a wider group of volunteers.

1 Like

This problem was solved by using aggregate.body.setPrestepType(PhysicsPrestepType.ACTION);.

Teleport mode didn’t work for me, and I don’t really understand what it’s supposed to do.

scene.onBeforeRenderObservable.add(async () => {
      if (pressRKey) {
        bodyMesh.physicsBody.setPrestepType(PhysicsPrestepType.ACTION);
        //fix motor rotation when fliped
        bodyMesh.rotation = new Vector3(0, Math.PI/2, 0)
        setTimeout(() => {
          bodyMesh.physicsBody.disablePreStep=true
        }, 500);
      }}

What do you think about this approach?

1 Like

In that case Body and Shape still exist in the physics world so it can be a collider with other bodies. If it’s not an issue, then it’s the way to go.