Thursday, June 10, 2010

Virtual Hourglass

My second app, a virtual hourglass, is now on the Android market. This uses a granular physics model to calculate the motion of each grain inside the hourglass. The particle size can be chosen from large (runs the fastest) to small (runs the slowest). The grains respond to gravity, if there's a sensor on the device, so the hourglass can be turned over when it's run out.

The hourglass runs out in about a minute or so on my phone (HTC Desire) but takes longer on the Android emulator. So it's not that useful for timing but fun to watch! There's a clock in the top left hand corner for timing.

It uses a soft-sphere discrete element model. I think there are a few applications that use this in the Market already, namely bubble ones. Each particle is modelled as a slightly squashy sphere. At each step the forces on the particle from all of its neighbours is worked out. Then the speed and position of all of the particles is updated. Not surprisingly, this takes a lot of processing! So, it might be quite slow on early Android phones, although I'm trying to improve the speed. After the nightmare of OpenGL ES, I decided to use the 2D Android graphics library, which should work on more (all?) phones.

Update 1.1: I'm getting a lot of NullPointerException errors reported in this. It seems to be caused by re-registering the sensors, which is weird. I've changed the way I do this, so hopefully it will behave a bit better.

Touching the screen now freezes the grains under the point touched.

Also, I added a bunch of options for changing the springiness and damping of each grain. Springiness doesn't really seem to make much of a difference. The damping, on the other hand, really affects the time it takes for particles to drain out where a small amount of damping makes the particles run out a lot faster.

Update 1.2: Added a lot more options, including the ability to change the skin and particle colour. People seem to like being able to customise the way things look..

Update 1.3: Re-wrote and optimised a lot of the internal code for speed, managed to get around a 50% improvement. Also fixed a very annoying bug in onResume. Previously this called a function which re-registered the acceleration sensors, but on some phones this seemed to occasionally use a null pointer. Why? Who knows? The sensor stuff has been re-written and it seems like this has got rid of the bug.


  1. Great app - perfect for timing my son when brushing teeth!

  2. That's a use I'd never have imagined! Glad to hear my app's helping your son, thanks for the great feedback :)

  3. I want it for my daughter, too!
    But the market has it no longer...

  4. Sorry all! Google changed their TOS for the market, so all apps have to have a rating. I think this caused it to vanish. It should be back now, though.