How to apply a factor over multiple frames?


am trying to do something that should be quite common in games: Applying a factor over a certain amount of time.
For example let’s say a number should become a quarter of itself every second.

How do we do this with variable frame rate?

I have written the following code:

using System;
                    
public class Program
{
    static double NthRoot(double x, double n)
    {
        return Math.Pow(x, 1.0f / n);
    }
    
    public static void Main()
    {
    double input = 10;
    double factor = 0.25;
    
    Console.WriteLine("Direct result: " + (input * factor));
    Random rnd = new Random();

    // simulate everything five times
    for (int i = 0; i < 5; i++)
    {
        double val = input;
        
        double total_time = 0;
        // simulate the frames that accumulate to one second
        while(total_time < 1.0)
        {
            // simulate a delta time for the current frame
            double delta = rnd.Next(50, 100)/1000.0;

            total_time += delta;
            double current_frames = 1.0/delta;  
            
            // apply the current factor
            val *= Program.NthRoot(factor, current_frames);
        }

        Console.WriteLine("Result over one second: " + val);
    }
}

It produces a random delta (as if frame rate were fluctuating) and calculates a special factor for every frame based on the framerate derived by this delta time.

However the results are inaccurate and tend to be too small:

Direct result: 2.5
Result over one second: 2.37171078785113
Result over one second: 2.36842517649817
Result over one second: 2.4655817612334
Result over one second: 2.34555299125168
Result over one second: 2.38820734095609

Here a Fiddle for the code: https://dotnetfiddle.net/1tEXba

How does one do this more accurately?

Huge thanks!



Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email