Warning: Constant FORCE_SSL_ADMIN already defined in /home/danieltzy/public_html/dblg001/wp-config.php on line 94
Programming Archives - Daniel's Blog

Delta Time – The Road to Frame Rate Independence

The word “delta” can be defined as the difference between two things or values. This was a word I first came across through physics, with delta(represented by a triangle) describing the change in a certain value. So from there, we can draw the conclusion that delta time is the change in time. In game development terms, it’d specifically represent the time that passes between each frame drawn. 

Why do we need delta time?

Delta time allows us to achieve what is known as “frame rate independence”. That means,
regardless of how low or high your framerate is, the rate at which things occur
or are updated(for example, the change in object positions according to
velocity, the rate at which an object’s velocity increases) remains consistent
throughout the game. 

For example, if we were to be updating a flying object’s position by a velocity of 20 pixels per second toward the north, having frame rate independence would help us ensure that regardless of whether we’re getting 30 frames per second on one computer or 60 frames per second on another, the position of that flying object on both computers would remain the same after each second. 

How do we implement it?

Let’s continue building off of the example that we used above. Our goal is to update an object’s position so that they move 20 pixels toward the north every second. 

First things first, let’s take delta time. This can be done by dividing 1 by the player’s frame rate, giving us the time between each frame(let’s say that value is 1/60 because the player’s frame rate is 60fps, do take note that in the real code implementation you won’t know what the frame rate is, you need to take the frame rate with whatever function your library or your engine supports). 

Now, we multiply the velocity of 20 pixels per second to the north by delta time, which reduces the velocity so that after 60 frames, i.e a full second, the object’s position would have moved by approximately 20 pixels towards the north. 

In this case, our object’s position would be updated by 0.3333 pixels towards the north every frame. So by the end of 60 frames, our object would have moved by 0.3333 * 60 = 19.998 pixels toward the north.

Below you’ll find the full representation of this process in pseudocode.

player_frame_rate = get_fps()
delta_time = 1/player_frame_rate

velocity = Vector2.new(0, 20)
object.y += velocity.y * delta_time

Alternatively, you can just divide the velocity by the frame rate because

y * 1/60 = y/60

Let’s say our game was being run on a lower-end computer and we were getting 30 frames per second. Let’s step through our code and see what happens. Intuitively, we know that if we’re getting only 30 frames per second, our object will have to move faster each frame compared to when we had higher frame rates. This is because we have less frames to display the journey of the object’s position to our target destination, and therefore we need to increase the distance our object moves each frame. Let’s check if our code does end up following that trend. 

First, with a frame rate of 30 fps, our delta time will now be 1/30. When we multiply 1/30 by our velocity of 20 pixels to the north, we get 1/30 * 20 = 0.6666 pixels toward the north every frame. That’s double the velocity we had previously with 60 frames per second. 

We now know that our code works and it indeed follows the inversely proportional trend between frame rate and update speed. 

Static Keyword in Java – What in the world is it?

Is a question many young and naive Java developers have that often go unanswered due to overly-complicated explanations that are rather obscure, and not geared towards beginners.

Here(not just this article/post but on the behalf of any subsequent tutorials), I aim to provide a quick yet effective definition of what this keyword actually means, and what it can be used for.

A Quick Definition

The static keyword tells Java that a method or variable in a class should be made accessible without the need of instantiation. Therefore, it’s a part of the class itself, rather than an attribute that you should be able to modify for each object you create.

Example - How can I use it?

Let’s create an application that categorises users into their age groups, based on their age input. These age groups consist of seniors, middle-aged, and youngsters. 

Let’s start by creating a class for each of them, and group them into a package just for demographics.

package org.demographics;

public class Senior/MiddleAged/Young {
public static int ageGroup = ??;
...some more code dedicated to each class...
}

Now, let’s create the main class that will make use of these classes and make up the logic of the application itself.

// Main class where we specify the logic
import org.demographics.Senior;
import org.demographics.MiddleAged;
import org.demographics.Young;

public class Categoriser {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age");

int age = scanner.nextInt();
String ageGroup = "";

if (age >= Senior.ageGroup) {
ageGroup = "Senior";
…do stuff, instantiate Senior and add to an array, etc…}

else if (age >= MiddleAged.ageGroup) {
ageGroup = "Middle Aged";
…do more stuff…
}

else if (age >= Young.ageGroup) {
ageGroup = "Young";
…do more stuff…
}

else {
System.out.println("You are too young!");
return;
}

System.out.println("You shall be categorised as " + ageGroup);
}
}

Reminder

Remember, we made the ageGroup variable static because:

1. We want to be able to access it without or before instantiation, otherwise we’d have to do: 

Senior placeholderSenior = Senior();
int seniorAgeGroup = placeholderSenior.ageGroup;

2. It is clearly established for all objects created from that class, and can’t be modified for any exceptional object. We don’t want two Senior objects having an age group of 10 years old and the other 60 years old.

Note

Do note that static variables aren’t constant(some may misunderstand it to be), rather it is consistent across all its instantiated objects. It can be changed, but the change will be reflected across all objects instantiated from it.

Programming Plans for The Next Few Months

After giving a lot of careful thought and consideration, I have decided to continue my programming journey by learning more about Python(by taking on different programming challenges with native Python), learning Web Development with Flask(for now, may switch to Django) and game development with Unity 3D.

First of all, I came upon this decision because of three primary reasons.

  1. I think that it’s good for me to stick to a specific language that I’m already comfortable with and improve on it
  2. I will be taking Computer Science for my O-Levels next year and it just so happens that the syllabus is more focused on webdev as the Pre-Release project is actually based on creating a web application/service. Therefore, I think it’ll be beneficial for me to have my own little side projects, so that they may prepare me for the final exams. I also happen to like creating websites.
  3. I really love creating games and I don’t think I will ever give it up. In the future, I aspire to be a hobbyist game developer while working a full-time job as either a software engineer or AI engineer. I just love the fact that gamedev is a mix of creativity(in terms of creating beautiful graphics and music), programming, story-writing and etc.

Anyways, that’s the end of my short and brief update!