Metaballs simulation in C++

Markus Buchholz
3 min readMar 12


We can think of the metaballs as organic-looking n-dimensional isosurface shapes moving in space. The most inspiring feature is their ability to meld together when in close proximity to create single, contiguous objects.
In the following article, I will shortly explain how the algorithm works and how to simulate the behavior.

The implementation of algorithm I deployed in C++ with the visualization in ImGui. The introduction to the C++ ImGui and information on how to compile and build programs you will find in one of my previous articles.

The source code you will find on my GitHub.


The isosurface (here in 2D space) can be considered as a space of pixels whose color depends directly on the defined function. The function can have different implementations, however in computer graphics is related to the position in the space the objects (here balls with randomly generated radius) are moving.

We can imagine that if the function of pixel color depends directly on the position on the canvas, for example,

so we can expect the following output.

The function affecting the pixel color depends on the pixel position on the canvas (by author)

The typical function chosen for metaballs is the inverse-square law, that is, the distance between initiated the ball and the particular pixel on the canvas. As we can imagine while the ball is moving the function return values depending on the current position of the ball.


The simulation is straightforward (see the source code). for the purpose of this article, I initialized the 5 balls with random sizes, start positions, and directions. While the balls are in motion, the function affecting the color of each pixel of the space is changing. The trick here is the function affects the color model in HSV which I map to RGB. See the result below.

Moving metaballs (by author)

Thank you for reading.