Skip to content

Navigation Menu

Sign in
Sign up

why the fixed body position is updated? #193

keman5 started this conversation in General
Discussion options

I have a box on the plane

  1. the box mass is 1, no force on it.
  2. call world.fixedStep(), the box position will be updated every tick
  3. after a few minites, the box will move out the plane.

If the box mass = 0, the position.x will not be updated, it's fine.

 const world = new CANNON.World({
 gravity: new CANNON.Vec3(0, -9.81, 0),
 });
 // box
 const boxBody = new CANNON.Body({
 mass: 1,
 shape: new CANNON.Box(new CANNON.Vec3(2, 2, 2)),
 });
 boxBody.position.set(2, 0, -2);
 world.addBody(boxBody);
 // ground
 const groundBody = new CANNON.Body({
 shape: new CANNON.Plane(new CANNON.Vec3(10, 10)),
 });
 groundBody.quaternion.setFromEuler(-Math.PI / 2, 0, 0); // 绕x轴旋转90度
 world.addBody(groundBody);
 const tick = () => {
 world.fixedStep();
 console.log(`boxBody.x: ${boxBody.position.x}`);
 cannonDebugger.update();
 window.requestAnimationFrame(tick);
 };
 tick();

how to fix it?

CleanShot_2023年10月30日_3.37.22.mp4
You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

The workaround for this is "body sleeping".

You can configure cannon-es so bodies moving slower than a configurable speed for a configurable time go to "sleep", and do not move until they are touched by another body, or manually woken up.

See this demo:
https://pmndrs.github.io/cannon-es/examples/sleep
https://github.com/pmndrs/cannon-es/blob/master/examples/sleep.html

You must be logged in to vote
2 replies
Comment options

Thanks, can I use body.applyLocalImpulse(new CANNON.Vec3(5, 0, 0), new CANNON.Vec3()) to wake up body when I press keyboard?

Comment options

Yep, applyLocalImpulse will wake up a sleeping body 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Converted from issue

This discussion was converted from issue #192 on October 30, 2023 11:15.

AltStyle によって変換されたページ (->オリジナル) /