pmndrs / cannon-es

💣 A lightweight 3D physics engine written in JavaScript.
https://pmndrs.github.io/cannon-es/
MIT License
1.75k stars 130 forks source link

Unknown Force is applied on some vertically stacked cubes on straight plane? #177

Open prathamVaidya opened 1 year ago

prathamVaidya commented 1 year ago

Check out the Video Proof

When three simple cubes are stacked together vertically, after some time the cube experience a force to the left direction. I checked the plane is not rotated at y or z axis. Am I missing something obvious or its just a bug in CannonJS?

johnnyrainbow commented 8 months ago

Yep this seems to be a known issue for cannon.. Even in the jenga example you'll notice that the blocks gradually move. (set solver iterations to 1 to speed this up). You can increase or decrease the time it takes by changing world solver iterations. However even at 50 iterations it still very slightly moves over time. The effect also seems to increase based on the gravity value.

I'd love to see a fix as this is a pretty prevalent issue

https://github.com/schteppe/cannon.js/issues/348 https://github.com/pmndrs/use-cannon/issues/240

prathamVaidya commented 8 months ago

Is there an alternative solution to fix this unexpected behaviour?

I read about using sleep or downgrading versions. Did that really work?

@johnnyrainbow

johnnyrainbow commented 8 months ago

@prathamVaidya sleeping the body seems like the only realistic way to get around it.

Since you still want gravity to apply, what you could do is add a collision listener to the cube. When it collides with another cube, call cubeBody.sleep(). This will stop it from jittering. (Also make sure to use cubeBody.allowSleep = true when you initialise the body)

gabrieldejesus commented 2 months ago

One solution you could try is the "sleep" feature (see the sleep demo).

In my case, I had to set sleepTimeLimit to 0 and sleepSpeedLimit to 0.1. It worked perfectly.