Hi all,
I wanted a more convenient way to do cooldown timers in my game (and just add numbers together at a certain rate without manually using delta time and without the hassle of using really small constants with it), so I made this little snippet:
public static double getConstant(double amount_to_add, double per_x_seconds) {
if (per_x_seconds <= 0) {
System.out.println("Time cannot be less than or equal to 0!");
return 0;
}
double time_in_mills = per_x_seconds * 1000;
double amount_to_add_per_frame = amount_to_add / time_in_mills;
amount_to_add_per_frame=amount_to_add_per_frame * deltaTime;
return amount_to_add_per_frame;
}
It will return a constant that increases/decreases a variable to the number you specify in the time you specify.
e.g.
double x = 0;
while (x < 5) {
x+=getConstant(5, 10); //in 10 seconds, x will be 5
}
It includes delta time as well, and you can easily change it to make delta a parametre if you want. I know this is simple but it is very useful to me and I wanted to share. Have a good day
edit: Added an error if the specified time span is <=0 (thanks, Riven!)