This is the perfect follow-up to your FPS video because viewers already understand what FPS is. Now you can explain why games use Delta Time.
๐ฌ How Delta Time Works in Games | Java Game Development
๐๏ธ INTRO (0:00โ0:45)
Hey everyone! Welcome back to Bit by Bit Dev.
In the previous video, we learned what FPS is and how to create an FPS counter in Java.
But there's a big problem.
If one computer runs your game at 30 FPS and another runs it at 120 FPS, should the player move at the same speed?
The answer is yes.
But if you write your code incorrectly, players with higher FPS will actually move much faster.
Today, we're going to solve that problem by learning one of the most important concepts in game development:
Delta Time.
Let's get started.
๐ฎ The Problem (0:45โ2:30)
Imagine you wrote this code.
Looks fine, right?
Every frame, the player moves 5 pixels.
Now imagine two different computers.
Computer A runs the game at 30 FPS.
That means the player moves:
5 pixels ร 30 frames = 150 pixels per second
Now Computer B runs at 120 FPS.
The player now moves:
5 pixels ร 120 frames = 600 pixels per second
Same code.
Different speed.
That's a huge problem.
The player shouldn't become four times faster just because the computer is faster.
๐ฎ Why Does This Happen? (2:30โ4:00)
Remember the game loop?
It keeps repeating over and over.
Every time Update runs,
our player moves.
More FPS means...
More Updates.
More Updates means...
More Movement.
That's why higher FPS makes the player move faster.
๐ฎ The Solution: Delta Time (4:00โ5:30)
Instead of moving the player a fixed amount every frame,
we move the player based on how much time has passed since the previous frame.
That amount of time is called Delta Time.
Simply put...
Delta Time is:
The time between two consecutive frames.
If your game runs at 60 FPS,
each frame takes around
1 รท 60
which is approximately
0.016 seconds.
If your game runs at 120 FPS,
each frame only takes
0.008 seconds.
Smaller frame time...
More frames.
Larger frame time...
Fewer frames.
๐ฎ Using Delta Time (5:30โ7:30)
Instead of writing
We write
Let's say
our player speed is
200 pixels per second.
If Delta Time is
0.016 seconds,
the player moves
200 ร 0.016
which is
3.2 pixels.
Now imagine the FPS increases.
Delta Time becomes smaller.
Maybe
0.008 seconds.
Now the player moves
200 ร 0.008
which is
1.6 pixels.
The player moves a shorter distance each frame,
but because there are twice as many frames,
the total movement over one second stays exactly the same.
That's the magic of Delta Time.
๐ฎ Java Example (7:30โ8:30)
Here's a simple example.
Without Delta Time:
With Delta Time:
If you're using LibGDX,
it's even easier.
LibGDX calculates Delta Time for you every frame.
You simply multiply your movement speed by it.
๐ฎ Real-World Example (8:30โ9:30)
Think about driving a car.
Speed is measured in kilometers per hour.
Not kilometers per frame.
If your car travels at 60 kilometers per hour,
it doesn't matter whether you check its position every second,
every half second,
or every millisecond.
The speed stays the same.
Delta Time works exactly like that.
It measures movement based on time, not on frames.
๐ฌ Conclusion (9:30โ10:00)
Now you know why professional games never move objects like this:
Instead, they always use Delta Time.
Today we learned:
- What Delta Time is
- Why FPS affects movement
- Why frame-based movement is wrong
- How Delta Time fixes the problem
- How to use Delta Time in Java
- How LibGDX makes it easy
In the next video, we'll build a proper Game Loop with Fixed Time Steps, the same technique used in many professional game engines.
If this video helped you understand Delta Time, don't forget to like the video and subscribe to Bit by Bit Dev.
Until next time...
Keep learning, keep building, one bit at a time.
See you in the next video! ๐