cannon.js: World.step() doesn't make sense
I’ve been trying to figure out how exactly World.step() works. I’ve read the documentation and multiple examples. Here’s my use case:
update(timeDelta) {
this.cannonWorld.step(1 / 60, timeDelta, 10);
}
update()
is called in the main game loop at a slightly variable interval (requestAnimationFrame()
recursive). timeDelta
is the amount of time since the last update cycle; usually around 0.016 seconds. Given that it’s the “time since last called” - the exact name of parameter 2 from the docs - it seems like a natural thing to pass in that spot.
However, with the above code, an increase in items being simulated actually slows down the physics simulation, but strangely, not the program itself. The frame-rate doesn’t drop, everything just starts moving in slow-motion, even though the arguments to step()
do not change (aside from a ~0.001 variance in timeDelta).
Removing all but the first parameter gets rid of the slowdown no matter how many objects are in the world, but it sounds like that also disables interpolation.
All of that aside, I’m confused by the docs as to why parameter 1 and 2 both exist… Both seem to represent the amount to progress time in the current simulation cycle, and 1 seems to have to be completely constant for some reason, even though frame-rate can be variable. I can’t even be sure if the slowdown behavior above is a bug, because it’s not clear what parameter 2 is supposed to do.
Can anyone provide some insight? This ticket can of course be converted to a bug report if the slowdown is in fact a bug.
About this issue
- Original URL
- State: open
- Created 6 years ago
- Comments: 15 (1 by maintainers)
I am using something like this:
@brundonsmith I am using cannon as well - can I ask if you see the issue in this demo? http://mesh.robrohan.com/#physics or is it something that builds up over a long time?
The reasons I ask, is before using cannon I was trying to write my own physics engine from scratch, and started with this for the loop: https://gafferongames.com/post/fix_your_timestep/ and when I moved over to cannon, just kept the same loop. If you don’t see the same problem in that demo, maybe checkout that article.
So, my theory is that the process itself isn’t actually slowing down, but that the interval between steps is getting larger somehow. I think this because my character movement happens independently of the physics, and it doesn’t slow down at all; only the physics simulation itself slows down. Why this might happen is beyond me.
What we need is for someone who’s familiar with the source code to weigh in and explain how those parameters actually work (and ideally fix the documentation accordingly, since it’s incorrect). If none of the developers respond here, and I find myself with both the time and the motivation, maybe I’ll take a stab at reading the source myself.