The Mathematics of Catenary

Many modern games feature hanging wires, cables and chains; this series of tutorials will explore the mathematics behind their shape, which is known as catenary.

You can find the Unity package to create catenaries in Unity at the end of the post.

An Introduction to Catenaries

Out of the many mathematical objects that have been studied and described, there is one that is very dear to many game developers. And yet, only a small number of them actually know its name: catenary.

A catenary is the shape that a rope or chain will naturally converge to, when suspended at its ends. It is not a coincidence that the name catenary itself comes from the Latin catenaria—which indeed means chain.

Modern games feature an increasing number of run down facilities and destroyed environments. And most comes with their fair share of hanging wires. Such as the ones seen in GLaDOS’ room in “Portal”, or “Half-Life: Alyx”, just to name a couple.

Because catenaries occurs everywhere around us, we have grown accustomed to their shape. This also means that it is very easy to spot when something is not hanging the right way. Like skin-complexion and cloth-physics, a wrong catenaries are hanging over an uncanny valley of their own.

And yet, so many games are getting catenaries wrong! The reason, however, is not surprising. While they are so easy to generate in the real world, their mathematical definition is made of the same substance of nightmares. Exception made for a few special cases, there is no “easy” equation to generate a catenary; at least not in the form that we need to properly decorate a level.

One common ways of creating physically-based catenaries “for free” is to use rigid bodies and hinge joints to create chains and ropes. This has the extra benefit of making them reactive to the player’s interaction, but at the cost of being computationally expensive. Most hanging wires and cables are part of the background, and using physics to create them would be too expensive. Consequently, being able to place static catenaries with no run-time cost is pretty critical.

On top of that, drawing catenaries comes with an additional benefit. Let’s imagine that you want to create an actual, physically-driven hanging wire for your game. How do you place the wire segments, when instantiating them? Many developers would simply place them along a line, letting the physics engine do the work for them by finding an equilibrium state. Drawing catenaries allows you to initialise physically-driven wires and cables in their equilibrium state, without having to wait for them to settle into position by themselves.

It is worth noticing that while Unity does not offer any build-in tools for cables and chains, Unreal Engine comes with a Cable Component that solves exactly this problem through a technique called Verlet Integration (which, incidentally, will be the topic of a future series). And in case you are into shaders, Ross Beardsall recently came up with an ingenious solution to simulate coiled cables in Unreal Engine 4…

A Formal Definition

If we want to get physically-accurate catenaries, perhaps is best to start from the beginning. The simplest catenary one can image has a well-defined equation, which almost entirely relies on \cosh, the hyperbolic cosine:

(1)   \begin{equation*} y=a \cosh{\left(\frac{x}{a}\right)}\end{equation*}

The catenary equation has a parameter, a, which changes the overall “wideness” of the curve. However, all catenary curves are similar, as they are all scaled versions of each others. The interactive widget below allows you to see how the curve changes as a function of a.

 
a
2

❓ What is the hyperbolic cosine?

❓ Show me the derivation!

❓ What are catenaries used for?

📰 Ad Break

Parametrising the catenary

If we want to draw physically sound catenaries, (1) might not be the best way to do it. The reason is simple: asides from a, there is very little control on where and how to place it.

A more “customisable” equation is (2), which allows to move the curve horizontally and vertically using two additional parameters, p and q, respectively:

(2)   \begin{equation*} y=a \cosh{\left(\frac{x-p}{a}\right)}+q\end{equation*}

Ideally, however, we would like the equation of a catenary that passes by two anchored points, P_1 and P_2:

    \[\begin{split}P_1 & = \left(x_1, y_1\right) \\P_2 & = \left(x_2, y_2\right)\end{split}\]

The parameter p allows to move the vertex of the catenary; when p=0, the vertex rests on the Y axis. We could shift the catenary so that its vertex is exactly in between P_1 and P_2, like this:

    \[p=\frac{x_1+x_2}{2}\]

This is actually perfect if both P_1 and P_2 are at the same level (i.e.: when y_1=y_2). But they are not, the resulting catenary would be physically incorrect. Also, this leave us with very little control over how “saggy” the catenary is. This is why we need to approach the problem differently: we need a more “artistic” way to control the catenary.

Solving the catenary problem

The following section will show the equations of a physically correct catenary that represents a rope anchored at two points in space, P_1 and P_2, with a given length l. The mathematical derivation is rather convoluted, so I will present just the solution to avoid you countless nights of terror.

First, let’s define two auxiliary parameters, h and v, which measure the horizontal and vertical distance of the two points.

(3)   \begin{equation*}\begin{split}h & = x_2 - x_1 \\v & = y_2 - y_1 \\\end{split}\end{equation*}

For this derivation, we are assuming that y_1 < y_2; if that is not the case, you can simply swap the two points. Also, we need to assume that l is greater than the distance between the two points. This seems only fair, since the length of the catenary must be, at least, the distance betweetn the anchors.

The resulting values for p and q are:

(4)   \begin{equation*} p=\frac{x_1+x_2-a \ln{\left(\frac{l+v}{l-v}\right)}}{2}\end{equation*}

(5)   \begin{equation*} q=\frac{y_1+y_2-l  \coth{\left(\frac{h}{2a}\right)}   }{2}\end{equation*}

where \coth is the hyperbolic cotangent, defined as:

    \begin{equation*}\coth{x}=\frac{\cosh{x}}{\sinh{x}}\end{equation}

With the interactive chart below you can move the second anchor point around (P_2) and change the length of the chain (l) to see how the shape evolves.

 
P_2^x
5
P_2^y
5
l
25

Finding a

The section above failed to provide an equation for the first parameter of the catenary: a. This is because in this scenario, there is no closed-form solution that can calculate its exact value. In fact, any attempt to derive a results in the following transcendental equation:

(6)   \begin{equation*} \sqrt{l^2-v^2}=2 a \sinh{\left(\frac{h}{2 a}\right)}\end{equation*}

To put it simply, this means that we cannot rearrange this equation in a simple form such as a=... where a does not appear on the right-hand side as well. Sometimes it is possible to rewrite transcendental equations in such a form, but that often requires an infinite numbers of operations (such as a series or an integral).

When this happens, it means that we need to a different approach to calculate the value of a. When analytical tools fails, we resort to numerical ones. Which means that we need to use an algorithm to find an approximated solution.

The next post in the series will explore how we can do that.

What’s Next…

This first post introduced catenaries, the mathematical objects used to model hanging chains. The next one in the series will explore how to implement them in a game engine such as Unity.

Download Unity Package

Become a Patron!

There are two different Unity packages available for this tutorial. They contain a simple library to draw efficiently catenaries, which you can use in your games. Both packages are available through Patreon.

The Standard package one contains the scripts to draw catenaries in 3D and 3D, along with a test scene. The Advanced package contains support for rigged models (such as corded cables or chains), along with some advanced code to sample catenaries uniformly.

FeatureStandardAdvanced
Catenary 2D
Catenary 3D
Rigged Model Support
Cable Shader

Comments

3 responses to “The Mathematics of Catenary”

  1. Hi Alan,
    I am a student in Year 13 interested in the derivation and parametrisation of the catenary curve formula. You stated that the derivation to solve the catenary problem is rather convoluted but I would love to read about it and I can’t seem to find the information on the internet. Would you be able to share the derivation with me, or the source you used to find p and q?
    Thank you so much for your epic article!
    – Mia 😀

  2. […] Part 1. The Mathematics of Catenary […]

Leave a Reply

Your email address will not be published. Required fields are marked *

">