Hacker News new | ask | show | jobs
by Strilanc 4925 days ago
You can cut down the amount of error even further by not iterating. Instead of iteratively updating the height, just store the initial position, velocity and time. You still compute the current position as pi + vi * dt - a * dt * dt/2, but intermediate results are discarded to avoid compounding floating point errors.

Of course, you will need to update the initial position/velocity/time whenever the jump is interrupted or modified. The reduction in error is also quite small.

1 comments

That assumes that there are no other forces. Your solution doesn't handle momentum changes (throwing an object, or taking a bullet, or being close to an explosion) while in the air, and it assumes that there's no maximum or terminal velocity in the game. It also becomes trickier to compute when your objects hit other objects.

You say that "you will need to update the initial [state vector] whenever the jump is interrupted or modified" - that's exactly what numerical integration does. So your solution seems like it would use a closed-form solution for some cases, and numerical integration for others, which would make the dynamics code easily twice as complicated.

While switching from Euler to leapfrog integration is a couple of lines of change, for a better approximation.