I have check delta value in render method in Screen class.. I saw it is not constant. Can any body tell where it come from and what it is? And does it differ in different screen sizes? If it is so how can we overcome this? I am asking this cause my player jump depends upon delta time and sometimes it jumps too high .....
The deltaTime
has nothing to do with screen sizes. It is the amount of time the last frame took to be rendered. Rendered in case of LibGDX also includes all the logic you execute in your render()
method.
Usually you want a game to run at the same speed on different devices. If your render method looks something like this...
public void render(float deltaTime) {
float speed = 5f;
position = position + speed;
}
...then the position would change faster on fast devices and less fast on slow devices, just because the render method is called a different amount of times.
To overcome this issue, there is the deltaTime
which in case of LibGDX is in seconds. To let the position change at the same rate on different devices, you usually do it like this:
public void render(float deltaTime) {
float speedPerSecond = 5f;
position = position + (speedPerSecond * deltaTime);
}
The float delta
in your render(delta)
method is the time between the last frame and this frame, given in seconds. You can get this value everywhere, by calling Gdx.graphics.getDelta()
.
This value is smoothed over n frames.
To get the real time (which is not really neccesary in most cases) you can call Gdx.graphics.getRawDeltaTime()
.
This value is used to have move the objects in the game, depending on the elapsed time between the frames, instead of moving them at a fixed amount of pixels. To do this you can simply mulitply your movementSpeed
with this delta
to get the distance your object should travel.
I would suggest to limit your delta time, as huge delta times (caused by slow devices or something else going on) can mess up for example collision detection. To limit it to, for example, the delta
time of 30FPS you can simply say delta = Math.min(delta, 1/30);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With