What is Delta time in LIBGDX

What is Delta time in LIBGDX? I read a lot of posts about this. As far as I know, Delta time,

  • The time interval between the previous and current frame
  • Delta time will add up to 1 since calculating x frames per second, i.e. (1 / x frames)
  • To make the speed constant for the game, use dt

    If we say 60 * dt, then it will move 60 frames per second, regardless of what mobile speed (for example).

    So, this is what I know about delta time, but I don’t get a clear idea about it, because, whether for the update or rendering method, we pass the delta time, but where is the code that we specify for calculation for the SECOND?

For instance,

public void update(float dt) { float distance +=2*dt; } 

Will this code move 2 frames per second? If so, what will the code below do?

  public void update(float dt) { .... } public void render(float delta) { update(delta); } 

so i need answers

  • What is the meaning of the above code?
  • What actually happens behind the code?
  • Why are we doing this?
  • where in this code do we indicate that it should move x frames per second, as in the previous example above?

    I understand that the render method passes delta time to the update method, but I need some idea about it. Sorry if the question seems silly, but it's really hard to continue without knowing what is going on. Any help would be great!

+5
source share
2 answers

Gdx.graphics.getDeltaTime() - time between the beginning of the previous and the beginning of the current render() call. This is also the value that you get in the Screen#render() method. It. No black magic or anything else. It just takes the current time and subtracts the previous time from it. unit of this value is seconds. Please note that it does not add up to one.

So, if the method was called the previous time, it was at 6:51:30.0159512 pm , and the current time it is called is at 6:51:30.0324858 pm , then the difference is 0.0165346 seconds .

Speed ​​(speed) is measured in units per second, for example meter per second or short: m/s . If your car travels 360 m/s , and the elapsed time is 0.0165346 s , then the distance you covered during this time is 0.0165346*360 s*m/s => 5.952456 m , so it’s almost 6 meters.

Please note that this is basic physics, it is not specific to libGDX. If you find it difficult to understand, you can read on velocity .

To answer your bottom questions, I think it's about splitting the rendering method into a separate update method.

  • The code does not mean anything.
  • There is nothing behind the code.
  • Using clearly defined short methods is usually good practice. Read: Separation of Problems.
  • I don’t know what you mean by "frame", but the speed is multiplied by the time to get the distance
+15
source

It is easier than you do it. Delta time - how many seconds have passed since the last render call. Since displacement = velocity * time you multiply the speed of the object by the delta time to get how far it has moved since the last render call. Speed ​​is in units per second. It can be a constant in the code or what you calculate if it affects acceleration. The distance you use is up to you. They can be meters, pixels (not a good idea, since different devices have different screen sizes) or some other arbitrary block of distance.

It makes no sense to say that the delta time is up to one. This is no more than the number of seconds since the last frame (therefore usually it is much less than 1.0).

It also makes no sense to say that you want to move something in frames per second. You want to move something a few units per second.

Your first sample code does not compile. But if you should write something like

 myObject.position.x += 5*dt; 

This means that your object moves horizontally at a distance of 5 units per second, and therefore you update its horizontal position accordingly.

+1
source

Source: https://habr.com/ru/post/1239289/


All Articles