# When are Lagrangian Mechanics useful in a game?

I have not used or heavily studied Lagrangian Mechanics before and I have not built a physics engine modeled with constraints yet.

I understand that Lagrangian Mechanics are an alternative way to think about Newtonian Mechanics (standard kinematics equations) in-terms of constraints, does this actually come into practical usage in a game engine?

I've been getting started with constraint-based resolution with this tutorial: http://gamedevelopment.tutsplus.com/tutorials/modelling-and-solving-physical-constraints--gamedev-12578

In addition I'm going through Erin Catto's white paper "Iterative Dynamics with Temporal Coherence": http://www.bulletphysics.com/ftp/pub/test/physics/papers/IterativeDynamics.pdf

• Just to give you some initial food for thought, consider the differences between both and the main advantages of Lagrangian Mechanics in general programming: physics.stackexchange.com/questions/8903/… – MAnd Feb 26 '16 at 9:25
• @MAnd I looked over that post before starting mine up and there's a lot of useful information and examples. However, at least maybe due to inexperience, I haven't personally made a connection to anything I saw in that article to anything I could practically implement in code for a game. – James Linden Feb 26 '16 at 10:56

Lagrangian mechanics is nice for a few different reasons:

• You can add constraints directly into the differential equation which leads to the equations of motion of a body.
• You can use whatever coordinate system best suits your purpose, so long as you can represent an object's potential and kinetic energy in terms of those coordinates.
• It's not too hard to take into account many particles interacting at once, even though it can be computationally expensive - which would be a problem no matter what method you try to use to analyze the system.

Let's look at some cases where these could be useful.

1. As I know you're aware, constraints are easy to add in to the Euler-Lagrange equations, i.e. the equations of motion from a Lagrangian. You simply add in another term, a sum of all the constraints. It's essentially an application of Lagrange multipliers, if you've used those before. In a game, this can be useful if you want a players to move on a certain surface or path (at least one Game Development question has asked for something like this, Specific path for ai for follow). Here, Newtonian mechanics is very close to useless. Go for the Lagrangian approach.
2. Say you have a character swinging on a pendulum. If you've done some basic mechanics, you probably know how to approach the system in terms of Newtonian kinematics. This is often simple. But what if you have multiple pendula? The equations of motion for, say, a double pendulum are nasty, but they can be computed numerically . . . if you can come up with the suitable Lagrangian. Doing a Newtonian analysis in terms of forces would be much, much harder.
3. Swinging on ropes can also be modeled using Lagrangian mechanics. For instance, you could approximate a rope of length $$\l\$$ as a bunch of $$\N\$$ tiny pendula with lengths $$\l/N\$$. In the limit as $$\N\$$ goes to infinity, you have a very good simulation of a rope of constant length. Do you want Mario to swing from one tree to the next on a vine? This kind of thing can work well. You could also use a Lagrangian density if you want to study the rope as a continuous object, without breaking it into discrete pendula. Rope physics as a whole can actually be described using a Lagrangian. It is the simplest way to approach the general catenary problem (see also a case using the Lagrangian density). Static or moving ropes can both be studied this way.
4. Do you want to navigate an field of really massive asteroids, in a spaceship? Well, if the asteroids are going to influence the ship via gravity (in real life, they won't), you have some options. Here, you could use the Newtonian approach of forces, but that can be a little annoying. Alternatively, you could create a Lagrangian and figure out the potential energy at any point, then simply find the equations of motion. If you assume the asteroids are somehow fixed (again, not realistic), then this becomes less tricky.
5. Actually, you can extend the previous example using any type of potential. Do you want your potential energy to be proportional to the distance between two objects raised to the 14th power? What about something more complicated, like $$\14e^xx^2\sin x\$$? Or what if the potential is somehow tied to the target object's velocity? All of this can be taken into account in your Lagrangian, and the Euler-Lagrange equations can be derived without too much trouble. Solving them numerically would be a pain, but so would doing it any other way.

Basically, Lagrangian mechanics isn't just useful in terms of constraints. It makes finding the equations of motion much easier, in any coordinate system you want, for any potential you want. Sometimes, that can be really helpful - and much better than having to constantly split forces up into different vectors.

You also may want to look into Hamiltonian mechanics. The Hamiltonian, $$\H\$$, is the Legendre transform of the Lagrangian, $$\L\$$, and results in a pair of coupled differential equations. I think most people would prefer to use Lagrangian mechanics; if you really want to, you can use Hamiltonian mechanics, but some applications may be trickier.

• Really great answer! – Drew Cummins Mar 15 '17 at 19:58