When converting the game to HD, one of my initial decisions was to go ahead and port the whole thing to Unity. I love Flash (the former game engine), but it felt like it was on the way out – if not already out. Unity had better support, a larger community, and most importantly, made it easy to port to a large number of platforms. This has already proven useful, as I recently tested the game on my Steambox and it worked flawlessly. All I had to do was check the “Linux” checkbox on the Unity Build menu and it… just worked. That doesn’t happen terribly often, so I’m counting my blessings.
But Unity has other advantages – a lot of built-in tools to play around with! One idea I had was to try to mess around a bit with the physics engine. I figured I could do something neat like give Chris and the various helium balloons in the game ribbons and tie them down to things. It could make for a few fun interactive components, or be useful to the story in a few places.
I’d seen enough chains and ropes in modern games that I felt like this should have been a solved problem by that point. I figured there was probably a module in Unity called “chain physics” or “rope physics” or something and I could watch a tutorial or two and be done in an afternoon.
Boy was I wrong.
It turns out that Unity does have built-in support for this kind of physics, but it was surprisingly tricky to get working right. Unity does support something called “joints,” which come in different types, and attach objects to each other in different ways. Checking out various tutorials and articles, it looked like you could make a chain fairly easily by taking a number of chain links and linking them together with “hinge joints.” If you make sure all the objects have the proper weight and gravity, it seemed like the chain should “just work.” Since a ribbon or string could be thought of as a chain with lots of tiny little pieces, I figured I was golden.
Except I wasn’t. The chain links kept stretching past where they should physically be able to, or rotating in funny ways, or breaking apart. I tweaked settings over and over again, and occasionally I would get something that looked about right, but then it would do something else funky. I was starting to get frustrated. The fact that the chain pieces weighed downward but balloons float upward appeared to throw all kinds of monkey wrenches into the physics engine (or else I just had something very wrong in the setup that I never diagnosed).
But then I had a realization. Strings and ribbons don’t really weigh that much. In fact, shouldn’t their weight be essentially negligible? Maybe I was going about this entirely the wrong way.
Maybe, instead of thinking of a ribbon as a series of hinge joints, I should think of it as exactly one “distance joint” – a type of joint in Unity that prevents two objects from separating too far from each other. This joint seemed much simpler and worked much more reliably. The ribbon then, instead of being actual physics objects with mass and joints and so forth, would just be a simple colored line, drawn using Unity’s 2D line drawer.
This worked much, much better. However, there was still a problem, and it involved math. Drawing a ribbon when it’s stretched tight is easy – that’s just a straight line. But what if there’s some slack? The ribbon has to hang down in a curve. At first I thought I would break out my old high school Algebra II and try to figure out how to draw a parabola. I had two points it had to go through, and a maximum length. Surely I could do some algebra, solve a few equations, and math magic would ensue.
Well, it turns out my Algebra II was a bit rusty. Also, I wasn’t even sure that the parabola was the right shape. I struggled to find out even how to look up the problem online, not knowing the right names of things, until I finally found the word I was looking for:
You ever heard of a catenary? I hadn’t. It turns out it’s a term used a lot in architecture for things like suspension bridges — it’s the name of the curve that’s formed by a rope or chain that’s held slack. It was exactly what I was looking for, and there was a whole equation for it, already there on the internet for my convenience. I didn’t have to derive anything myself.
I found some pseudocode for it on the internet, ported it to C# in Unity, and lo and behold, I had reasonably convincing looking ribbons! Woo hoo!
Once I had the ability, I came up with a few other ways to use it – including in a brand, new level you’ll get to see in the demo.
Not that I necessarily want you really scrutinizing the curve of hanging strings, but I do think it turned out pretty well in the final product. Let me know what you think!