I found a formula for gravity and jumping in this article: http://error454.com/2013/10/23/platformer-physics-101-and-the-3-fundamental-equations-of-platformers/
I implemented this formula and proceeded to do some test calculations, but they yield incorrect results. I expected the outcome to be 4
, but the test resulted in 3.8
. What am I doing wrong? How do I get the number 4
?
float deltaTime = 1f / 50f;
float maxJumpHeight = 4;
float timeToApex = 0.44f;
float gravity = (2 * maxJumpHeight) / (timeToApex * timeToApex);
float jumpHeight = 4;
float jumpVelocity = (float)Math.Sqrt(2 * gravity * jumpHeight);
float velocity = -jumpVelocity;
float position = 0;
while (velocity < 0)
{
velocity += gravity * deltaTime;
position += velocity * deltaTime;
Console.WriteLine("Position = {0}", position);
}