Java Demo-- Flying Saucers

You need a Java-enabled browser to view this applet.

The bouncing balls above show pinned addition, using a table. We want to overlay each flying saucer on top of the rest. Just adding each saucer to the pixels already in the buffer works, but quickly overflows the pixel values. This leads to ugly wraparound effects.

The solution is to use "saturating arithmetic" (aka clamped or pinned arithmetic). Here, 1+2=3, but 100+200=255. We can implement saturating addition using a table-- the index into the table is the raw sum; and the value of the table is that sum, pinned into [0,255]. (I use one table for saturating addition as well as subtraction,by biasing everything at the beginning).

The only other interesting part of the saucers above is that they bounce based on actual 2D physics-- each saucer has a position, velocity, and acceleration (imposed by gravity). It's nice that nature follows such simple differential equations!

Link to the source code.


Back to Demo List.