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

  • 1
    \$\begingroup\$ 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/… \$\endgroup\$
    – MAnd
    Commented Feb 26, 2016 at 9:25
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Feb 26, 2016 at 10:56

2 Answers 2


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.

  • \$\begingroup\$ Really great answer! \$\endgroup\$ Commented Mar 15, 2017 at 19:58

Lagrangian mechanics is useful for deriving the equations of motion for any system in a simple and structured way. Lagrangian mechanics will result in the same equations of motion (which is what describes how everything moves) as Newtonian mechanics, and once you have the equations of motion, you will probably only be using those in your game and not Lagrangian mechanics.

For simple systems in which you know the forces acting on all objects, Newtonian mechanics is probably going to be easier to use. However, for more complex systems, which may involve holonomic constraints, any coordinate system for describing the state of the system (not necesarily a Carteesian), and many parts that may interact with each other in some non-trivial way, the equations of motion may still be obtained relatively easily with Lagrangian mechanics. If your system is unconstrained, all you need to do is to formulate the kinetic energy and potential energy of the system as functions of the generalized coordinates q, as well as their time derivatives. Then you form the Lagrangian as L = K - V, and the Euler–Lagrange equations will be your equations of motion. You may need to work them a little bit to get them in a form that is useful for implementation in a game (you want to isolate the highest time derivative for each generalized coordinate), but that's basically it.

If you have holonomic constraint, you also need to define a function f(q,t) for each constraint for which the constraint is the iso-surface that corresponds to f(q,t)=0. Then you instead form the Lagrangian as L = K - V + a1·f1 + a2·f2 + ... + an·fn, where f1, ..., fn are the functions for your constraints and a1, ..., an are corresponding, unknown coefficients, or so called Lagrange multipliers (basically, the higher these multipliers are, the higher the force from the corresponding constraints are). Then you use the Euler–Lagrange equations as usual, plus equations for your holonomic constraints, which you use to solve for a1, ..., an to make sure that you stay on the isosurface and that the constraints are satisfied (i.e. you find the forces require to make sure you stay where you are supposed to stay). Even if the principle is simple, how to do this in practice can be a bit involved, but one effective solution is described here. These mutipliers will show up in your equations of motion so you just plug in what you solved for them there.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .