Hey guys, i’m trying to make a multiplayer game and i’m noticing fairly big time differences between different devices… i’m wondering how to synchronise them if that’s possible at all? For example my laptop runs the game much faster than my iphone. Objects that fall from the top to the bottom on my laptop fall much faster than on my iphone. I presume it is all to do with the speed of the processor? I know a javascript setInterval will vary in speed just a tiny tiny almost un noticeable amount between different devices. The cocos 2Dx update function which uses delta time seems to vary in timing massively. What affects this? how can i get devices to run the game at a similar speed? This is happening both in the update functions of any object and also when the physics engine is used to apply gravity. The timing is totally out, my laptop runs the game how i want it to be, my iphone is about 2 3rds the speed and it ruins the game.
Hi, do you use delta time in calculations for your physics?
No, I found the physics engine is completely different depending what device you use and seems to make the game lag the more its used so i only use gravity to make collectable items fall. Every other object that moves down is controlled through the update function using calculations and as i say, those nodes do not have physics applied to them.
See for yourself…
cc.Class({
extends: cc.Component,
properties: {
totalepochs:0,
totalepochsLabel: cc.Label,
velocity:1,
vlabel: cc.Label,
totaldt:0,
totalDeltaTimeElapsed: cc.Label,
stop:false,
},
update: function (dt) {
if(!this.stop){
this.totalepochsLabel.string = this.totalepochs++;
// adjust to change speed
var monsterLevel = 1;
var velinc = ((monsterLevel / 10) * 0.25);
var vlinc = 1.75 + velinc;
var velocity = this.velocity;
this.vlabel.string = velocity;
this.totaldt += dt;
this.totalDeltaTimeElapsed.string = this.totaldt;
this.velocity += vlinc;
console.log(this.node.y);
this.node.y += (velocity * dt) * -1; // move the meteor down
if(this.node.y <= -480){
this.stop = true;
}
}
}
});
here are the comparison stats;
Laptop i7 32gb ram;
time to reach bottom (total DT) = 2.96s (avrg)
total epochs to reach bottom = 368 (avrg)
volocity reached = 654 (avrg)
iphone 7;
time to reach bottom (total DT) = 4.25s (avrg)
total epochs to reach bottom = 256 (avrg)
volocity reached = 455 (avrg)
As you can see there is a big difference… any suggestions how i could try to equalise them?
I have just tested with the following
this.node.y += (200 * dt) * -1; // move the meteor down
now both devices reach the bottom in 4.8 seconds.
What could be causing the problem when i add in the velocity??? Any ideas?
The problem is the amount of epochs varies between device. So if you make calculations that effect the rate of fall ie velocity, the calculations are different depending on the device. So the solution is to some how try to create a timer for the calculations rather than run every epoch.
Your logic when you just do this
this.node.y += (200 * dt) * -1; // move the meteor down
Properly ties all of your calculations to the update delta value.
In your earlier post with velocity, those velocity calculations are not tied in any way to the update delta when you store them in the class member on this line:
this.velocity += vlinc;
So if one machine is calling update more frequency, those velocity calculations will be executed more frequently. So by the time you calculate the node’s y position here
this.node.y += (velocity * dt) * -1; // move the meteor down
You’ve attempted to tie your calculations to the update delta too late. You need to come up with a solution that ties your velocity calculation to the update delta earlier, when you assign it back to your this.velocity class member.
That still seems inelegant. This problem can be solved in a proper way so that it doesn’t creep up later on in the future when you don’t expect it. Remove the time keeper logic for now (just comment it out for the moment so you can put it back if need be) and try modifying this line:
var vlinc = 1.75 + velinc;
To this:
var vlinc = (1.75 + velinc) * dt;
And see if that fixes the problem.
Sure was the problem
To clarify for people like me who learn slow…
The dt variable in the update function is the time since the function was last run, also known as an epoch i think?
The amount of epochs will vary depending on the power of the processor. Bu the delta time will always be the time in between the epochs.
So the problem was this, On my laptop there were nearly 1000 epochs but on my phone only 300. that meant that the velocity was increased 1000 times in my laptop and only 300 times in my phone. So the timekeep variable is needed but only for the increase of the velocity.
Why do you name frame updates as “epoch” ?
dt is time since last frame update (scene draw)
frame updates per second are named fps (frames per second)
UNIX Epoch time is 00:00:00 Thursday, 1 January 1970