language: C# (mono-2.8)
date: 555 days 13 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
 
namespace FrictionTest
{
    class Program
    {
        static readonly Random Random = new Random();
 
        static void Main(string[] args)
        {
            const float friction = 0.95f;
 
            float value = 1000f;
            Console.WriteLine(ApplyFrictionImmediate(value, friction));
 
            float totalTime = 0f;
            while (totalTime < 1f)
            {
                float dt = RandomFloatBetween(1/60f, 1/40f);
                value = ApplyFrictionOverTime(value, friction, dt);
                totalTime += dt;
            }
            Console.WriteLine(value);
 
            Console.ReadKey();
        }
 
        static float RandomFloatBetween(float min, float max)
        {
            return (max - min)*(float) Random.NextDouble() + min;
        }
 
        static float ApplyFrictionImmediate(float value, float friction)
        {
            return value * friction;
        }
 
        static float ApplyFrictionOverTime(float value, float friction, float dt)
        {
            return value - value * (1f - friction) * dt;
        }
    }
}