10

What is a Delta time in LIBGDX? I read many posts regarding that. To my knowledge, Delta time is,

  • The time gap between previous and current frame
  • delta time will add upto 1 since its the computation of x frames per second ie.(1/x of the frames)
  • To make the speed constant for the game we use dt

    If we say 60 *dt then it will move 60 frames per second, no matter what the speed of the mobile(for instance) is.

    So, this is what I know about delta time but I am not getting a clear view about it because , be it for an update or render method we are passing the delta time but where in the code we are specifying to calculate for PER SECOND?

For example,

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

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

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

so, I need answers for,

  • what the above code is implying??
  • What's actually happening behind the code?
  • Why are we doing so?
  • where in this code we are specifying it must move x frames per second like the previous above example?

    I can understand the render method is passing the delta time to the update method but I need some clear view about it. Sorry if the question seems stupid but it's really hard to proceed without actually knowing what's happening .Any help would be great !!

Anusha
  • 939
  • 3
  • 13
  • 31

3 Answers3

24

Gdx.graphics.getDeltaTime() is the time between the start of the previous and the start of the current call to render(). It is also the value you get in your Screen#render() method. That's it. No black magic or something. It just takes the current time and subtracts the previous time from it. The unit of this value is seconds. Note that it does not add up to one.

So if the previous time the method was called 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 (velocity) is measured in "units" per second, for example meter per second or short: m/s. If your car travels at 360 m/s and the time elapsed is 0.0165346 s, then the distance you've traveled in that time is 0.0165346*360 s*m/s => 5.952456 m, so almost 6 meters.

Note that this is basic physics, it is not specific to libGDX. If you find it hard to understand then you might want to read up on velocity.

To answer your bottom questions, which I guess are about splitting your render method into an separate update method.

  • The code is not implying anything
  • There is nothing behind the code
  • Using well defined short methods is usually a good practice, read: separation of concerns
  • No clue what you mean by "frame" but the velocity is multiplied by the time to get the distance
Xoppa
  • 7,983
  • 1
  • 23
  • 34
  • Thanks but can you please tell me why we are passing this time difference(delta time) to the update method? Why it is useful ? I understood explanation for my first example but why we are doing so for the second example? What it is trying to reveal when we pass delta time to update method ? How is it useful @Xoppa – Anusha Dec 28 '15 at 06:18
  • So you are asking why you have a delta time argument in your update method? That is a method you created, it is not part of libGDX, you don't have to do it that way. But usually you want to separate updating your game from rendering it (see my last link). Updating your game involves, e.g. setting the car at its new location and thus it needs the delta time to know the distance it traveled. It is useful because it is easier to read and maintain. You could safely move the content of your update method to your render method. Apart from that last link, you might also want to read up on OOP. – Xoppa Dec 28 '15 at 08:46
2

It's simpler than you're making it out to be. Delta time is how many seconds have passed since the last render call. Since displacement = velocity * time you multiply an object's velocity by delta time to get how far it has moved since the last render call. The velocity is in units per second. It can be a constant in your code or something you calculate if it's affected by acceleration. The distance units you use are up to you. They can be meters, pixels(not a good idea since various devices have different screen dimensions) or some other arbitrary distance unit.

It doesn't make sense to say delta time adds up to one. It's nothing more than the number of seconds since the last frame (so it is usually much less than 1.0).

It also doesn't make sense to say you want to move something in frames per second. You want to move something some distance units per second.

Your first code example is not compileable. But if you were to write something like

myObject.position.x += 5*dt;

This would mean your object moves horizontally at 5 distance units per second and so you are updating its horizontal position accordingly.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
0

Delta time, also known as frame time or time step, is the time elapsed between the current frame and the previous frame in a game or simulation. In LIBGDX, delta time is commonly used in game development to make sure that game elements move at the same speed regardless of the computer's performance or the number of frames per second (FPS) that the game is running at.

In LIBGDX, delta time can be obtained using the Gdx.graphics.getDeltaTime() method, which returns the time elapsed in seconds since the last frame was rendered. This value can then be used in game logic to update the position and behavior of game elements, ensuring that they move smoothly and predictably regardless of the frame rate.

For example, if you want to move an object at a constant speed of 100 pixels per second, you would multiply the object's velocity by delta time in each frame. This would ensure that the object moves the same distance regardless of how many frames are rendered per second

float speed = 100;
float delta = Gdx.graphics.getDeltaTime();
object.position.x += object.velocity.x * delta * speed;
object.position.y += object.velocity.y * delta * speed;
Emmanuel Mtali
  • 4,383
  • 3
  • 27
  • 53