A physics engine that uses Lagrangian mechanics