Spatial Partitioning on a Low Timebase or How to play Pong with 10 fps or Applied Path to Math

After going a bit through C++ basics with my older son, I dived into a Pacman homage to demonstrate basic Euclidian geometry. The whole game logic and details were covered in this blog. Of course, this was too big to digest in whole, so we pulled back and looked at a simple ball in freefall.

Freefall Timeframe Simulation

The beauty – or shall I say big source of error, of it is its simplicity in implementing the physics simulation using spatial (or timeframe) partitioning. So we just let the ball fall freely based on its gravity induced velocity until it hits the floor.

Since I had to add collision for general directions (w/ velocity) for good measure, we ended with something like the following 🙂

Above clip shows the tiny simulation @ 60fps, hence the delta between frames were small enough to give a realistic impression. Here is the same with debug graphics enabled, showing the velocity of each object and its tangents and reflection vector when colliding – all with its length reflecting the magnitude.

So far so good @ 60 fps, i.e. 16.67ms delta between the frames.
But how good is the math if we decrease rendering to 10 fps, i.e. 100ms delta between frames.

We can imagine that the objects have long passed a barrier until the update function calculates things. Well, spoiler, its not perfect but it works somewhat. At least it is fun to watch the flip-book animation showing the accumulated magnitude of things …


Pong Self Humiliation

So what has all of this to do with Pong as advertised in the title you ask?
OK, so I added the collision and vector math to our fun project for a reason, games!

Hence the first one most simple was pong, and at 10fps its quite hard to catch that ball 🙂

Sure, you can make life easy by simply using 60f fps as shown below.

While all of this is fun, it shows the limitations or difficulty of timeframe (or spatial partitioning) based simulation.
One may also ask: How much realtime is good enough?


Just in case anybody asks, the whole framework simply renders points, no more no less.
The point rendering sets pixels in a simple memory framebuffer, which then updates the underlying toolkit’s surface. In case of SDL, the latter is an SDL OpenGL texture usually. However, SFML is supported as well.

Reason to choose a completely agnostic rendering design is to demonstrate computer graphics and applied (vector) math in the first place, which these lessons are all about.

Currently my older son and I work and play on space wars on-and-off, but we got the asteroids somewhat right already 🙂 I can only recommend this path to math, as it immediately shows the impact and work applying it.