The velocity of the sprite is set to 120 using .setVelocityX(120)
and I am also calling the update() of the object manually with hardcoded values like obj.update(0,16) from the scene’s update()
The obj extents Phaser.Physics.Arcade.Sprite and inside its update function I am printing its position as follows
Though at the beginning it increments by 1.92 (as 1.92x1000/16 = 120) but then it sometimes skips an update or updates twice. (ie increments by 1.92x2).
I event tried this.physics.world.setFPS(1000/16) in the scene but no use.
My current project demands the physics simulation to be exact every time I run it. To be clear, I am doing a sort of physics simulation. The first case is a body moving through space at a constant velocity.
And given a dt = 16ms the simulation will ultimately produce positions, velocity etc of each particle for each time step (dt) and I should produce the same output on every run.(not only after 1s but on very dt step).
The physics simulation steps are independent of the frequency at which scene update is called.
If you run this at anything other than 60Hz, you’ll see a lot more ‘inconsistency’.
So you shouldn’t be expecting x to increase on every scene update. You could check after a second where x is, that would make more sense.
Also, you’re not using t or dt (unless you’ve overwritten Sprite.update?), so there’s no need for any of this. You should do setVelocity only once.
Yes I have override the sprite’s update method. The update function snippet it showed above is from the sprite class.
I want the physic simulation to be same given the same innitial condition and a fixed ‘td’, the runs should produce the same output.
How to achieve this?
The game loop steps at a nonfixed size, averaging 16.66ms on most devices, but with some variance. So usually there is one physics step per scene update, but sometimes two and sometimes zero.
The physics step happens through the Arcade Physics plugin, not through the game object’s update() method.
@Milton and @samme thanks for all the help.
I was thinking the game object’s update() method was doing the physics calculations and that was causing all the confusion. So as pointed out by Milton the logging is wrong.
So logging is done inside this callback then the output is correct. scene.physics.world.on('worldstep', function(delta) { /* ... */ }, this);
If my understanding is correct I can tie the physics update step with the scene update using the below code:
And inside scene’s update() this.physics.world.step(16/1000)
Then that too should make the logging consistent?
Update: using the above way the logging is consistent on every run but the physics calculations seems to be messed up.
So what’s the correct way to tie the physics and scene update loops?
Why would you want to do that? Scene delta (game framerate) isn’t constant. So if you then use a constant step (16/1000), your physics get messed up.
If you step yourself, you should step(delta). But that’s not deterministic of course…
My guess is you don’t want to tie the physics and scene updates. Do everything that depends on the physics step inside the worldstep callback, and anything that doesn’t inside update().
@samme@Milton
I think the this.physics.world.on('worldstep', function callback(delta) { ... } is still not perfect.
Cause I found a run with a body under uniform velocity as follows,
x step
25 1
26 2
27 3
27 4 *
29 5
30 6
My hypothesis: It can happen that the callback is called before the “worldstep” execution is complete. (Like the callback gets executed in between the physics calculation before x is updated).
Also, I think the problem is with the use of callback. If using call back there is no guarantee when the js engine will execute it. For example, this.physics.world.on('worldstep', function cb1(delta) { ... } cb1 can get executed immediately after worldstep or after world.postUpdate is completed or maybe even further in the future.