I am decreasing my velocity by 50% every second using a guide I found online. I am using the code here and it gives the right result but only for very small values for dt:
float dt, velocity, position;
dt = 0.002f;
velocity = 1.0f;
position = 0.0f;
for (float t = 0.0f; t <= 1.0f; t += dt) {
position += velocity * dt;
velocity = lerp(velocity, 0.0f, 1.0f - std::pow(0.5f, dt));
}
printf("dt = %f, p = %f, v = %f\n", dt, position, velocity);
dt = 0.09f;
velocity = 1.0f;
position = 0.0f;
for (float t = 0.0f; t <= 1.0f; t += dt) {
position += velocity * dt;
velocity = lerp(velocity, 0.0f, 1.0f - std::pow(0.5f, dt));
}
printf("dt = %f, p = %f, v = %f\n", dt, position, velocity);
return 0;
But I am getting roughly the right velocity BUT two different final positions back when I use two different delta times (not due to floating point issues):
dt = 0.002000, p = 0.722848, v = 0.499308
dt = 0.090000, p = 0.784219, v = 0.473029
How can I fix this difference inside of my update loop?
How do I account for the extra acceleration between timesteps?