Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delta value in render method libgdx

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 .....

like image 477
MGDroid Avatar asked Mar 14 '14 12:03

MGDroid


2 Answers

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);
}
like image 128
noone Avatar answered Oct 30 '22 00:10

noone


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);

like image 36
Robert P Avatar answered Oct 29 '22 23:10

Robert P