Wednesday, 9 April 2014

Well Hello Mr Euler

The big no no of Euler integration is right out of Physics 101.  Briefly, the method looses accuracy as the time differential gets larger, due to its inherent assumption of constant acceleration.

When the timestep is very small and constant, this error is negligible. However, in computer games we can not guarantee the uniformity of the time step very well and, for most real objects, acceleration is not constant.

For something like the ball, which we want to behave pretty realistically as a physical object, it's probably a good idea to use a better integration method. If you are interested in the technical details, google "Verlet" and "RK4."

Anyway, for a game as simple (relatively) as Senseless Soccer, the Euler method is *almost* good enough. But you do get the odd visible hint of its shortcomings ever now and then due to the frame rate/time step measurement fluctuating for whatever reason. It manifests in the form of players running through the ball, the ball looking like it just teleported and bouncing off the posts too late or too early etc.. So lets implement a slightly more robust method for the ball.

Contrary to how most of the online tutorials come across, it's quite simple!! To smooth out the errors we simply take the velocities across 2 time steps, and average them to get a better estimation of what the current position should be.

The other methods average the values across a larger sample for even greater accuracy, but for our purposes this improved Euler should be fine. It may be some sort of psychological confirmation bias, but I think the ball looks more realistic already!

Incidentally, for the movement of the players we stick to simple Euler integration (of course the other methods would work fine too, but just aren't necessary). This is because the players always move with a constant velocity. There is no acceleration involved. It would be possible to implement a game where the players do have an acceleration component (so they couldn't change direction or stop immediately) but if you imagine the space ship in Asteroids as a player, you can see how infuriating that gameplay mechanism would be for a footy game like this!

1 comment:

  1. On a side note, Goal! (the spiritual successor to KickOff 2) implemented movement with inertia and actually it wasn't infuriating at all. It was a stonking great game.