using System.Diagnostics;
private const float deltaTime = 0.02f;
private const float tolerance = 0.01f;
public static void Main()
Stopwatch stopwatch = new Stopwatch();
DoDecelerationTest( 0.1f, stopwatch );
DoDecelerationTest( 0.25f, stopwatch );
DoDecelerationTest( 0.5f, stopwatch );
DoDecelerationTest( 1f, stopwatch );
DoDecelerationTest( 5f, stopwatch );
DoDecelerationTest( 10f, stopwatch );
DoDecelerationTest( 25f, stopwatch );
DoDecelerationTest( 50f, stopwatch );
DoDecelerationTest( 100f, stopwatch );
DoDecelerationTest( 500f, stopwatch );
DoDecelerationTest( 750f, stopwatch );
DoDecelerationTest( 1000f, stopwatch );
DoDecelerationTest( 2000f, stopwatch );
public static void DoDecelerationTest( float initialVelocity, Stopwatch stopwatch )
const float desiredDistance = 10f;
float deceleration = initialVelocity;
float delta = deceleration * 2f;
bool isRevisingDownward = false;
int maxIterations = 1000;
float distance = 0, velocity = initialVelocity;
while( i < maxIterations )
velocity = initialVelocity;
while( velocity > 0f && distance < desiredDistance )
distance += velocity * deltaTime;
velocity -= deceleration * deltaTime;
distance = desiredDistance + 1;
if( Math.Abs( distance - desiredDistance ) < tolerance )
Console.WriteLine( string.Format(
"For velocity of {0}, a deceleration rate of {1} is required ({2} simulated frames performed over {3} ticks).",
initialVelocity, deceleration, j, stopwatch.ElapsedTicks ) );
if( distance < desiredDistance )
if( !isRevisingDownward )
isRevisingDownward = true;
isRevisingDownward = false;
Console.WriteLine( string.Format(
"TIMEOUT — Velocity: {0}. After {1} simulations over {2} simulated frames, final distance was {3}, velocity was {4}. It took {5} ticks.",
initialVelocity, i, j, distance.ToString(), velocity.ToString(), stopwatch.ElapsedTicks ) );