1. Magnus Hoff
  2. android-metaballs


Clone wiki

android-metaballs / Home

Android Metaballs

Watch the video on YouTube!

Run it yourself!

You can for example use the "Google Goggles"-application available on the Market to scan this QR-code, or browse here on the phone, and click the image to follow the same link.

You will need Android 2.0 or newer, with SDK version 5 or higher.

In practice:

  • It runs beautifully on HTC Legend
  • It runs slowly on HTC Desire (and therefore probably the Nexus One), because of the much higher resolution. The amount of work per frame is directly proportional to the count of pixels on the screen. Compared to the Legend, the Desire has 2.5 times the amount of pixels, but just 1.7 times the cycles per second
  • I have also seen it look more like copper bars on one phone. I have no idea why

Good luck :)

The implementation

The implementation is a bit hairy, since it is basically just me banging the hello-gl2 example in the Native Development Kit (NDK) into submission.

The NDK is in use because the program needs GLSL shaders, which are only available since OpenGL 2.0, which is only available through the NDK as of right now. The downside of using the NDK is that it isn't future compatible in case of new processors, since the code is indeed compiled down to machine code.

All the interesting code is in jni/gl_code.cpp. It is a pretty straight-forward metaballs-implementation, and you can read about those everywhere. The only exciting things are that the balls have colours, and that the space is a torus -- when a ball exits the screen, it comes in at the opposite end.


In order to get colours, I first assign one colour to each ball. The contribution from each ball to each fragment/pixel is then multiplied by a four dimensional vector (vec4), where the first three elements are the colour, and the fourth element is set to 1.0. The fourth element -- let's just call it alpha -- is then equal to the traditional metaballs value.

When the summation of contributions from each ball is done, the colour value is divided by the alpha, in order to normalize it. Now, the accumulated colour is an interpolation between the colours of the metaballs, but the brightness of it is constant.

A function of the alpha is then multiplied in, to get the threshold effect.

Future prospects

This is just a kind of proof of concept. I don't see myself working much more with this project. But it would be cool if you extended it! And I'll gladly accept patches :)

Random ideas:

  • Interactivity: The ability to drag balls with touch input
  • Interactivity: The ability to create a new gravitational point by poking at the screen

Comments, feedback, etc

Comments are very welcome. Please raise an issue (or send a bitbucket message) if you have any :)