forked from schteppe/cannon.js
-
Notifications
You must be signed in to change notification settings - Fork 154
I have a box on the plane
- the box mass is 1, no force on it.
- call world.fixedStep(), the box position will be updated every tick
- 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
All reactions
Replies: 1 comment 2 replies
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
All reactions
2 replies
Thanks, can I use body.applyLocalImpulse(new CANNON.Vec3(5, 0, 0), new CANNON.Vec3()) to wake up body when I press keyboard?
All reactions
-
👍 1
Yep, applyLocalImpulse will wake up a sleeping body 🙂
All reactions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment