deltaFix , my solution to no deltaTime

edited July 2017 in Share Your Work

I have created a, kind of simple, formula for both emulating deltaTime and creating a 'game speed'; into a single variable. As I am not the best with math, I called the variable 'deltaFix', as it was my fix to the problem and also adds more to it.

I tried looking for information on using deltaTime, but read that it was a local variable that in inaccessible. So for being able to emulate it, I took a small amount of time to create it on my own for my needs and decided to share it because it is a bit more useful than 'just' normal deltaTime.

There are 2 variables that can be 'played with' to get the game running how you want it. FPS changes the screen's frameRate, and is used for determining 'deltaFix'. GAMESPEED is how fast you want the game running at (normal is 60, fast is 120, slow is 30), and also used to determine deltaFix. previousMilli is just a spot holder for the millis(), to compare frame start to frame start. deltaDiv is used as a small float placeholder to adjust the speed slightly to account for the time difference in frames (if frame 1 took a little longer to run, then frame 2 will be slightly adjusted by that)- so that the variable will not have a sudden drop due to frame rate slowing down (it is very minor, and unnoticeable; but useful if you had a sudden lag).

First step is to set the FPS (I usually set it to 120) and GAMESPEED (I usually set it to 60); before setup. Then in setup, I define frameRate to FPS, decide the size of the screen, then provide a previousMilli starting point (this will cause frame 1 to have a larger jump than every other frame.

inside draw, at the very beginning, I do my main operations for determining deltaDiv and deltaFix. deltaDiv is simply previousMilli divided by millis(), in order to get that minor (or by chance large) time difference between frames. deltaFix is a 2 step equation. First, we determine the base multiplier to properly give the GAMESPEED based on the chosen FPS. Then we multiply that by deltaDiv to apply the frame rate difference. All that is left is to make prebiousMilli = milis(), and the calculation portion is done.

To show an example of how it can be used, I made a simple square that moves in a singular pattern constantly, and multiplied it's movements by deltaDiv. So, while the gamespeed is at 120, I can freely adjust the FPS- and it will always be at the same point in space at the same point in time- no matter the FPS.

I am more then sure this can be made more accurate and faster; and has some flaws probably (I am an amateur programmer, with barely any memory of High School math from 2 years ago). I am actually using this formula in a game engine that I am developing using Processing, because why not- plus there is no game engine available that is up to date with processing currently.

final float FPS = 120;
final float GAMESPEED = 60;

float previousMilli, deltaDiv, deltaFix;

float x = 1;
float y = 1;
void setup() {
  frameRate(FPS);
  size(640, 480, P3D);
  previousMilli = millis();
}

void draw() {
  deltaDiv = previousMilli / millis();
  deltaFix = (GAMESPEED / FPS) * deltaDiv;
  previousMilli = millis();
  background(0, 0, 0);
  x += 1 * deltaFix;
  y += 1 * deltaFix;
  rect(x, y, 25, 25);
  if (x > 665) {
    x = 0;
  }
  if (y > 505) {
    y = 0;
  }
}

Comments

Sign In or Register to comment.