What is a Delta time in LIBGDX

北城余情 提交于 2019-12-04 06:54:00

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!