Search Unity

Tiny Bubbles: Custom soap-foam physics in Unity

April 2, 2018 in Technology | 4 min. read
Topics covered

Veteran indie developer Stu Denman had a grandfather who had worked on the Manhattan Project, and afterward studied the physics of soap bubbles. Half a century later, Stu couldn’t get his grandfather’s bubble work out of his head. This is a story of how he developed custom physics in Unity to make a mesmerizing 2D game called Tiny Bubbles.

When Denman was considering new game ideas, he wanted to create something like the classic, Tetris: a game with no story, no art team, and no main characters, yet one that would stand out because of its powerful game mechanics.

His mind kept wandering back to his grandfather, a scientist who had studied the properties of plutonium and discovered similarities between molten metals and the unusual behavior of soap bubble foams.

“Soap bubbles have these weird properties where the edges meet at vertices. 2D soap foams connect on three surfaces, never four or two, and always at a 120-angle. I became determined to create a game using the physics of soap bubbles: how they connect, the surface tension and the pressure,” Denman says.

Really complicated math and soapy prototypes

In the first instance, Denman attempted to make a prototype based on mathematical theory.

“I had to teach myself this horrendous math,” he says. “I read a PhD thesis on non-linear Jacobian Matrix solvers and attempted to use it to reproduce the physics. But in the end, it didn’t behave correctly in all cases. I had to throw it out because it worked 99% of time, but for games you need it to work 100 percent of time.”

Denman was almost ready to give up when he realized that, instead of using mathematical theory, he could simulate what molecules do in nature on a scale that would work for games. So he studied the surface tension and molecular dynamics, for example, how one side is attracted to fat, one side to water. And he began a series of prototypes where he converted circles into soap foams.

This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.

Complex molecular dynamics in script

In order to get where he wanted to go, Denman had to write a custom physics engine in C# built from script via Unity’s powerful IL2CPP compiler. Because his molecular dynamics engine has to process force calculations for tens of thousands of molecules, it had to be carefully tuned to run as fast as possible, even on older mobile devices. Before the bubbles can be displayed, the molecules are “skinned” with dynamic meshes and the interior areas are triangulated. All this complex processing is achieved purely in C# script.

Denman was able to use tools like Apple’s Instruments to profile the C code generated by IL2CPP, running on the target iPhone. Based on these results, he was able to modify the C# scripts, so that the inner-loops of the dynamics engine would run at native speeds, even though it was authored in script.

After three days, he had made a solution that achieved realistic-looking animations, running at 60 FPS on mobile devices and was stable enough that the simulation never collapsed or exploded out to infinity. Denman, achieving the cool rubber-band effect he had observed in nature, where waves and undulations run through the bubbles.

Playing with bubbles pays off

Once he had his custom physics and the bubble editor, Denman could play around with the mechanics.

“Really what your doing is exploring your restrictions, the rules you’re going to give players, what I call the possibility space of what things you can create,” he says.”I tried out different bubble puzzles, threw out what didn’t work and kept what was fun. In short, Unity allowed me to play, and that’s what I love to do when I design games.”

So far, Denman’s playful experimentations have resulted in multiple awards, including SXSW Gamer’s Voice: Best Mobile Game, Google Indie Festival, Intel Buzz: Best Overall PC Game, Seattle Indie Games Competition, and Mobile Games Forum Indie Showdown and Best Quickplay at IMGA Global, First Runner-Up at Big Indie Pitch at GDC and Grand Prize at The Label’s Indie Showdown.

You can learn more about how Denman created his game in the Unity for 2D Tiny Bubbles case study.

April 2, 2018 in Technology | 4 min. read
Topics covered