This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

Fast and Feature-Complete Differentiable Physics for Articulated Rigid Bodies with Contact

Keenon Werling1, Dalton Omens1, Jeongseok Lee2, Ioannis Exarchos1 and C. Karen Liu1 1Stanford University: {keenon, domens, exarchos, ckliu38}@stanford.edu 2Robotics AI, Amazon: jeoslee@amazon.com
Abstract

We present a fast and feature-complete differentiable physics engine, Nimble (nimblephysics.org), that supports Lagrangian dynamics and hard contact constraints for articulated rigid body simulation. Our differentiable physics engine offers a complete set of features that are typically only available in non-differentiable physics simulators commonly used by robotics applications. We solve contact constraints precisely using linear complementarity problems (LCPs). We present efficient and novel analytical gradients through the LCP formulation of inelastic contact that exploit the sparsity of the LCP solution. We support complex contact geometry, and gradients approximating continuous-time elastic collision. We also introduce a novel method to compute complementarity-aware gradients that help downstream optimization tasks avoid stalling in saddle points. We show that an implementation of this combination in a fork of an existing physics engine (DART) is capable of a 87x single-core speedup over finite-differencing in computing analytical Jacobians for a single timestep, while preserving all the expressiveness of original DART.

I Introduction

Many modern robotics problems are optimization problems. Finding optimal trajectories, guessing the physical parameters of a world that best fits our observed data, or designing a control policy to optimally respond to a dynamic environment are all types of optimization problems. Optimization methods can be sorted into two buckets: gradient-based, and gradient-free. Despite the well-known drawbacks of gradient-free methods (high sample complexity and noisy solutions), they remain popular in robotics because physics engines with the necessary features to model complex robots are generally non-differentiable. When gradients are required, we typically approximate them using finite differencing [41].

Recent years have seen many differentiable physics engines published [11, 19, 15, 42, 38, 10, 34, 29, 12], but none has yet gained traction as a replacement for popular non-differentiable engines [9, 41, 26]. We hypothesize that an ideal differentiable physics engine needs to implement an equivalent feature set to existing popular non-differentiable engines, as well as provide excellent computational efficiency, in order to gain adoption. In this paper, we extend the existing physics engine DART [26], which is commonly used in robotics and graphics communities, and make it differentiable. Our resulting engine supports all features available to the forward simulation process, meaning existing code and applications will remain compatible while also enjoying new capabilities enabled by efficient analytical differentiability.

A fully-featured physics engine like DART is complex and has many components that must be differentiated. Some components (such as collision detection and contact force computation) are not naively differentiable, but we show that under very reasonable assumptions we can compute useful Jacobians regardless. In order to differentiate through contacts and collision forces, we introduce an efficient method for differentiating the contact Linear Complementarity Problem (LCP) that exploits sparsity, as well as novel contact geometry algorithms and an efficient continuous-time approximation for elastic collisions. As a result, our engine is able to compute gradients with hard contact constraints up to 87 times faster than finite differencing methods, depending on the size of the dynamic system. Our relative speedup over finite differencing grows as the system complexity grows. In deriving our novel method to differentiate through the LCP, we also gain an understanding of the nature of contact dynamics Jacobians that allows us to propose heuristic “complementarity-aware gradients” that may be good search directions to try, if a downstream optimizer is getting stuck in a saddle point.

We provide an open-source implementation of all of these ideas, as well as derivations from previous work [25, 7], in a fully differentiable fork of the DART physics engine, which we call Nimble. Code and documentation are available at nimblephysics.org.

Refer to caption
Figure 1: Boston Dynamics’ Atlas Robot learning to do yoga, being simulated by our engine. This robot has 34 mesh colliders and 32 degrees of freedom. This freeze frame contains 24 contact points, 12 per foot. Even with all that complexity, we are able to compute the Jacobians of dynamics on this robot 87x faster on a single CPU core using the analytical methods introduced in this paper than by finite differencing (only 8.5ms vs 749ms for finite differencing).

To summarize, our contributions are as follows:

  • A novel and fast method for local differentiability of LCPs that exploits the sparsity of the LCP solution, which gives us efficient gradients through static and sliding contacts and friction without changing traditional forward-simulation formulations. Section IV

  • A novel method that manipulates gradient computation to help downstream optimization problems escape saddle points due to discrete contact states. Section IV-D

  • Fast geometric analytical gradients through collision detection algorithms which support various types of 3D geometry and meshes. Section V

  • A novel analytical approximation of continuous-time gradients through elastic contact, which otherwise can lead to errors in discrete-time systems. Section VI

  • An open-source implementation of all of our proposed methods (along with analytical gradients through Featherstone first described in GEAR [25]) in a fork of the DART physics engine which we call Nimble. We have created Python bindings and a PyPI package, pip install nimblephysics, for ease of use.

II Related Work


Engine Contact Dynamic Collision Gradients
Force State Geometry Method
MuJoCo customized generalized complete finite
Degrave impulse cartesian primitives auto
DiffTaichi impulse caresian primitives auto
Heiden iter LCP generalized primitives auto
de A. B.-P. direct LCP cartesian primitives symbolic
Geilinger customized generalized primitives symbolic
Ours direct LCP generalized complete symbolic
TABLE I: Differentiable engines supporting articulated rigid bodies

Differentiable physics simulation has been investigated previously in many different fields, including mechanical engineering [16], robotics [14], physics [23, 21] and computer graphics [32, 28]. Enabled by recent advances in automatic differentiation methods and libraries [31, 1, 18], a number of differentiable physics engines have been proposed to solve control and parameter estimation problems for rigid bodies [25, 11, 18, 15, 10, 12, 34] and non-rigid bodies [37, 27, 13, 20, 34, 17, 12]. While they share a similar high-level goal of solving “inverse problems”, the features and functionality provided by these engines vary widely, including the variations in contact handling, state space parameterization and collision geometry support. Table I highlights the differences in a few differentiable physics engines that have demonstrated the ability to simulate articulated rigid bodies with contact. Based on the functionalities each engine intends to support, the approaches to computing gradients can be organized in following categories.

Finite-differencing is a straightforward way to approximate gradients of a function. For a feature-complete physics engine, where analytical gradients are complex to obtain, finite-differencing provides a simpler method. For example, a widely used physics engine, MuJoCo [41], supports gradient computation via finite differencing. However, finite-differencing tends to introduce round-off errors and performs poorly for a large number of input variables.

Automatic differentiation (auto-diff) is a method for computing gradients of a sequence of elementary arithmetic operations or functions automatically. However, the constraint satisfaction problems required by many existing, feature-complete robotic physics engines are not supported by auto-diff libraries. To avoid this issue, many recent differentiable physics engines instead implement impulse-based contact handling, which could lead to numerical instability and constraint violation if the contact parameters are not tuned properly for the specific dynamic system and the simulation task. Degrave et al. [11] implemented a rigid body simulator in the Theano framework [1], while DiffTaichi [18] implemented a number of differentiable physics engines, including rigid bodies, extending the Taichi programming language [19], both representing dynamic equations in Cartesian coordinates and handling contact with impulse-based methods. In contrast, Tiny Differentiable Simulator [15] models contacts as an LCP, but they solve the LCP iteratively via Projected Gauss Siedel (PGS) method [24], instead of directly solving a constraint satisfaction problem, making it possible to compute gradient using auto-diff libraries.

Symbolic differentiation is another way to compute gradients by directly differentiate mathematical expressions. For complex programs like Lagrangian dynamics with constraints formulated as a Differential Algebraic Equations, symbolic differentiation can be exceedingly difficult. Earlier work computed symbolic gradients for smooth dynamic systems [25], and [7] simplified the computation of the derivative of the forward dynamics exploiting the derivative of the inverse dynamics. Symbolic differentiation becomes manageable when the gradients are only required within smooth contact modes [42] or a specific contact mode is assumed [38]. Recently, Amos and Kolter proposed a method, Opt-Net, that back-propagates through the solution of an optimization problem to its input parameters [2]. Building on Opt-Net, de Avila Belbute-Peres et al. [10] derived analytical gradients through LCP formulated as a QP. Their method enables differentiability for rigid body simulation with hard constraints, but their implementation represents 2D rigid bodies in Cartesian coordinates and only supports collisions with a plane, insufficient for simulating complex articulated rigid body systems. More importantly, computing gradients via QP requires solving a number of linear systems which does not take advantage of the sparsity of the LCP structure. Qiao et al. [34] built on [2] and improved the performance of contact handling by breaking a large scene into smaller impact zones. A QP is solved for each impact zone to ensure that the geometry is not interpenetrating, but contact dynamics and conservation laws are not considered. Solving contacts for localized zones has been previously implemented in many existing physics engines [41, 9, 26]. Adapting the collision handling routine in DART, our method by default utilizes the localized contact zones to speed up the performance. Adjoint sensitivity analysis [35] has also been used for computing gradients of dynamics. Millard et al. [29] combined auto-diff with adjoint sensitivity analysis to achieve faster gradient computation for higher-dof systems, but their method did not handle contact and collision. Geilinger et al. [12] analytically computed derivatives through adjoint sensitivity analysis and proposed a differentiable physics engine with implicit forward integration and a customized frictional contact model that is natively differentiable.

Approximating physics with neural networks is a different approach towards differentiable physics engine. Instead of forward simulating a dynamic system from the first principles of Newtonian mechanics, a neural network is learned from training data. Examples of this approach include Battaglia et. al [4], Chang et. al. [8], and Mrowca et. al [30].

Our engine employs symbolic differentiation to compute gradients through every part of the engine using hand-tuned C++ code. We introduce a novel method to differentiate the LCP analytically that takes advantage of the sparsity of the solution and is compatible with using direct methods to solve the LCP. In addition, our engine supports a richer set of geometry for collision and contact handling than has been previously available, including mesh-mesh and mesh-primitive collisions, in order to achieve a fully functional differentiable version of the DART physics engine for robotic applications.

III Overview

A physics engine can be thought of as a simple function that takes the current position 𝒒t\boldsymbol{q}_{t}, velocity 𝒒˙t\dot{\boldsymbol{q}}_{t}, control forces 𝝉\boldsymbol{\tau} and inertial properties 𝝁\boldsymbol{\mu}, and returns the position and velocity at the next timestep, 𝒒t+1\boldsymbol{q}_{t+1} and 𝒒˙t+1\dot{\boldsymbol{q}}_{t+1}:

P(𝒒t,𝒒˙t,𝝉,𝝁)=[𝒒t+1,𝒒˙t+1].P(\boldsymbol{q}_{t},\dot{\boldsymbol{q}}_{t},\boldsymbol{\tau},\boldsymbol{\mu})=[\boldsymbol{q}_{t+1},\dot{\boldsymbol{q}}_{t+1}]. (1)

In an engine with simple explicit time integration, our next position 𝒒t+1\boldsymbol{q}_{t+1} is a trivial function of current position and velocity, 𝒒t+1=𝒒t+Δt𝒒˙t\boldsymbol{q}_{t+1}=\boldsymbol{q}_{t}+\Delta t\dot{\boldsymbol{q}}_{t} , where Δt\Delta t is the descritized time interval.

The computational work of the physics engine comes from solving for our next velocity, 𝒒˙t+1\dot{\boldsymbol{q}}_{t+1}. We are representing our articulated rigid body system in generalized coordinates using the following Lagrangian dynamic equation:

𝑴(𝒒t,𝝁)𝒒˙t+1=𝑴(𝒒t,𝝁)𝒒˙tΔt(𝒄(𝒒t,𝒒˙t,𝝁)𝝉)+𝑱T(𝒒t)𝒇,\begin{split}\bm{M}(\bm{q}_{t},\boldsymbol{\mu})\dot{\bm{q}}_{t+1}&=\bm{M}(\bm{q}_{t},\boldsymbol{\mu})\dot{\bm{q}}_{t}-\Delta t(\bm{c}(\bm{q}_{t},\dot{\bm{q}}_{t},\boldsymbol{\mu})-\bm{\tau})\\ &+\bm{J}^{T}(\bm{q}_{t})\bm{f},\end{split} (2)

where 𝑴\bm{M} is the mass matrix, 𝒄\bm{c} is the Coriolis and gravitational force, and 𝒇\bm{f} is the contact impulse transformed into the generalized coordinates by the contact Jacobian matrix 𝑱\bm{J}. Note that multiple contact points and/or other constraint impulses can be trivially added to Equation 2.

Every term in Equation 2 can be evaluated given 𝒒t\boldsymbol{q}_{t}, 𝒒˙t\dot{\boldsymbol{q}}_{t} and 𝝉\bm{\tau} except for the contact impulse 𝒇\boldsymbol{f}, which requires the engine to form and solve an LCP:

find𝒇,𝒗t+1\displaystyle\mathrm{find\;\;\;}\bm{f},\bm{v}_{t+1}
suchthat𝒇𝟎,𝒗t+1𝟎,𝒇T𝒗t+1=0.\displaystyle\mathrm{such\;that\;\;\;}\bm{f}\geq\bm{0},\;\bm{v}_{t+1}\geq\bm{0},\;\bm{f}^{T}\bm{v}_{t+1}=0. (3)

The velocity of a contact point at the next time step, 𝒗t+1\bm{v}_{t+1}, can be expressed as a linear function in 𝒇\bm{f}

𝒗t+1\displaystyle\bm{v}_{t+1} =𝑱𝒒˙t+1=𝑱𝑴1(𝑴𝒒˙tΔt(𝒄𝝉)+𝑱T𝒇)\displaystyle=\bm{J}\dot{\bm{q}}_{t+1}=\bm{J}\bm{M}^{-1}\left(\bm{M}\dot{\bm{q}}_{t}-\Delta t(\bm{c}-\bm{\tau})+\bm{J}^{T}\bm{f}\right)
=𝑨𝒇+𝒃,\displaystyle=\bm{A}\bm{f}+\bm{b}, (4)

where 𝑨=𝑱𝑴1𝑱T\bm{A}=\bm{J}\bm{M}^{-1}\bm{J}^{T} and 𝒃=𝑱(𝒒˙t+Δt𝑴1(𝝉𝒄))\bm{b}=\bm{J}(\dot{\bm{q}}_{t}+\Delta t\bm{M}^{-1}(\bm{\tau}-\bm{c})). The LCP procedure can then be expressed as a function that maps (𝑨,𝒃)(\bm{A},\bm{b}) to the contact impulse 𝒇\bm{f}:

fLCP(𝑨(𝒒t,𝝁),𝒃(𝒒t,𝒒˙t,𝝉,𝝁))=𝒇f_{\text{LCP}}\big{(}\bm{A}(\bm{q}_{t},\boldsymbol{\mu}),\bm{b}(\boldsymbol{q}_{t},\dot{\boldsymbol{q}}_{t},\boldsymbol{\tau},\boldsymbol{\mu})\big{)}=\bm{f} (5)

As such, the process of forward stepping is to find 𝒇\bm{f} and resulting 𝒒˙t+1\dot{\bm{q}}_{t+1} that satisfy Equation 2 and Equation 5.

Refer to caption

Figure 2: Data flow during a forward simulation, visualizing equations 2 and 5. The inputs are 𝒒t\boldsymbol{q}_{t} and 𝒒˙t\dot{\boldsymbol{q}}_{t}, the current position and velocity in generalized coordinates, μ\mu, the inertial properties, and τ\tau, the external (control) torques on the joints. The outputs are the generalized position and velocity at the next timestep, 𝒒t+1\boldsymbol{q}_{t+1} and 𝒒˙t+1\dot{\boldsymbol{q}}_{t+1}. Every forward arrow represents a dependency in data flow, which must be differentiated during backpropagation. Challenging dependencies, and the relevant sections where we introduce analytical Jacobians, are labeled with colored arrows and text in the diagram.

The main task in developing a differentiable physics engine is to solve for the gradient of next velocity 𝒒˙t+1\dot{\boldsymbol{q}}_{t+1} with respect to the input to the current time step, namely 𝒒t,𝒒˙t,𝝉t\boldsymbol{q}_{t},\dot{\boldsymbol{q}}_{t},\bm{\tau}_{t}, and 𝝁\bm{\mu}. The data flow is shown in Figure 2. For brevity we refer to the ouput of a function for a given timestep by the same name as the function with a subscript tt (e.g. 𝑱t=𝑱(𝒒t)\bm{J}_{t}=\bm{J}(\boldsymbol{q}_{t})). The velocity at the next step can be simplified to

𝒒˙t+1=𝒒˙t+𝑴t1𝒛t,\dot{\bm{q}}_{t+1}=\dot{\bm{q}}_{t}+\bm{M}_{t}^{-1}\bm{z}_{t}, (6)

where 𝒛tΔt(𝒄t𝝉t)+𝑱tT𝒇t\boldsymbol{z}_{t}\equiv-\Delta t(\bm{c}_{t}-\bm{\tau}_{t})+\bm{J}_{t}^{T}\bm{f}_{t}. The gradients we need to compute at each time step are written as:

𝒒˙t+1𝒒t\displaystyle\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{q}_{t}} =𝑴t1𝒛t𝒒t+𝑴t1(Δt𝒄t𝒒t+𝑱tT𝒒t𝒇t\displaystyle=\frac{\partial\bm{M}_{t}^{-1}\boldsymbol{z}_{t}}{\partial\boldsymbol{q}_{t}}+\bm{M}_{t}^{-1}\left(-\Delta t\frac{\partial\bm{c}_{t}}{\partial\boldsymbol{q}_{t}}+\frac{\partial\bm{J}_{t}^{T}}{\partial\boldsymbol{q}_{t}}\bm{f}_{t}\right.
+𝑱tT𝒇t𝒒t)\displaystyle\left.+\boldsymbol{J}_{t}^{T}\frac{\partial{\boldsymbol{f}_{t}}}{\partial\boldsymbol{q}_{t}}\right) (7)
𝒒˙t+1𝒒˙t\displaystyle\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\dot{\bm{q}}_{t}} =𝑰+𝑴t1(Δt𝒄t𝒒˙t+𝑱tT𝒇t𝒒˙t)\displaystyle=\bm{I}+\boldsymbol{M}_{t}^{-1}\left(-\Delta t\frac{\partial\boldsymbol{c}_{t}}{\partial\dot{\boldsymbol{q}}_{t}}+\boldsymbol{J}_{t}^{T}\frac{\partial\boldsymbol{f}_{t}}{\partial\dot{\boldsymbol{q}}_{t}}\right) (8)
𝒒˙t+1𝝉t\displaystyle\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{\tau}_{t}} =𝑴t1(Δt𝑰+𝑱tT𝒇t𝝉t)\displaystyle=\boldsymbol{M}_{t}^{-1}\left(\Delta t\bm{I}+\boldsymbol{J}_{t}^{T}\frac{\partial\boldsymbol{f}_{t}}{\partial\boldsymbol{\tau}_{t}}\right) (9)
𝒒˙t+1𝝁\displaystyle\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{\mu}} =𝑴t1𝒛t𝝁+𝑴t1(Δt𝒄t𝝁+𝑱tT𝒇t𝝁)\displaystyle=\frac{\partial\boldsymbol{M}_{t}^{-1}\boldsymbol{z}_{t}}{\partial\boldsymbol{\mu}}+\boldsymbol{M}_{t}^{-1}\left(-\Delta t\frac{\partial\boldsymbol{c}_{t}}{\partial\boldsymbol{\mu}}+\boldsymbol{J}_{t}^{T}\frac{\partial\boldsymbol{f}_{t}}{\partial\boldsymbol{\mu}}\right) (10)

We tackle the tricky intermediate Jacobians in sections that follow. In Section IV we will introduce a novel sparse analytical method to compute the gradients of contact force 𝒇t\boldsymbol{f}_{t} with respect to 𝒒t,𝒒˙t,𝝉t,𝝁\boldsymbol{q}_{t},\dot{\boldsymbol{q}}_{t},\bm{\tau}_{t},\bm{\mu}. Section V will discuss 𝑱t𝒒t\frac{\partial\boldsymbol{J}_{t}}{\partial\boldsymbol{q}_{t}}—how collision geometry changes with respect to changes in position. In Section VI we will tackle 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} and 𝒒t+1𝒒˙t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\dot{\boldsymbol{q}}_{t}}, which is not as simple as it may at first appear, because naively taking gradients through a discrete time physics engine yields problematic results when elastic collisions take place. Additionally, the appendix gives a way to apply the derivations from [25] and [7] to analytically find 𝑴t1𝒛t𝒒t\frac{\partial\boldsymbol{M}_{t}^{-1}\boldsymbol{z}_{t}}{\partial\boldsymbol{q}_{t}}, 𝒄t𝒒t\frac{\partial\boldsymbol{c}_{t}}{\partial\boldsymbol{q}_{t}}, and 𝒄t𝒒˙t\frac{\partial\boldsymbol{c}_{t}}{\partial\dot{\boldsymbol{q}}_{t}}.

IV Differentiating the LCP

This section introduces a method to analytically compute 𝒇𝑨\frac{\partial\boldsymbol{f}}{\partial\boldsymbol{A}} and 𝒇𝒃\frac{\partial\boldsymbol{f}}{\partial\boldsymbol{b}}. It turns out that it is possible to efficiently get unambiguous gradients through an LCP in the vast majority of practical scenarios, without recasting it as a QP (which throws away sparsity information by replacing the complementarity constraint with an objective function). To see this, let us consider a hypothetical LCP problem parameterized by 𝑨,𝒃\boldsymbol{A},\boldsymbol{b} with a solution 𝒇\boldsymbol{f}^{*} found during the forward pass: fLCP(𝑨,𝒃)=𝒇f_{\text{LCP}}(\bm{A},\bm{b})=\bm{f}^{*}.

For brevity, we only include the discussion on normal contact impulses in this section and leave the extension to friction impulses in Appendix -A. Therefore, each element in 𝒇𝟎\boldsymbol{f}^{*}\geq\boldsymbol{0} indicates the normal impulse of a point contact. By complementarity, we know that if some element fi>0f^{*}_{i}>0, then vi=(𝑨𝒇+𝒃)i=0v_{i}=(\boldsymbol{A}\boldsymbol{f}^{*}+\boldsymbol{b})_{i}=0. Intuitively, the relative velocity at contact point ii must be 0 if there is any non-zero impulse being exerted at contact point ii. We call such contact points “Clamping” because the impulse fi>0f_{i}>0 is adjusted to keep the relative velocity vi=0v_{i}=0. Let the set 𝒞\mathcal{C} to be all indices that are clamping. Symmetrically, if fj=0f_{j}=0, then the relative velocity vj=(𝑨𝒇+𝒃)j0v_{j}=(\boldsymbol{A}\boldsymbol{f}^{*}+\boldsymbol{b})_{j}\geq 0 is free to vary without the LCP needing to adjust fjf_{j} to compensate. We call such contact points “Separating” and define the set 𝒮\mathcal{S} to be all indices that are separating. Let us call indices jj where fj=0f_{j}=0 and vj=0v_{j}=0 “Tied.” Define the set 𝒯\mathcal{T} to be all indices that are tied.

If no contact points are tied (𝒯=\mathcal{T}=\emptyset), the LCP is strictly differentiable and the gradients can be analytically computed. When some contact points are tied (𝒯\mathcal{T}\neq\emptyset), the LCP has valid subgradients and it is possible to follow any in an optimization. The tied case is analogous to the non-differentiable points in a QP where an inequality constraint is active while the corresponding dual variable is also zero. In such a case, computing gradients via taking differentials of the KKT conditions will result in a low-rank linear system and thus non-unique gradients [2].

IV-A Strictly differentiable cases

Consider the case where 𝒯=\mathcal{T}=\emptyset. We shuffle the indices of 𝒇\boldsymbol{f}^{*}, 𝒗\boldsymbol{v}, 𝑨\boldsymbol{A} and 𝒃\boldsymbol{b} to group together members of 𝒞\mathcal{C} and 𝒮\mathcal{S}. The LCP becomes:

find𝒇𝒞,𝒇𝒮,𝒗𝒞,𝒗𝒮such that[𝒗𝒞𝒗𝒮]=[𝑨𝒞𝒞𝑨𝒞𝒮𝑨𝒮𝒞𝑨𝒮𝒮][𝒇𝒞𝒇𝒮]+[𝒃𝒞𝒃𝒮]𝒇𝒞𝟎,𝒇𝒮𝟎,𝒗𝒞𝟎,𝒗𝒮𝟎𝒇𝒞T𝒗𝒞=0,𝒇𝒮T𝒗𝒮=0.\displaystyle\begin{matrix}[l]\text{find}&&\boldsymbol{f}^{*}_{\mathcal{C}},\boldsymbol{f}^{*}_{\mathcal{S}},\boldsymbol{v}_{\mathcal{C}},\boldsymbol{v}_{\mathcal{S}}\\ \text{such that}&&\begin{bmatrix}\boldsymbol{v}_{\mathcal{C}}\\ \boldsymbol{v}_{\mathcal{S}}\end{bmatrix}=\begin{bmatrix}\boldsymbol{A}_{\mathcal{CC}}&&\boldsymbol{A}_{\mathcal{CS}}\\ \boldsymbol{A}_{\mathcal{SC}}&&\boldsymbol{A}_{\mathcal{SS}}\\ \end{bmatrix}\begin{bmatrix}\boldsymbol{f}^{*}_{\mathcal{C}}\\ \boldsymbol{f}^{*}_{\mathcal{S}}\end{bmatrix}+\begin{bmatrix}\boldsymbol{b}_{\mathcal{C}}\\ \boldsymbol{b}_{\mathcal{S}}\end{bmatrix}\\ &&\boldsymbol{f}^{*}_{\mathcal{C}}\geq\boldsymbol{0},\;\boldsymbol{f}^{*}_{\mathcal{S}}\geq\boldsymbol{0},\;\boldsymbol{v}_{\mathcal{C}}\geq\boldsymbol{0},\;\boldsymbol{v}_{\mathcal{S}}\geq\boldsymbol{0}\\ &&\boldsymbol{f}_{\mathcal{C}}^{*T}\boldsymbol{v}_{\mathcal{C}}=0,\;\boldsymbol{f}_{\mathcal{S}}^{*T}\boldsymbol{v}_{\mathcal{S}}=0.\end{matrix} (11)

Since we know the classification of each contact that forms the valid solution 𝒇\boldsymbol{f}^{*}, we rewrite the LCP constraints as follows:

find𝒇𝒞,𝒇𝒮,𝒗𝒞,𝒗𝒮such that[𝒗𝒞𝒗𝒮]=[𝑨𝒞𝒞𝑨𝒞𝒮𝑨𝒮𝒞𝑨𝒮𝒮][𝒇𝒞𝒇𝒮]+[𝒃𝒞𝒃𝒮]𝒇𝒞>𝟎,𝒇𝒮=𝟎,𝒗𝒞=𝟎,𝒗𝒮>𝟎.\displaystyle\begin{matrix}[l]\text{find}&&\boldsymbol{f}^{*}_{\mathcal{C}},\boldsymbol{f}^{*}_{\mathcal{S}},\boldsymbol{v}_{\mathcal{C}},\boldsymbol{v}_{\mathcal{S}}\\ \text{such that}&&\begin{bmatrix}\boldsymbol{v}_{\mathcal{C}}\\ \boldsymbol{v}_{\mathcal{S}}\end{bmatrix}=\begin{bmatrix}\boldsymbol{A}_{\mathcal{CC}}&&\boldsymbol{A}_{\mathcal{CS}}\\ \boldsymbol{A}_{\mathcal{SC}}&&\boldsymbol{A}_{\mathcal{SS}}\\ \end{bmatrix}\begin{bmatrix}\boldsymbol{f}^{*}_{\mathcal{C}}\\ \boldsymbol{f}^{*}_{\mathcal{S}}\end{bmatrix}+\begin{bmatrix}\boldsymbol{b}_{\mathcal{C}}\\ \boldsymbol{b}_{\mathcal{S}}\end{bmatrix}\\ &&\boldsymbol{f}^{*}_{\mathcal{C}}>\boldsymbol{0},\;\boldsymbol{f}^{*}_{\mathcal{S}}=\boldsymbol{0},\;\boldsymbol{v}_{\mathcal{C}}=\boldsymbol{0},\;\boldsymbol{v}_{\mathcal{S}}>\boldsymbol{0}.\end{matrix} (12)

From here we can see how the valid solution 𝒇\boldsymbol{f}^{*} changes under infinitesimal perturbations ϵ\bm{\epsilon} to 𝑨\boldsymbol{A} and 𝒃\boldsymbol{b}. Since 𝒇𝒮=𝟎\boldsymbol{f}^{*}_{\mathcal{S}}=\boldsymbol{0} and 𝒗𝒞=𝟎\boldsymbol{v}_{\mathcal{C}}=\boldsymbol{0}, the LCP can be reduced to three conditions on 𝒇𝒞\boldsymbol{f}^{*}_{\mathcal{C}}:

𝟎=𝑨𝒞𝒞𝒇𝒞+𝒃𝒞\displaystyle\boldsymbol{0}=\boldsymbol{A}_{\mathcal{CC}}\boldsymbol{f}^{*}_{\mathcal{C}}+\boldsymbol{b}_{\mathcal{C}} (13)
𝒇𝒞>𝟎\displaystyle\boldsymbol{f}^{*}_{\mathcal{C}}>\boldsymbol{0} (14)
𝑨𝒮𝒞𝒇𝒞+𝒃𝒮>𝟎.\displaystyle\boldsymbol{A}_{\mathcal{SC}}\boldsymbol{f}^{*}_{\mathcal{C}}+\boldsymbol{b}_{\mathcal{S}}>\boldsymbol{0}. (15)

We will show that these conditions will always be possible to satisfy under small enough perturbations ϵ\bm{\epsilon} in the neighborhood of a valid solution. Let us first consider tiny perturbations to 𝒃𝒮\boldsymbol{b}_{\mathcal{S}} and 𝑨𝒮𝒞\boldsymbol{A}_{\mathcal{SC}}. If the perturbations are small enough, then Equation 15 will still be satisfied with our original 𝒇𝒞\boldsymbol{f}_{\mathcal{C}}^{*}, because we know Equation 15 already holds strictly such that there is some non-zero room to decrease any element of 𝑨𝒮𝒞𝒇𝒞+𝒃𝒮\boldsymbol{A}_{\mathcal{SC}}\boldsymbol{f}^{*}_{\mathcal{C}}+\boldsymbol{b}_{\mathcal{S}} without violating Equation 15. Therefore,

𝒇𝒞𝒃𝒮=𝟎 and 𝒇𝒞𝑨𝒮𝒞=𝟎.\displaystyle\frac{\partial\boldsymbol{f}_{\mathcal{C}}^{*}}{\partial\boldsymbol{b}_{\mathcal{S}}}=\boldsymbol{0}\text{\;\;and\;\;}\frac{\partial\boldsymbol{f}_{\mathcal{C}}^{*}}{\partial\boldsymbol{A}_{\mathcal{SC}}}=\boldsymbol{0}. (16)

Next let us consider an infinitesimal perturbation ϵ\bm{\epsilon} to 𝒃𝒞\boldsymbol{b}_{\mathcal{C}} and the necessary change on the clamping force Δ𝒇𝒞\Delta\boldsymbol{f}_{\mathcal{C}}^{*} to satisfy Equation 13:

𝟎=𝑨𝒞𝒞(𝒇𝒞+Δ𝒇𝒞)+𝒃𝒞+ϵ.\boldsymbol{0}=\boldsymbol{A}_{\mathcal{CC}}(\boldsymbol{f}^{*}_{\mathcal{C}}+\Delta\boldsymbol{f}^{*}_{\mathcal{C}})+\boldsymbol{b}_{\mathcal{C}}+\bm{\epsilon}. (17)

Setting 𝑨𝒞𝒞𝒇𝒞+𝒃𝒞=𝟎\boldsymbol{A}_{\mathcal{CC}}\boldsymbol{f}^{*}_{\mathcal{C}}+\boldsymbol{b}_{\mathcal{C}}=\boldsymbol{0} and assuming 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} is invertible, the change of the clamping force is given as Δ𝒇𝒞=𝑨𝒞𝒞1ϵ\Delta\boldsymbol{f}^{*}_{\mathcal{C}}=-\boldsymbol{A}_{\mathcal{CC}}^{-1}\bm{\epsilon}. Since 𝒇𝒞\boldsymbol{f}^{*}_{\mathcal{C}} is strictly greater than 𝟎\boldsymbol{0}, it is always possible to choose an ϵ\bm{\epsilon} small enough to make 𝒇𝒞𝑨𝒞𝒞1ϵ>0\boldsymbol{f}^{*}_{\mathcal{C}}-\boldsymbol{A}_{\mathcal{CC}}^{-1}\bm{\epsilon}>0 and 𝑨𝒮𝒞(𝒇𝒞+Δ𝒇𝒞)+𝒃𝒮>0\boldsymbol{A}_{\mathcal{SC}}(\boldsymbol{f}^{*}_{\mathcal{C}}+\Delta\boldsymbol{f}^{*}_{\mathcal{C}})+\boldsymbol{b}_{\mathcal{S}}>0 remain true. Therefore,

𝒇𝒞𝒃𝒞=𝑨𝒞𝒞1.\displaystyle\frac{\partial\boldsymbol{f}_{\mathcal{C}}^{*}}{\partial\boldsymbol{b}_{\mathcal{C}}}=-\boldsymbol{A}_{\mathcal{CC}}^{-1}. (18)

Note that 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} is not always invertible because 𝑨\boldsymbol{A} is positive semidefinite. We will discuss the case when 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} is not full rank in Section IV-C along with a method to stabilize the LCP when there exists multiple LCP solutions in Appendix -B.

Lastly, we compute gradients with respect to 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}}. In practice, changes to 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} only happen because we are differentiating with respect to parameters 𝒒\boldsymbol{q} or 𝝁\bm{\mu}, which also changes 𝒃𝒞\boldsymbol{b}_{\mathcal{C}}. As such, we introduce a new scalar variable, xx, which could represent any arbitrary scalar quantity that effects both 𝑨\boldsymbol{A} and 𝒃\boldsymbol{b}. Equation 13 can be rewritten as:

𝒇𝒞=𝑨𝒞𝒞(x)1𝒃𝒞(x).\displaystyle\boldsymbol{f}^{*}_{\mathcal{C}}=-\boldsymbol{A}_{\mathcal{CC}}(x)^{-1}\boldsymbol{b}_{\mathcal{C}}(x). (19)

Because 𝑨𝒞𝒞(x)\boldsymbol{A}_{\mathcal{CC}}(x) and 𝒃𝒞(x)\boldsymbol{b}_{\mathcal{C}}(x) are continuous, and the original solution is valid, any sufficiently small perturbation to xx will not reduce 𝒇𝒞\boldsymbol{f}^{*}_{\mathcal{C}} below 0 or violate Equation 15. The Jacobian with respect to xx can be expressed as:

𝒇𝒞x=𝑨𝒞𝒞(x)1𝑨𝒞𝒞(x)x𝑨𝒞𝒞(x)1𝒃𝒞(x)\displaystyle\frac{\partial\boldsymbol{f}^{*}_{\mathcal{C}}}{\partial x}=\boldsymbol{A}_{\mathcal{CC}}(x)^{-1}\frac{\partial\boldsymbol{A}_{\mathcal{CC}}(x)}{\partial x}\boldsymbol{A}_{\mathcal{CC}}(x)^{-1}\boldsymbol{b}_{\mathcal{C}}(x)
+𝑨𝒞𝒞(x)1𝒃𝒞(x)x.\displaystyle+\boldsymbol{A}_{\mathcal{CC}}(x)^{-1}\frac{\partial\boldsymbol{b}_{\mathcal{C}}(x)}{\partial x}. (20)

Using 𝑨=𝑱𝑴1𝑱T\bm{A}=\bm{J}\bm{M}^{-1}\bm{J}^{T} and 𝒃=𝑱(𝒒˙t+Δt𝑴1(𝝉𝒄))\bm{b}=\bm{J}(\dot{\bm{q}}_{t}+\Delta t\bm{M}^{-1}(\bm{\tau}-\bm{c})), along with the derivations of Featherstone presented in Appendix -F, it is possible to compute 𝑨𝒞𝒞x\frac{\partial\boldsymbol{A}_{\mathcal{CC}}}{\partial x} and 𝒃𝒞x\frac{\partial\boldsymbol{b}_{\mathcal{C}}}{\partial x} for any specific xx.

Remark: Previous methods [10, 27, 34] cast an LCP to a QP and solved for a linear system of size n+mn+m derived from taking differentials of the KKT conditions of the QP, where nn is the dimension of the state variable and mm is the number of contact constraints. Our method also solves for linear systems to obtain 𝑨𝒞𝒞1\bm{A}_{\mathcal{CC}}^{-1}, but the size of 𝑨𝒞𝒞\bm{A}_{\mathcal{CC}} is often much less than mm due to the sparsity of the solution (𝒇,𝒗)(\bm{f}^{*},\bm{v}^{*}).

IV-B Subdifferentiable case

Now let us consider when 𝒯\mathcal{T}\neq\emptyset. Replacing the LCP constraints with linear constraints will no longer work because any perturbation will immediately change the state of the contact and the change also depends on the direction of perturbation. Including the class of “tied” contact points to Equation 11, we need to satisfy an additional linear system,

𝒗𝒯\displaystyle\bm{v}_{\mathcal{T}} =𝑨𝒯𝒯𝒇𝒯+𝑨𝒯𝒞𝒇𝒞+𝑨𝒯𝒮𝒇𝒮+𝒃𝒯\displaystyle=\bm{A}_{\mathcal{TT}}\bm{f}_{\mathcal{T}}^{*}+\bm{A}_{\mathcal{TC}}\bm{f}_{\mathcal{C}}^{*}+\bm{A}_{\mathcal{TS}}\bm{f}_{\mathcal{S}}^{*}+\bm{b}_{\mathcal{T}}
=𝑨𝒯𝒯𝒇𝒯+𝒃~𝒯,\displaystyle=\bm{A}_{\mathcal{TT}}\bm{f}_{\mathcal{T}}^{*}+\tilde{\bm{b}}_{\mathcal{T}}, (21)

where 𝒃~𝒯=𝑨𝒯𝒞𝒇𝒞+𝒃𝒯\tilde{\bm{b}}_{\mathcal{T}}=\bm{A}_{\mathcal{TC}}\bm{f}_{\mathcal{C}}^{*}+\bm{b}_{\mathcal{T}} and 𝒗𝒯\bm{v}_{\mathcal{T}} and 𝒇𝒯\bm{f}_{\mathcal{T}}^{*} are both zero at the solution. Let i𝒯i\in\mathcal{T} be the index of a tied contact point. Consider perturbing the ii’th element of 𝒃~𝒯\tilde{\boldsymbol{b}}_{\mathcal{T}} by ϵ\epsilon. If ϵ>0\epsilon>0, 𝑨𝒯𝒯𝒇𝒯\bm{A}_{\mathcal{TT}}\bm{f}_{\mathcal{T}}^{*} cannot become negative to balance Equation IV-B because 𝑨𝒯𝒯\bm{A}_{\mathcal{TT}} is positive semidefinite and 𝒇𝒯\bm{f}_{\mathcal{T}}^{*} must be nonnegative. Therefore, v𝒯iv_{\mathcal{T}i} must become positive, resulting contact point ii being separated and f𝒯if_{\mathcal{T}i}^{*} remaining zero. If ϵ<0\epsilon<0, then ii is immediately bumped into the “clamping” set 𝒞\mathcal{C} because v𝒯iv_{\mathcal{T}i} cannot be negative. Therefore, f𝒯if^{*}_{\mathcal{T}i} must become positive to balance Equation IV-B. The gradients for the clamping and separating cases are both valid subgraidents for a tied contact point. In an optimization, we can choose either of the two subgradients at random without impacting the convergence [6]. In practice, encountering elements in 𝒯\mathcal{T} is quite rare for practical numerical reasons.

IV-C When 𝐀𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} is not full rank

When 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} is not full rank, the solution to fLCP(𝑨,𝒃)=𝒇f_{\text{LCP}}(\boldsymbol{A},\boldsymbol{b})=\boldsymbol{f}^{*} is no longer unique. Nevertheless, once a solution is computed using any algorithm and the clamping set 𝒞\mathcal{C} is found, we can use the stabilization method proposed in Appendix -B to find the least-squares minimal 𝒇\boldsymbol{f}^{*} that solves the LCP. The gradient of clamping forces can then be written as:

𝒇𝒞x=𝑨𝒞𝒞1𝑨𝒞𝒞x𝑨𝒞𝒞1𝒃𝒞+𝑨𝒞𝒞1𝒃𝒞x\displaystyle\frac{\partial\boldsymbol{f}^{*}_{\mathcal{C}}}{\partial x}=\boldsymbol{A}_{\mathcal{CC}}^{-1}\frac{\partial\boldsymbol{A}_{\mathcal{CC}}}{\partial x}\boldsymbol{A}_{\mathcal{CC}}^{-1}\boldsymbol{b}_{\mathcal{C}}+\boldsymbol{A}_{\mathcal{CC}}^{-1}\frac{\partial\boldsymbol{b}_{\mathcal{C}}}{\partial x}
+(𝑰𝑨𝒞𝒞+𝑨𝒞𝒞)(𝑨𝒞𝒞Tx)𝑨𝒞𝒞+T𝑨𝒞𝒞+,\displaystyle+(\boldsymbol{I}-\boldsymbol{A}^{+}_{\mathcal{CC}}\boldsymbol{A}_{\mathcal{CC}})\big{(}\frac{\partial\boldsymbol{A}_{\mathcal{CC}}^{T}}{\partial x}\big{)}\boldsymbol{A}_{\mathcal{CC}}^{+T}\boldsymbol{A}_{\mathcal{CC}}^{+}, (22)

where 𝑨𝒞𝒞+\boldsymbol{A}_{\mathcal{CC}}^{+} is the pseudo inverse matrix of the low-rank 𝑨\boldsymbol{A}. For numerical stability, we can solve a series of linear systems instead of explicitly evaluating 𝑨𝒞𝒞(x)+\boldsymbol{A}_{\mathcal{CC}}(x)^{+}.

IV-D Complementarity-aware gradients via contact constraints

Sometimes analytically correct Jacobians through the LCP can actually prevent an optimizer from finding a good solution. When a contact ii is clamping (i𝒞i\in\mathcal{C}), we effectively impose a constraint that the relative velocity at that contact point is zero no matter how we push or pull on the contact. This prevents the gradients from pointing towards any motions that require breaking contact, because our constraints will zero out gradients that lead to 𝒗i>0\boldsymbol{v}_{i}>0.

This behavior is caused by the complementarity constraint requiring that at most one of viv_{i} or fif_{i} can be non-zero. This phenomenon grows out of the short-sightedness of the gradient (it only considers an infinitely small neighborhood ϵ\epsilon around the current state). While it is true that small attempts to push vi>0v_{i}>0 will result in no change to 𝒗\boldsymbol{v} as 𝒇\boldsymbol{f} compensates to keep vi=0v_{i}=0, eventually we will reach a discontinuity where 𝒇\boldsymbol{f} can no longer prevent the contact from separating. However, a gradient-based optimizer may never take steps in that direction because gradients in that direction are 0. In this section we propose a heuristic to opportunistically explore “projecting forward” to the discontinuity where the complementarity constraint flips during backpropagation, depending on the gradient of loss function with respect to 𝒗\boldsymbol{v} and 𝒗\frac{\partial\ell}{\partial\boldsymbol{v}}.

To make it more concrete, let us consider a simple example of a 2D circle attached to a linear actuator that can produce force along the vertical axis (Figure 3). Our goal is to lift the circle up to a target height above the ground by generating an upward velocity using the linear actuator. When the circle is resting on the ground under gravity, the contact point with the ground is classified as “clamping”. This means that any tiny perturbation in the control force of linear actuator will be met with an exactly offsetting contact force to ensure that the relative velocity between the circle and the ground remains zero. As such, no matter what gradient of loss function with respect to the control force we try to backpropagate through the contact, we will always get l𝝉=𝟎\frac{\partial l}{\partial\bm{\tau}}=\boldsymbol{0}.

Refer to caption

Figure 3: Increasing force from the linear actuator (𝝉\bm{\tau}) is met by an equal and opposite decrease in ground reaction force 𝒇\boldsymbol{f}, resulting in no change in velocity and thus no gain in height. Gradients of loss function will be zeroed out by 𝒗𝝉=𝟎\frac{\partial\boldsymbol{v}}{\partial\bm{\tau}}=\boldsymbol{0} when backpropgating through the contact. In contrast, if the contact is classified as “separating”, the gradient will increase the linear actuator, resulting in an upward force 𝒗\boldsymbol{v}.

Consider another example where the same 2D circle is uncontrolled and resting on a linearly actuated platform (Figure 4). To lift the circle up, we need to utilize the contact force from the platform. If the initialization of the problem results in the contact point being classified as “separating”, no constraint force will be applied on the contact point, as if the contact did not exist. Therefore, the gradient of contact force with respect to the platform control force is zero, which may cause the optimization to be trapped in a saddle point.

Refer to caption


Figure 4: If the initial contact state is “separating”, the platform cannot apply contact force to the circle, resulting in zero gradient of the contact force with respect to the control force of the platform, 𝒇𝝉=𝟎\frac{\partial\bm{f}}{\partial\bm{\tau}}=\boldsymbol{0}. However, if the contact is classified as “clamping”, we can utilize the contact force from the platform to push the circle upwards by increasing the control force of the platform.

In both examples, the issues can be resolved by classifying the contact point differently. If we know that to solve this problem we need to actuate the circle and the platform separately, we would set the contact point to be “separating.” This gives the optimizer a chance to explore a solution with contact breaking. On the other hand, if we know that we need to exploit the contact force between the circle and the platform, we would relabel a separating contact point to be “clamping”. This lets the optimizer search for the most effective contact force that reduces the loss function.

Is there a way to know which strategy to use in advance? In the general case, the answer seems to be no, but we can do better than naive gradients which stick with whatever contact classification they were initialized into and do not explore to break out of resulting saddle points. We propose a heuristic to explore more broadly at a constant additional cost.

We propose that always picking the contact strategy that results in the largest 𝒒˙t22+𝝉t22Δt||\frac{\partial\ell}{\partial\dot{\boldsymbol{q}}_{t}}||_{2}^{2}+\frac{||\frac{\partial\ell}{\partial\bm{\tau}_{t}}||_{2}^{2}}{\Delta t} is a good heuristic during learning for avoiding saddle points. While we could try backprop through all 𝒪(2n)\mathcal{O}(2^{n}) possible strategies and pick the best one, that gets expensive as the number of contacts becomes larger than a small handful. Instead of exhaustively searching for all possible combinations of contact states with exponential complexity, we propose to only check two classifications:

  1. 1.

    First, check the “correct” contact classification solved by the LCP.

  2. 2.

    Second, we compute 𝒗\frac{\partial\ell}{\partial\boldsymbol{v}} (the gradient of loss with respect to relative contact velocity), and use the elements of 𝒗\frac{\partial\ell}{\partial\boldsymbol{v}} to compute the “clamping” and “separating” sets as follows. Take any indices ii that are trying to increase the relative velocity at that contact vi<0\frac{\partial\ell}{\partial v_{i}}<0, which implies the optimizer is feebly trying to separate the objects, and put those indices into “separating” to remove the constraint that vi=0v_{i}=0. Take any indices ii where vi>0\frac{\partial\ell}{\partial v_{i}}>0, which implies the optimizer is trying to move the objects closer together (which would violate contact constraints), and put them into “clamping” to impose the constraint that vi=0v_{i}=0.

The contact strategy with the larger 𝒒˙t22+𝝉t22Δt||\frac{\partial\ell}{\partial\dot{\boldsymbol{q}}_{t}}||_{2}^{2}+\frac{||\frac{\partial\ell}{\partial\bm{\tau}_{t}}||_{2}^{2}}{\Delta t} is used during backpropagation for learning. We call the gradient produced by this exploratory procedure a “Complementarity-aware Gradient,” and find empirically that it can help avoid saddle points during trajectory optimization. We show an example of this in Section VII.

The complementarity-aware gradients do not guarantee to improve global convergence because the gradient is chosen for each contact point independently, which might not result in an aggregated gradient 𝝉\frac{\partial\ell}{\partial\bm{\tau}} that moves in a globally optimal direction. However, if an optimization problem currently gets stuck in a saddle point, complementarity-aware gradients provide another tool for practitioners to try.

V Gradients through collision geometry

This section addresses efficient computation of 𝑱tT𝒇𝒒t\frac{\partial\boldsymbol{J}_{t}^{T}\boldsymbol{f}}{\partial\boldsymbol{q}_{t}}, the relationship between position and joint impulse. In theory, we could utilize auto-diff libraries for the derivative computation. However, using auto-diff and passing every gradient through a long kinematic chain of transformations is inefficient for complex articulated rigid body systems. In contrast, computing the gradients symbolically shortcuts much computation by operating directly in the world coordinate frame.

Let 𝒜ise(3)\mathcal{A}_{i}\in se(3) be the screw axis for the ii’th DOF, expressed in the world frame. Let the kk’th contact point give an impulse kdse(3)\mathcal{F}_{k}\in dse(3), also expressed in the world frame. Let ψi,k{1,0,1}\psi_{i,k}\in\{-1,0,1\} be the relationship between contact kk and joint ii. ψi,k=1\psi_{i,k}=1 or ψi,k=1\psi_{i,k}=-1 means contact kk is on a child body of joint ii. Otherwise, ψi,k=0\psi_{i,k}=0. The total joint impulse caused by contact impulses for the ii’th joint is given by:

(𝑱tT𝒇)i=kψi,k𝒜iTk=𝒜iTkψi,kk.(\boldsymbol{J}_{t}^{T}\boldsymbol{f})_{i}=\sum_{k}\psi_{i,k}\mathcal{A}_{i}^{T}\mathcal{F}_{k}=\mathcal{A}_{i}^{T}\sum_{k}\psi_{i,k}\mathcal{F}_{k}. (23)

Taking the derivative of Equation 23 gives

(𝑱tT𝒇)i𝒒t=𝒜i𝒒tTkψi,kk+𝒜iTkψi,kk𝒒t.\frac{\partial(\boldsymbol{J}_{t}^{T}\boldsymbol{f})_{i}}{\partial\boldsymbol{q}_{t}}=\frac{\partial\mathcal{A}_{i}}{\partial\boldsymbol{q}_{t}}^{T}\sum_{k}\psi_{i,k}\mathcal{F}_{k}+\mathcal{A}_{i}^{T}\sum_{k}\psi_{i,k}\frac{\partial\mathcal{F}_{k}}{\partial\boldsymbol{q}_{t}}. (24)

Evaluating 𝒜i𝒒t\frac{\partial\mathcal{A}_{i}}{\partial\boldsymbol{q}_{t}} is straightforward, but computing kqt\frac{\partial\mathcal{F}_{k}}{\partial q_{t}} requires understanding how the contact normal 𝒏k3\boldsymbol{n}_{k}\in\mathcal{R}^{3} and contact position 𝒑k3\boldsymbol{p}_{k}\in\mathcal{R}^{3} change with changes in 𝒒t\boldsymbol{q}_{t}.

Let k\mathcal{F}_{k} be a concatenation of torque and force in dse(3)dse(3). The derivative with respect to the current joint position 𝒒t\boldsymbol{q}_{t} is:

k𝒒t=[𝒏k𝒒t×𝒑k+𝒏k×𝒑k𝒒t𝒏k𝒒t]\frac{\partial\mathcal{F}_{k}}{\partial\boldsymbol{q}_{t}}=\begin{bmatrix}\frac{\partial\boldsymbol{n}_{k}}{\partial\boldsymbol{q}_{t}}\times\boldsymbol{p}_{k}+\boldsymbol{n}_{k}\times\frac{\partial\boldsymbol{p}_{k}}{\partial\boldsymbol{q}_{t}}\\ \frac{\partial\boldsymbol{n}_{k}}{\partial\boldsymbol{q}_{t}}\end{bmatrix} (25)

It turns out that computing 𝒏kqt\frac{\partial\boldsymbol{n}_{k}}{\partial q_{t}} for curved primitives shape colliders (spheres and capsules) requires different treatment from meshes or polygonal primitives. To understand why, consider using a high polycount mesh to approximate a true sphere as shown in Figure 5.

Refer to caption

Figure 5: 𝒏k𝒒\frac{\partial\boldsymbol{n}_{k}}{\partial\boldsymbol{q}} can be set to zero for mesh or polygonal geometry, but has to be computed analytically for curved geometry.

On a mesh approximation of a sphere, infinitesimally moving the contact point 𝒑k\boldsymbol{p}_{k} by ϵ\epsilon (by perturbing 𝒒\boldsymbol{q} of the triangle) will not cause the normal of the contact face 𝒏k\boldsymbol{n}_{k} to change at all, because we remain on the same face of the mesh. So 𝒏k𝒒\frac{\partial\boldsymbol{n}_{k}}{\partial\boldsymbol{q}} is always zero for a mesh or polygonal shape. However, on a true sphere, an infinitesimal perturbation of the contact point with the sphere (no matter how small) will cause the contact normal with the sphere 𝒏k\boldsymbol{n}_{k} to change. The way the contact normal 𝒏k\boldsymbol{n}_{k} changes with position (e.g. 𝒏k𝒒\frac{\partial\boldsymbol{n}_{k}}{\partial\boldsymbol{q}}) is often crucial information for the optimizer to have in order to solve complex problems, and mesh approximations to curved surfaces falsely set 𝒏k𝒒=0\frac{\partial\boldsymbol{n}_{k}}{\partial\boldsymbol{q}}=0.

We refer the interested reader to Appendix -C for a discussion of how we compute 𝒏k𝒒t\frac{\partial\boldsymbol{n}_{k}}{\partial\boldsymbol{q}_{t}} and 𝒑k𝒒t\frac{\partial\boldsymbol{p}_{k}}{\partial\boldsymbol{q}_{t}} for different combinations of collider types.

VI Gradients through elastic contacts

DiffTaichi [18] pointed out an interesting problem that arises from discretization of time in simulating elastic bouncing phenomenon between two objects. The problems arise from the discrete time integration of position: 𝒒t+1=𝒒t+Δt𝒒˙t\boldsymbol{q}_{t+1}=\boldsymbol{q}_{t}+\Delta t\dot{\boldsymbol{q}}_{t}. The Jacobians 𝒒t+1𝒒t=𝑰\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}=\boldsymbol{I} and 𝒒t+1𝒒˙t=Δt𝑰\frac{\partial\boldsymbol{q}_{t+1}}{\partial\dot{\boldsymbol{q}}_{t}}=\Delta t\boldsymbol{I} are correct for most scenarios. However, when an elastic collision occurs, the discrete time integration scheme creates problems for differentiation. In a discrete time world, the closer the object is to the collision site at the beginning of the time step when collision happens, the closer to the collision site it ends up at the end of that time step. In continuous time, however, the closer the object begins to its collision site, the further away it ends up, because the object changes velocity sooner (Figure 6).

Refer to caption

Figure 6: Discrete-time Jacobians do not adequately describe the dynamics of an elastic collision. In the system pictured here, letting 𝒒\boldsymbol{q} be a scalar giving the distance from the ground, discrete-time would lead us to falsely believe that 𝒒t+1𝒒=1\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}}=1. In continuous time, we would instead expect 𝒒t+1𝒒=σ\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}}=-\sigma, where σ\sigma is the coefficient of restitution. Modeling this requires understanding how the time of collision changes with initial conditions, and how that affects the final state after the step.

DiffTaichi [18] proposed using continuous-time collision detection and resolution during training, but noted that switching to discrete-time at test time did not harm performance. Since introducing full continuous-time collision detection is computationally costly for complex dynamic systems and scenes, we instead opt to find a 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} and 𝒒t+1𝒒˙t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\dot{\boldsymbol{q}}_{t}} that approximates the behavior of continuous-time Jacobians when elastic collisions occur.

Let 𝒑t,i\boldsymbol{p}_{t,i} be the relative distance at contact ii at time tt and 𝒗t,i\boldsymbol{v}_{t,i} be the relative velocity. We further define σi\sigma_{i} as the coefficient of restitution at contact ii and t+𝒄it+\boldsymbol{c}_{i} as the time of collision of contact ii, where 𝒄i\boldsymbol{c}_{i}\in\mathcal{R}. Assuming the relative velocity is constant in this time step, we get 𝒄i=𝒑i/𝒗i\boldsymbol{c}_{i}=-\boldsymbol{p}_{i}/\boldsymbol{v}_{i} and 𝒑t+1,i=(Δt𝒄i)σ𝒗t,i\boldsymbol{p}_{t+1,i}=(\Delta t-\boldsymbol{c}_{i})\sigma\boldsymbol{v}_{t,i}. From there we have:

𝒑i,t+1𝒑i,t=σi,𝒑i,t+1𝒗i,t=σiΔt.\frac{\partial\boldsymbol{p}_{i,t+1}}{\partial\boldsymbol{p}_{i,t}}=-\sigma_{i},\;\;\;\frac{\partial\boldsymbol{p}_{i,t+1}}{\partial\boldsymbol{v}_{i,t}}=-\sigma_{i}\Delta t. (26)

After finding the above gradients for each collision independently, we need to find a pair of Jacobians, 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} and 𝒒t+1𝒒˙t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\dot{\boldsymbol{q}}_{t}}, that approximate our desired behavior at each of the contact points as closely as possible.

𝒑i,t+1𝒑i,t\displaystyle\frac{\partial\boldsymbol{p}_{i,t+1}}{\partial\boldsymbol{p}_{i,t}} =𝒑i,t+1𝒒t+1𝒒t+1𝒒t𝒒t𝒑i,t\displaystyle=\frac{\partial\boldsymbol{p}_{i,t+1}}{\partial\boldsymbol{q}_{t+1}}\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}\frac{\partial\boldsymbol{q}_{t}}{\partial\boldsymbol{p}_{i,t}}
=𝑱i,t+1𝒒t+1𝒒t𝑱i,t1,fori=1m\displaystyle=\boldsymbol{J}_{i,t+1}\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}\boldsymbol{J}_{i,t}^{-1},\;\mathrm{for}\;i=1\cdots m (27)

where 𝑱i,t+1\boldsymbol{J}_{i,t+1} is the Jacobian matrix of contact ii, 𝑱i,t1\boldsymbol{J}_{i,t}^{-1} is the pseudo inverse Jacobian, and mm is the total number of contact points. Our goal is to find a Jacobian 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} that satisfies the above equations (Equation VI) as closely as possible (details in Appendix -E). Once we find a satisfactory approximation for 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}, we can get

𝒒t+1𝒒˙t=Δt𝒒t+1𝒒t.\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{\dot{q}}_{t}}=\Delta t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}. (28)
TABLE II: Benchmarks against Finite Differencing
Environment Analytical Central Speedup
Differences
Atlas 16.1ms 737ms 45.8x
Half Cheetah 0.870ms 7.52ms 8.64x
Jump-Worm 0.484ms 3.08ms 6.36x
Catapult 0.576ms 3.69ms 6.40x

VII Evaluation

First, we evaluate our methods by testing the performance of gradient computation. In addition, we compare gradient-based trajectory optimization enabled by our method to gradient-free stochastic optimization, and discuss implications. We also demonstrate the effectiveness of the complementarity-aware gradients in a trajectory optimization problem. Finally, we show that our physics engine can solve optimal control problems for complex dynamic systems with contact and collision, including an Atlas humanoid jumping in the air.

VII-A Performance

Controlled comparison to existing methods can be challenging because they use different formulations for forward simulation, essentially simulating different physical phenomena. We therefore benchmark the performance of our method at the atomic level–measuring the computation time of a Jacobian for a single time step using a single-core CPU and comparing it to finite differencing methods, using the same forward simulation process provided by an existing non-differentiable physics engine (DART). This comparison removes factors due to differences in forward simulation, implementation techniques, and computation resources, and focuses on the speed gain solely contributed by our gradient computation method.

Table II contains abbreviated benchmark performance of our Jacobians against central differencing. We evaluate our results in four environments: the Atlas robot on the ground (33 DOFs, 12 contact points), Half Cheetah on the ground (9 DOFs, 2 contact points), Jump-Worm (5 DOFs, 2 contact points), and Catapult (5 DOFs, 2 contact points). For each environment, we compare the speed of evaluating all five primary Jacobians. For a complete table, including the speed of individual Jacobian evaluations, see Appendix -D.

VII-B Gradient-based vs gradient-free trajectory optimization

To demonstrate the benefit of analytical Jacobians of dynamics, we compare trajectory optimization on our catapult trajectory problem between Multiple Shooting and Stochastic Search (SS) [5], as well as cartpole and the double-pendulum using both Differential Dynamic Programming (DDP) [39, 22, 40] and Stochastic Search [5]. The gradient-based methods (DDP and Multiple Shooting) are able to converge much more quickly, because the additional convergence gained from analytical Jacobians more than offsets the time to compute them. See Figure 7.

Refer to caption

Figure 7: Comparing wall clock time to find a single-pendulum cartpole (CP), double-pendulum cartpole (DCP), and catapult (CTPLT) trajectory, using DDP (D), SS (S), and Multiple Shooting (M). The results are unsurprising: gradient information speeds convergence tremendously.

VII-C Complementarity-aware gradients

To highlight the complementarity-aware gradients, we solve a trajectory optimization problem of a drone taking-off from the ground and reaching a fixed height in 500500 timesteps.

Because the drone is initialized resting on the ground, it has a contact with the ground that is classified as “clamping.” When we attempt to optimize the control on the drone using correct gradients, we get zero gradients and make no progress. By contrast, when we use our complementarity aware gradient, while we still see no change in loss for the first few iterations of SGD, we’re able to get non-zero gradients and escape the saddle point (Figure 7). See the supplementary video for the resulting drone trajectories.

Refer to caption

Figure 8: Training a drone to lift off the ground and fly to a target height after 500 timesteps. Loss is the squared distance of the drone from the target at t=500t=500. The drone is initialized resting on the ground, which means the drone-ground contacts are classified as “clamping.” That means Jacobians will show 𝒒˙t+1𝝉t=0\frac{\partial\dot{\boldsymbol{q}}_{t+1}}{\partial\boldsymbol{\tau}_{t}}=0. While both standard and complementarity-aware training runs start in a saddle point, the complementarity-aware gradients are able to guide SGD to escape after several iterations of learning.

VII-D Optimal control with contact

We present several trajectory optimization problems that involve complex contact dynamics, optimized using multiple shooting. We demonstrate “Catapult”, which is a 3-dof robot that is tasked with batting a free ball towards a target, in such a way that it exactly hits the target at the desired timestep (pictured in Figure 7). We also demonstrate “Jump-Worm”, which is a 5-dof worm-shaped robot that is attempting to jump as high as possible at the end of the trajectory. Both of these problems involve complex contact switching throughout the course of the trajectory. We also optimize a trajectory where the “Atlas” robot learns to jump. See the supplementary video for the resulting trajectories.

Refer to caption

Figure 9: A few snapshots of the trajectory that an Atlas robot learns when it is asked to jump up towards the green markers, starting from a crouch.

VIII Conclusions

We present a fast and feature-complete differentiable physics engine for articulated rigid body simulation. We introduce a method to compute analytical gradients through the LCP formulation of inelastic contact by exploiting the sparsity of the LCP solution. Our engine supports complex contact geometry and approximating continuous-time elastic collision. We also introduce a novel method to compute complementarity-aware gradients that help optimizers to avoid stalling in saddle points.

There a few limitations of our current method. Currently, computing 𝑱t𝒇𝒒t\frac{\partial\boldsymbol{J}_{t}\boldsymbol{f}}{\partial\boldsymbol{q}_{t}}, the way the joint torques produced by the contact forces change as we vary joint positions (which in turn varies contact positions and normals) takes a large portion of the total time to compute all Jacobians of dynamics for a given timestep. Perhaps a more efficient formulation can be found.

Our engine also doesn’t yet differentiate through the geometric properties (like link length) of a robot, or friction coefficients. Extending to these parameters is an important step to enable parameter estimation applications.

We are excited about future work that integrates gradients and stochastic methods to solve hard optimization problems in robotics. In running the experiments for this paper, we found that stochastic trajectory optimization methods could often find better solutions to complex problems than gradient-based methods, because they were able to escape from local optima. However, stochastic methods are notoriously sample inefficient, and have trouble fine-tuning results as they begin to approach a local optima. The authors speculate that there are many useful undiscovered techniques waiting to be invented that lie at the intersection of stochastic gradient-free methods and iterative gradient-based methods. It is our hope that an engine like the one presented in this paper will enable such research.

Acknowledgments

References

  • Al-Rfou et al. [2016] Rami Al-Rfou, Guillaume Alain, Amjad Almahairi, Christof Angermueller, Dzmitry Bahdanau, Nicolas Ballas, Frédéric Bastien, Justin Bayer, Anatoly Belikov, Alexander Belopolsky, et al. Theano: A python framework for fast computation of mathematical expressions. arXiv e-prints, pages arXiv–1605, 2016.
  • Amos and Kolter [2017] Brandon Amos and J Zico Kolter. Optnet: Differentiable optimization as a layer in neural networks. In Proceedings of the 34th International Conference on Machine Learning-Volume 70, pages 136–145. JMLR. org, 2017.
  • Baraff [1994] David Baraff. Fast contact force computation for nonpenetrating rigid bodies. In Proceedings of the 21st annual conference on Computer graphics and interactive techniques, pages 23–34, 1994.
  • Battaglia et al. [2016] Peter Battaglia, Razvan Pascanu, Matthew Lai, Danilo Jimenez Rezende, et al. Interaction networks for learning about objects, relations and physics. In Advances in neural information processing systems, pages 4502–4510, 2016.
  • Boutselis et al. [2020] George I Boutselis, Ziyi Wang, and Evangelos A Theodorou. Constrained sampling-based trajectory optimization using stochastic approximation. In 2020 IEEE International Conference on Robotics and Automation (ICRA), pages 2522–2528. IEEE, 2020.
  • Boyd et al. [2003] Stephen Boyd, Lin Xiao, and Almir Mutapcic. Subgradient methods. lecture notes of EE392o, Stanford University, Autumn Quarter, 2004:2004–2005, 2003.
  • Carpentier and Mansard [2018] Justin Carpentier and Nicolas Mansard. Analytical derivatives of rigid body dynamics algorithms. In Robotics: Science and Systems (RSS 2018), 2018.
  • Chang et al. [2016] Michael B Chang, Tomer Ullman, Antonio Torralba, and Joshua B Tenenbaum. A compositional object-based approach to learning physical dynamics. arXiv preprint arXiv:1612.00341, 2016.
  • Coumans [2010] Erwin Coumans. Bullet physics engine. Open Source Software: http://bulletphysics. org, 1(3):84, 2010.
  • de Avila Belbute-Peres et al. [2018] Filipe de Avila Belbute-Peres, Kevin Smith, Kelsey Allen, Josh Tenenbaum, and J Zico Kolter. End-to-end differentiable physics for learning and control. In Advances in Neural Information Processing Systems, pages 7178–7189, 2018.
  • Degrave et al. [2019] Jonas Degrave, Michiel Hermans, Joni Dambre, and Francis Wyffels. A differentiable physics engine for deep learning in robotics. Frontiers in neurorobotics, 13:6, 2019.
  • Geilinger et al. [2020] Moritz Geilinger, David Hahn, Jonas Zehnder, Moritz Bächer, Bernhard Thomaszewski, and Stelian Coros. Add: analytically differentiable dynamics for multi-body systems with frictional contact. ACM Transactions on Graphics (TOG), 39(6):1–15, 2020.
  • Hahn et al. [2019] David Hahn, Pol Banzet, James M. Bern, and Stelian Coros. Real2sim: Visco-elastic parameter estimation from dynamic motion. ACM Trans. Graph., 38(6), November 2019.
  • Heiden et al. [2019] Eric Heiden, David Millard, Hejia Zhang, and Gaurav S Sukhatme. Interactive differentiable simulation. arXiv preprint arXiv:1905.10706, 2019.
  • Heiden et al. [2020] Eric Heiden, David Millard, Erwin Coumans, and Gaurav S Sukhatme. Augmenting differentiable simulators with neural networks. arXiv preprint arXiv:2007.06045, 2020.
  • Hermans et al. [2014] Michiel Hermans, Benjamin Schrauwen, Peter Bienstman, and Joni Dambre. Automated design of complex dynamic systems. PLOS ONE, 9(1):1–11, 01 2014.
  • Holl et al. [2020] Philipp Holl, Nils Thuerey, and Vladlen Koltun. Learning to control pdes with differentiable physics. In International Conference on Learning Representations, 2020.
  • Hu et al. [2019a] Yuanming Hu, Luke Anderson, Tzu-Mao Li, Qi Sun, Nathan Carr, Jonathan Ragan-Kelley, and Frédo Durand. Difftaichi: Differentiable programming for physical simulation. arXiv preprint arXiv:1910.00935, 2019a.
  • Hu et al. [2019b] Yuanming Hu, Tzu-Mao Li, Luke Anderson, Jonathan Ragan-Kelley, and Frédo Durand. Taichi: a language for high-performance computation on spatially sparse data structures. ACM Transactions on Graphics (TOG), 38(6):1–16, 2019b.
  • Hu et al. [2019c] Yuanming Hu, Jiancheng Liu, Andrew Spielberg, Joshua B Tenenbaum, William T Freeman, Jiajun Wu, Daniela Rus, and Wojciech Matusik. Chainqueen: A real-time differentiable physical simulator for soft robotics. Proceedings of IEEE International Conference on Robotics and Automation (ICRA), 2019c.
  • Iollo et al. [2001] A. Iollo, M. Ferlauto, and L. Zannetti. An aerodynamic optimization method based on the inverse problem adjoint equations. Journal of Computational Physics, 173(1):87–115, 2001.
  • Jacobson and Mayne [1970] David H Jacobson and David Q Mayne. Differential dynamic programming. 1970.
  • Jarny et al. [1991] Y. Jarny, M.N. Ozisik, and J.P. Bardon. A general optimization method using adjoint equation for solving multidimensional inverse heat conduction. International Journal of Heat and Mass Transfer, 34(11):2911–2919, 1991.
  • Jourdan et al. [1998] Franck Jourdan, Pierre Alart, and Michel Jean. A gauss-seidel like algorithm to solve frictional contact problems. Computer Methods in Applied Mechanics and Engineering, 155(1):31–47, 1998.
  • Kim [2012] Junggon Kim. Lie group formulation of articulated rigid body dynamics. Technical report, Technical Report. Carnegie Mellon University, 2012.
  • Lee et al. [2018] Jeongseok Lee, Michael X Grey, Sehoon Ha, Tobias Kunz, Sumit Jain, Yuting Ye, Siddhartha S Srinivasa, Mike Stilman, and C Karen Liu. Dart: Dynamic animation and robotics toolkit. Journal of Open Source Software, 3(22):500, 2018.
  • Liang et al. [2019] Junbang Liang, Ming Lin, and Vladlen Koltun. Differentiable cloth simulation for inverse problems. In H. Wallach, H. Larochelle, A. Beygelzimer, F. d'Alché-Buc, E. Fox, and R. Garnett, editors, Advances in Neural Information Processing Systems, volume 32. Curran Associates, Inc., 2019.
  • McNamara et al. [2004] Antoine McNamara, Adrien Treuille, Zoran Popović, and Jos Stam. Fluid control using the adjoint method. ACM Trans. Graph., 23(3):449–456, August 2004.
  • Millard et al. [2020] David Millard, Eric Heiden, Shubham Agrawal, and Gaurav S. Sukhatme. Automatic differentiation and continuous sensitivity analysis of rigid body dynamics, 2020.
  • Mrowca et al. [2018] Damian Mrowca, Chengxu Zhuang, Elias Wang, Nick Haber, Li F Fei-Fei, Josh Tenenbaum, and Daniel L Yamins. Flexible neural representation for physics prediction. In Advances in neural information processing systems, pages 8799–8810, 2018.
  • Paszke et al. [2017] Adam Paszke, Sam Gross, Soumith Chintala, Gregory Chanan, Edward Yang, Zachary DeVito, Zeming Lin, Alban Desmaison, Luca Antiga, and Adam Lerer. Automatic differentiation in pytorch. 2017.
  • Popović et al. [2000] Jovan Popović, Steven M. Seitz, Michael Erdmann, Zoran Popović, and Andrew Witkin. Interactive manipulation of rigid body simulations. In Proceedings of the 27th Annual Conference on Computer Graphics and Interactive Techniques, SIGGRAPH ’00, page 209–217. ACM Press/Addison-Wesley Publishing Co., 2000.
  • Press et al. [2007] William H. Press, Saul A. Teukolsky, William T. Vetterling, and Brian P. Flannery. Numerical Recipes 3rd Edition: The Art of Scientific Computing. Cambridge University Press, USA, 3 edition, 2007.
  • Qiao et al. [2020] Yi-Ling Qiao, Junbang Liang, Vladlen Koltun, and Ming C. Lin. Scalable differentiable physics for learning and control. In ICML, 2020.
  • Rackauckas et al. [2018] Christopher Rackauckas, Yingbo Ma, Vaibhav Dixit, Xingjian Guo, Mike Innes, Jarrett Revels, Joakim Nyberg, and Vijay Ivaturi. A comparison of automatic differentiation and continuous sensitivity analysis for derivatives of differential equation solutions, 2018.
  • Ridders [1982] C.J.F. Ridders. Accurate computation of f’(x) and f’(x) f”(x). Advances in Engineering Software (1978), 4(2):75–76, 1982.
  • Schenck and Fox [2018] Connor Schenck and Dieter Fox. Spnets: Differentiable fluid dynamics for deep neural networks. In Proceedings of The 2nd Conference on Robot Learning, volume 87 of Proceedings of Machine Learning Research, pages 317–335. PMLR, 29–31 Oct 2018.
  • Song and Boularias [2020] Changkyu Song and Abdeslam Boularias. Learning to slide unknown objects with differentiable physics simulations. In Robotics: Science and Systems, Oregon State University at Corvallis, Oregon, USA, 14–16 July 2020.
  • Tassa et al. [2012] Yuval Tassa, Tom Erez, and Emanuel Todorov. Synthesis and stabilization of complex behaviors through online trajectory optimization. In 2012 IEEE/RSJ International Conference on Intelligent Robots and Systems, pages 4906–4913. IEEE, 2012.
  • Todorov and Li [2005] Emanuel Todorov and Weiwei Li. A generalized iterative lqg method for locally-optimal feedback control of constrained nonlinear stochastic systems. In Proceedings of the 2005, American Control Conference, 2005., pages 300–306. IEEE, 2005.
  • Todorov et al. [2012] Emanuel Todorov, Tom Erez, and Yuval Tassa. Mujoco: A physics engine for model-based control. In 2012 IEEE/RSJ International Conference on Intelligent Robots and Systems, pages 5026–5033. IEEE, 2012.
  • Toussaint et al. [2018] Marc Toussaint, Kelsey R. Allen, K. Smith, and J. Tenenbaum. Differentiable physics and stable modes for tool-use and manipulation planning. In Robotics: Science and Systems, 2018.
TABLE III: Full Benchmarks against Finite Differencing
Environment Jacobian Analytical Central Differences Speedup Ridders Speedup
Time Time Time
Atlas All 8.53ms 749ms 87.84x 2229ms 261x
𝒒t+1𝒒t\frac{\partial\bm{q}_{t+1}}{\partial\bm{q}_{t}} 0.0204ms 160ms 7850x 581ms 28480x
𝒒˙t+1𝒒t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{q}_{t}} 6.75ms 161ms 23.8x 581ms 86x
𝒒t+1𝒒˙t\frac{\partial\bm{q}_{t+1}}{\partial\dot{\bm{q}}_{t}} 0.016ms 107.2ms 6570.9x 304ms 19000x
𝒒˙t+1𝒒˙t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\dot{\bm{q}}_{t}} 0.778ms 160ms 205.7x 487ms 626x
𝒒˙t+1𝝉t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{\tau}_{t}} 0.981ms 161ms 163.9x 524ms 471x
Half Cheetah All 0.395ms 8.64ms 21.86x 24ms 60x
𝒒t+1𝒒t\frac{\partial\bm{q}_{t+1}}{\partial\bm{q}_{t}} 0.0059ms 1.69ms 284x 3.17ms 537x
𝒒˙t+1𝒒t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{q}_{t}} 0.254ms 1.874ms 7.35x 7.92ms 31.1x
𝒒t+1𝒒˙t\frac{\partial\bm{q}_{t+1}}{\partial\dot{\bm{q}}_{t}} 0.00358ms 1.228ms 342x 2.42ms 675.9x
𝒒˙t+1𝒒˙t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\dot{\bm{q}}_{t}} 0.053ms 1.98ms 37.1x 5.63ms 106.2x
𝒒˙t+1𝝉t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{\tau}_{t}} 0.077ms 1.87ms 24.1x 5.75ms 74.6x
Jump-Worm All 0.256ms 3.82ms 14.89x 9.727ms 37.9x
𝒒t+1𝒒t\frac{\partial\bm{q}_{t+1}}{\partial\bm{q}_{t}} 0.00433ms 0.732ms 168.8x 1.58ms 364.8x
𝒒˙t+1𝒒t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{q}_{t}} 0.174ms 0.836ms 4.78x 2.84ms 16.32x
𝒒t+1𝒒˙t\frac{\partial\bm{q}_{t+1}}{\partial\dot{\bm{q}}_{t}} 0.00307ms 0.532ms 173.1x 0.81ms 263.8x
𝒒˙t+1𝒒˙t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\dot{\bm{q}}_{t}} 0.0343ms 0.872ms 25.4x 2.17ms 63.2x
𝒒˙t+1𝝉t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{\tau}_{t}} 0.040ms 0.851ms 21.2x 2.31ms 57.75x
Catapult All 0.265ms 4.36ms 16.45x 12.0ms 45.2x
𝒒t+1𝒒t\frac{\partial\bm{q}_{t+1}}{\partial\bm{q}_{t}} 0.0050ms 0.845ms 167.8x 1.79ms 358x
𝒒˙t+1𝒒t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{q}_{t}} 0.181ms 0.972ms 5.36x 3.71ms 20.49x
𝒒t+1𝒒˙t\frac{\partial\bm{q}_{t+1}}{\partial\dot{\bm{q}}_{t}} 0.0036ms 0.574ms 156.7x 0.958ms 266x
𝒒˙t+1𝒒˙t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\dot{\bm{q}}_{t}} 0.035ms 1.01ms 28.42x 2.72ms 77.7x
𝒒˙t+1𝝉t\frac{\partial\dot{\bm{q}}_{t+1}}{\partial\bm{\tau}_{t}} 0.039ms 0.960ms 24.3x 2.85ms 73.1x

-A Frictional impulse

In DART, friction impulses are solved by the boxed LCP method, using the same implementation of the boxed variant of the Dantzig algorithm found in the Open Dynamics Engine, and originally proposed in [3]. Boxed LCPs are not theoretically guaranteed to be solvable, but are quite common in practice because of their speed and high-quality results. We therefore extend our formulation in Section IV to compute the gradient of frictional impulse magnitudes found in a boxed LCP solver with respect to 𝑨\boldsymbol{A} and 𝒃\boldsymbol{b}. Similar to normal impulses, each frictional impulse is classified into one of the two states:

Clamping (𝒞\mathcal{C})

If the relative velocity along the frictional impulse direction is zero, and friction impulse magnitude is below its bound, then any attempt to push this contact will be met with an increase in frictional impulse holding the point in place. This means the contact point is clamping, which behaves will be treated in the same way as clamped normal forces.

Bounded (\mathcal{B})

If the frictional impulse magnitude is at its bound (either positive or negative) then the contact point is sliding or about to slide along this friction direction. Bounded frictional impulse \mathcal{B} is quite like “Separating” 𝒮\mathcal{S} in normal impulse. The difference is that frictional impulses in \mathcal{B} are not zero but at a non-zero bound based on the corresponding normal impulses.

Bounded frictional impulse can be expressed by 𝒇=𝑬𝒇𝒞\boldsymbol{f}^{*}_{\mathcal{B}}=\boldsymbol{E}\boldsymbol{f}^{*}_{\mathcal{C}}, where each row of 𝑬||×|𝒞|\boldsymbol{E}\in\mathcal{R}^{|\mathcal{B}|\times|\mathcal{C}|} contains a single non-zero value with the friction coefficient, μs\mu_{s}, for the corresponding normal impulse index in 𝒞\mathcal{C}.

We define 𝑱𝒞\boldsymbol{J}_{\mathcal{C}} to be the matrix with just the columns of 𝑱\boldsymbol{J} corresponding to the indices in 𝒞\mathcal{C}. Likewise, we define 𝑱\boldsymbol{J}_{\mathcal{B}} containing just the columns of 𝑱\boldsymbol{J} that are bounded. If we multiply 𝑱𝒞T𝒇𝒞\boldsymbol{J}^{T}_{\mathcal{C}}\boldsymbol{f}_{\mathcal{C}} we get the joint torques due to the clamping constraint forces. Similarly, if we multiply 𝑱T𝒇\boldsymbol{J}^{T}_{\mathcal{B}}\boldsymbol{f}_{\mathcal{B}} we get the joint torques due to bounded friction impulses. Since 𝒇=𝑬𝒇𝒞\boldsymbol{f}_{\mathcal{B}}=\boldsymbol{E}\boldsymbol{f}_{\mathcal{C}}, we can modify 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} to take bounded frictional impulses into account:

𝑨𝒞𝒞=𝑱𝒞𝑴1(𝑱𝒞T+𝑱T𝑬)\boldsymbol{A}_{\mathcal{CC}}=\boldsymbol{J}_{\mathcal{C}}\boldsymbol{M}^{-1}(\boldsymbol{J}^{T}_{\mathcal{C}}+\boldsymbol{J}^{T}_{\mathcal{B}}\boldsymbol{E})

With this one small change, all the formulation in Section IV works with frictional forces. An interesting observation is that the bounded frictional cases are analogous to the separating cases for the normal forces, in that the force value is constrained at μsfi\mu_{s}f_{i} (or μsfi-\mu_{s}f_{i}), where fif_{i} is the corresponding normal force for the same contact. The only way the bounded force values will change is through the change of corresponding clamping normal force, which needs to be accounted for when computing the gradients.

Just like the overall boxed LCP problem is not solvable, 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} is no longer guaranteed to be exactly invertible. In order to support this, we need to use the pseudoinverse of 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} during the forward pass, and the gradient of the pseudoinverse when computing Jacobians.

-B LCP stabilization

When 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} is not full rank, the solution to fLCP(𝑨,𝒃)=𝒇f_{\text{LCP}}(\boldsymbol{A},\boldsymbol{b})=\boldsymbol{f}^{*} is no longer unique. To grasp this intuitively, consider a 2D case where a box of unit mass that cannot rotate is resting on a plane. The box has two contact points, with identical contact normals, and because the box is not allowed to rotate, the effect of an impulse at each contact point is exactly the same (it causes the box’s upward velocity to increase). This means that both columns of 𝑨\boldsymbol{A} (one per contact) are identical. That means that 𝑨𝒞𝒞2×2\boldsymbol{A}_{\mathcal{CC}}\in\mathcal{R}^{2\times 2} is actually only rank one. Let’s assume we need a total upward impulse of mg-mg to prevent the box from interpenetrating the floor. Because 𝑨𝒞𝒞\boldsymbol{A}_{\mathcal{CC}} is a low rank, we’re left with one equation and two unknowns:

𝑨𝒞𝒞𝒇𝒞=[1111][f𝒞1f𝒞2]=[f𝒞1+f𝒞2f𝒞1+f𝒞2]=[mgmg]\boldsymbol{A}_{\mathcal{CC}}\boldsymbol{f}^{*}_{\mathcal{C}}=\begin{bmatrix}1&&1\\ 1&&1\end{bmatrix}\begin{bmatrix}f^{*}_{{\mathcal{C}_{1}}}\\ f^{*}_{{\mathcal{C}}_{2}}\end{bmatrix}=\begin{bmatrix}f^{*}_{{\mathcal{C}}_{1}}+f^{*}_{{\mathcal{C}}_{2}}\\ f^{*}_{{\mathcal{C}}_{1}}+f^{*}_{{\mathcal{C}}_{2}}\end{bmatrix}=\begin{bmatrix}-mg\\ -mg\end{bmatrix}

It is easy to see that this is just f𝒞1+f𝒞2=mgf^{*}_{{\mathcal{C}}_{1}}+f^{*}_{{\mathcal{C}}_{2}}=-mg. And that means that we have an infinite number of valid solutions to the LCP.

In order to have valid gradients, our LCP needs to have predictable behavior when faced with multiple valid solutions. Thankfully, our analysis in the previous sections suggests a quite simple and efficient (and to the authors’ knowledge novel) LCP stabilization method. Once an initial solution is computed using any algorithm, and the clamping set 𝒞\mathcal{C} is found, we can produce a least-squares-minimal (and numerically exact) solution to the LCP by setting:

𝒇𝒞=𝑨𝒞𝒞1𝒃𝒞,𝒇𝒮=0\boldsymbol{f}^{*}_{\mathcal{C}}=\boldsymbol{A}_{\mathcal{CC}}^{-1}\boldsymbol{b}_{\mathcal{C}},\hskip 30.00005pt\boldsymbol{f}^{*}_{\mathcal{S}}=0

This is possible with a single matrix inversion because the hard part of the LCP problem (determining which indices belong in which classes) was already solved for us by the main solver. Once we know which indices belong in which classes, solving the LCP exactly reduces to simple linear algebra.

As an interesting aside, this “LCP stabilization” method doubles as an extremely efficient LCP solver for iterative LCP problems. In practical physics engines, most contact points do not change from clamping (𝒞\mathcal{C}) to separating (𝒮\mathcal{S}) or back again on most time steps. With that intuition in mind, we can opportunistically attempt to solve a new fLCP(𝑨t+1,𝒃t+1)=𝒇t+1f_{\text{LCP}}(\boldsymbol{A}_{t+1},\boldsymbol{b}_{t+1})=\boldsymbol{f}_{t+1}^{*} at a new timestep by simply guessing that the contacts will sort into 𝒞\mathcal{C} and 𝒮\mathcal{S} in exactly the same way they did on the last time step. Then we can solve our stabilization equations for 𝒇t+1\boldsymbol{f}_{t+1}^{*} as follows:

𝒇t+1𝒞=𝑨t+1𝒞𝒞1𝒃t+1𝒞,𝒇t+1𝒮=0{\boldsymbol{f}_{t+1}}^{*}_{\mathcal{C}}={\boldsymbol{A}_{t+1}}_{\mathcal{CC}}^{-1}{\boldsymbol{b}_{t+1}}_{\mathcal{C}},\hskip 30.00005pt{\boldsymbol{f}_{t+1}}^{*}_{\mathcal{S}}=0

If we guessed correctly, which we can verify in negligible time, then 𝒇t+1\boldsymbol{f}_{t+1}^{*} is a valid, stable, and perfectly numerically exact solution to the LCP. When that happens, and in our experiments this heuristic is right >95%>95\% of the time, we can skip the expensive call to our LCP solver entirely. As an added bonus, because 𝒞\mathcal{C} is usually not all indices, inverting 𝑨t+1𝒞𝒞{\boldsymbol{A}_{t+1}}_{\mathcal{CC}} can be considerably cheaper than inverting all of 𝑨t+1\boldsymbol{A}_{t+1}, which can be necessary in an algorithm to solve the full LCP.

When our heuristic does not result in a valid 𝒇t+1\boldsymbol{f}_{t+1}^{*}, we can simply return to our ordinary LCP solver to get a valid set 𝒞\mathcal{C} and 𝒮\mathcal{S}, and then re-run our stabilization.

As long as results from our LCP are stabilized, the gradients through the LCP presented in this section are valid even when 𝑨\boldsymbol{A} is not full rank.

-C Contact point and normal stabilization and gradients

To compute gradients through each contact point and normal with respect to 𝒒t\boldsymbol{q}_{t}, we need to provide specialized routines for each type of collision geometry, including collisions between spheres, capsules, boxes, and arbitrary convex meshes. Deriving the routines is a matter of straightforward algebra. However in order for well-defined gradients to exist at all, each collision must have precisely specified contact points and normals, so that we can compute well-behaved gradients.

We describe in this appendix how we produce well-defined contact points and normals, since this is a design choice. The gradients of these methods are left as an exercise to the reader, who is invited to check their work against our open-source implementation.

-C1 Mesh-mesh collisions

Mesh-mesh collisions only have two types of contacts: vertex-face and edge-edge collisions. The other cases (vertex-edge, vertex-vertex, edge-face, face-face) are degenerate and easily mapped into vertex-face and edge-edge collisions.

Mesh-mesh collision detection algorithms like Gilbert-Johnson-Keerthi or Minkowski Portal Refinement are iterative, and so produce imprecise collision points and normals that can vary depending on initialization. Both algorithms produce a separating “witness plane” as part of their output, which is a 3D plane that approximately separates the two meshes (as much as possible, if they’re intersecting). Going from an approximate separating witness plane to precisely specified vertex-face and edge-edge collisions in the general case is complex. Take all the points on object A that lie within some tiny ϵ\epsilon of the witness plane, and map them into 2D coordinates within the witness plane. Do likewise with the points on object B. Now we have two convex 2D shapes, because any linear slice of a convex shape is itself convex. Throw out any vertices that are not on the 2D convex hull of the point cloud for A and B respectively. Call the resulting convex shapes “witness hulls” of A and B. Now any vertices on the witness hull of A that lie within the witness hull of B are vertex-face collisions from A to B. Analogously, any vertices on the witness hull of B that lie within the witness hull of A are face-vertex collisions from A to B. Finally, any edges of the witness hulls A and B that intersect are edge-edge collisions.

For all vertex-face collisions, during the forward simulation, the collision detector places a collision at the point of the vertex, with a normal dictated by the face under collision. The body providing the vertex can only influence the collision location 𝒑\boldsymbol{p}, and the body providing the face can only influence the collision normal 𝒏\boldsymbol{n}.

For all edge-edge collisions, the collision detector first finds the nearest point on edge A to edge B (call that 𝒂\boldsymbol{a}), and the nearest point on edge B to edge A (call that 𝒃\boldsymbol{b}). Then the contact point is set to the average of 𝒂\boldsymbol{a} and 𝒃\boldsymbol{b}, which is 𝒂+𝒃2\frac{\boldsymbol{a}+\boldsymbol{b}}{2}. The normal is given by the cross product of the two edges. That means changing 𝒒\boldsymbol{q} of Object A can affect the contact normal and the contact location along the other edge from Object B. For this reason, we need to construct our Jacobians globally.

-C2 Sphere-sphere collisions

These are straightforward. We have sphere A, with center cac_{a} and radius rar_{a}, and sphere B, with center cbc_{b} and radius rbr_{b}. The contact normal is the normalized vector pointing from cbc_{b} to cac_{a}. The contact point is a weighted combination of the two sphere centers, rbca+racbra+rb\frac{r_{b}*c_{a}+r_{a}*c_{b}}{r_{a}+r_{b}}.

-C3 Mesh-sphere collisions

These can be divided into three categories: sphere-face collisions, sphere-edge collisions, and sphere-vertex collisions. Starting from the simplest, a sphere-vertex collision places the contact point at the vertex and the normal points from the vertex to the sphere center.

A sphere-edge collision places the contact point on the closest point to the sphere center along the edge. The contact normal points from the collision point to the sphere center.

A sphere-face collision gets the contact normal from the normal of the colliding face. Call the contact normal 𝒏\boldsymbol{n}, and define the sphere center as 𝒄\boldsymbol{c} and radius as 𝒓\boldsymbol{r}. For the simplicity of downstream gradient computation, we then place the contact point at the point on the sphere, projected along with the contact normal towards the contact: 𝒄+𝒓𝒏\boldsymbol{c}+\boldsymbol{r}*\boldsymbol{n}.

-C4 Pipe-pipe collisions

In a capsule-capsule collision, when both capsule cylinders are colliding, we call that a pipe-pipe collision. Mechanically, this is very similar to an edge-edge collision, only with added radius variables rar_{a} and rbr_{b}. Let cac_{a} be the nearest point on the centerline of pipe A to the centerline of pipe B. Let cbc_{b} be the nearest point on the centerline of pipe B to the centerline of pipe A. Then we say the contact point is rbca+racbra+rb\frac{r_{b}*c_{a}+r_{a}*c_{b}}{r_{a}+r_{b}}. The contact normal points from cbc_{b} to cac_{a}.

-C5 Pipe-sphere collisions

These look a lot like pipe-pipe collisions, except that cbc_{b} is now fixed to the center of the sphere. Let cac_{a} be the nearest point on the centerline of the pipe to cbc_{b}. Then we say the contact point is rbca+racbra+rb\frac{r_{b}*c_{a}+r_{a}*c_{b}}{r_{a}+r_{b}}. The contact normal points from cbc_{b} to cac_{a}.

-C6 Pipe-mesh collisions

This breaks down into two types of contact: vertex-pipe, and edge-pipe. Face-pipe contacts reduce to two edge-pipe contacts.

For vertex-pipe contacts, we put the contact point at the vertex, and point the normal towards the nearest point on the centerline of the pipe.

For edge-pipe contacts, we treat them as pipe-pipe contacts where the radius of the first pipe is 0.

-C7 Gradients

Once all the contact behavior is firmly established, it’s just a matter of rote calculus to compute derivatives. We refer the reader to our open source code for the implementation of those derivatives. Once a stable collision detection system and its derivatives are implemented that, it’s possible to efficiently compute JtTfqt\frac{\partial J_{t}^{T}f}{\partial q_{t}}.

-D Jacobian Benchmark Evaluation

In addition to speed, we are interested in the accuracy of our Jacobians, as gradients computed via finite differencing become more inaccurate as the system becomes more complex, which can lead to instability in optimization. As another baseline, we apply Ridders’ method [36] to efficiently calculate Jacobians to a higher-order error than central differencing, using the stopping criterion given in [33].

Table III contains the full benchmark performance of our analytical Jacobians against central differencing and the accurate Ridders’ extrapolated finite differences. For each environment, we compare the speed of evaluation of each individual component Jacobian as well as the total time.

-E Calculating the Bounce Approximation Jacobian

Recall the matrix 𝑱T\boldsymbol{J}^{T} that transforms contact forces to joint forces. By conservation of momentum 𝑱𝒒˙t=𝒗t\boldsymbol{J}\dot{\boldsymbol{q}}_{t}=\boldsymbol{v}_{t}, leading to:

𝒑t+1𝒑t𝑱𝒒t+1𝒒t𝑱1\frac{\partial\boldsymbol{p}_{t+1}}{\partial\boldsymbol{p}_{t}}\approx\boldsymbol{J}\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}\boldsymbol{J}^{-1} (29)

Our matrix notation is somewhat misleading here, because we do not want our approximation to capture off-diagonals of 𝒑t+1𝒑t\frac{\partial\boldsymbol{p}_{t+1}}{\partial\boldsymbol{p}_{t}}. Because we construct our approximate 𝒑t+1𝒑t\frac{\partial\boldsymbol{p}_{t+1}}{\partial\boldsymbol{p}_{t}} by assuming each bounce is independent, we end up with 0s on the off-diagonals, but we know that assumption to be false. We just want to find a 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} where the above equation matches the diagonals as closely as possible, ignoring other elements. The following derives this relation in closed form.

Since we are only interested in enforcing the diagonal entries, so we can write out a series of linear constraints we would like to attempt to satisfy. Let 𝑱(i)\boldsymbol{J}_{(i)} denote the ii’th column of 𝑱\boldsymbol{J}. Recall that 𝒑i,t+1𝒑i,t=σi\frac{\partial\boldsymbol{p}_{i,t+1}}{\partial\boldsymbol{p}_{i,t}}=-\sigma_{i}.

𝑱(i)𝒒t+1𝒒t𝑱(i)1σi\boldsymbol{J}_{(i)}\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}\boldsymbol{J}^{-1}_{(i)}\approx-\sigma_{i} (30)

We would like to find some approximate matrix 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} that satisfies all nn of the above constraints as closely as possible. Stated directly as a least-squares optimization object:

mini(𝑱(i)𝒒t+1𝒒t𝑱(i)1(σi))2\text{min}\sum_{i}\big{(}\boldsymbol{J}_{(i)}\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}\boldsymbol{J}^{-1}_{(i)}-(-\sigma_{i})\big{)}^{2} (31)

The solution to this optimization can be found without iterative methods.

Let 𝒒t+1𝒒t(i)\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}_{(i)} be the ii’th column of 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}, and 𝑱(j,i)\boldsymbol{J}_{(j,i)} be the jj’th row and the ii’th column of 𝑱\boldsymbol{J}. Note that:

𝑱(i)𝒒t+1𝒒t𝑱(i)1=j𝑱(j,i)scalar(𝑱(i)T𝒒t+1𝒒t(j))\boldsymbol{J}_{(i)}\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}\boldsymbol{J}^{-1}_{(i)}=\sum_{j}\underbrace{\boldsymbol{J}_{(j,i)}}_{\text{scalar}}(\boldsymbol{J}^{-T}_{(i)}\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}_{(j)}) (32)
𝑱(j,i)scalar(𝑱(i)T𝒒t+1𝒒t(j))=j(𝑱(j,i)𝑱(i)T)vectorT𝒒t+1𝒒t(j))\underbrace{\boldsymbol{J}_{(j,i)}}_{\text{scalar}}(\boldsymbol{J}^{-T}_{(i)}\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}_{(j)})=\sum_{j}{\underbrace{(\boldsymbol{J}_{(j,i)}\boldsymbol{J}^{-T}_{(i)})}_{\text{vector}}}^{T}\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}_{(j)}) (33)

It becomes clear that we could construct a long vector 𝒗\boldsymbol{v}, which will map to every column of 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} placed end to end. We can also construct a matrix 𝑾\boldsymbol{W} where every column 𝑾(i)\boldsymbol{W}_{(i)} is the vectors 𝑱(j,i)𝑱(i)1\boldsymbol{J}_{(j,i)}\boldsymbol{J}^{-1}_{(i)} placed end to end (iterating over jj). Now if we take the values of σi\sigma_{i} as entries of a vector 𝒓n\boldsymbol{r}\in\mathcal{R}^{n}, we can write our optimization problem as a linear equation:

mini(𝑱(i)𝒒t+1𝒒t𝑱(i)1(σi))2=min 𝑾T𝒗+𝒓22\text{min}\sum_{i}\big{(}\boldsymbol{J}_{(i)}\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}\boldsymbol{J}^{-1}_{(i)}-(-\sigma_{i})\big{)}^{2}=\text{min }||\boldsymbol{W}^{T}\boldsymbol{v}+\boldsymbol{r}||_{2}^{2} (34)

This is a standard least squares problem, and is solved when:

𝒗=𝑾T𝒓\boldsymbol{v}=-\boldsymbol{W}^{T\dagger}\boldsymbol{r} (35)

Once we have a value of 𝒗\boldsymbol{v}, we can reconstruct the original matrix 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} by taking each column of 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} the appropriate segment of 𝒗\boldsymbol{v}.

This is almost always an under-determined system, and we want to default to having 𝒒t+1𝒒t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} as close to II as possible, rather than as close to 0 as possible. We can slightly reformulate our optimization problem where the least square objective tries to keep the diagonals at 1, rather than 0. If we define an arbitrary 𝒄\boldsymbol{c} vector (for “center”), we can use the identity:

𝑾T(𝒗𝒄)=𝒓𝑾T𝒄\boldsymbol{W}^{T}(\boldsymbol{v}-\boldsymbol{c})=-\boldsymbol{r}-\boldsymbol{W}^{T}\boldsymbol{c} (36)
𝒗=𝒄𝑾T(𝒓+𝑾T𝒄)\boldsymbol{v}=\boldsymbol{c}-\boldsymbol{W}^{T\dagger}(\boldsymbol{r}+\boldsymbol{W}^{T}\boldsymbol{c}) (37)

If we set 𝒄\boldsymbol{c} to the mapping for 𝒒t+1𝒒t=I\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}}=I, then we get a solution that minimizes the distance to the identity while satisfying the constraints, measured as the sum of the squares of all the terms.

Also, remember that to get our Jacobian with respect to velocity, we simply:

dθt+1dθ˙t=Δt𝒒t+1𝒒t\frac{d\theta_{t+1}}{d\dot{\theta}_{t}}=\Delta t\frac{\partial\boldsymbol{q}_{t+1}}{\partial\boldsymbol{q}_{t}} (38)

And that should approximately solve the “gradient bounce” problem. On timesteps where there is only a single bouncing contact, this will provide an exact solution. With more than one bounce in a single frame, this may be approximate.

-F Analytical derivatives through Featherstone

We present the derivation of analytical derivatives computation through the Featherstone algorithm. Although the intellectual computation should be credited entirely to [25] and [7], we specialized the derivations to obtain 𝑴t1𝒛t𝒒t\frac{\partial\boldsymbol{M}_{t}^{-1}\boldsymbol{z}_{t}}{\partial\boldsymbol{q}_{t}}, 𝒄t𝒒t\frac{\partial\boldsymbol{c}_{t}}{\partial\boldsymbol{q}_{t}}, and 𝒄t𝒒˙t\frac{\partial\boldsymbol{c}_{t}}{\partial\dot{\boldsymbol{q}}_{t}} for our implementation, rather than the entire inverse and forward dynamics. The detailed derivation might be of interest to some readers.

The partial derivative of the inverse of joint space inertia matrix can be computed from the partial derivative of the joint space inertia matrix through the relation [7]:

𝑴1𝒛𝒒\displaystyle\frac{\partial{\boldsymbol{M}^{-1}\boldsymbol{z}}}{\partial{\boldsymbol{q}}} =𝑴1𝑴𝒒𝑴1𝒛.\displaystyle=\boldsymbol{M}^{-1}\frac{\partial{\boldsymbol{M}}}{\partial{\boldsymbol{q}}}\boldsymbol{M}^{-1}\boldsymbol{z}. (39)

Algorithm 1 and 2 show the recursive algorithms to compute 𝑴1\boldsymbol{M}^{-1} and 𝑴𝒒𝑴1𝒛\frac{\partial{\boldsymbol{M}}}{\partial{\boldsymbol{q}}}\boldsymbol{M}^{-1}\boldsymbol{z}, respectively. In Algorithm 3, the derivatives of the Coriolis force with respect to the joint position and the joint velocity are given.

Algorithm 1 Recursive forward dynamics for 𝑴1\boldsymbol{M}^{-1}
1:for j=1 to nj=1\text{ to }n do
2:     for i=n to 1i=n\text{ to }1 do
3:         𝓘^i=𝓘i+lμ(i)Ad𝑻i,l1𝚷lAd𝑻i,l1\hat{\boldsymbol{\mathcal{I}}}_{i}=\boldsymbol{\mathcal{I}}_{i}+\sum_{l\in\mu(i)}\text{Ad}_{\boldsymbol{T}_{i,l}^{-1}}^{*}\boldsymbol{\Pi}_{l}\text{Ad}_{\boldsymbol{T}_{i,l}^{-1}}
4:         𝓑^i=lμ(i)Ad𝑻i,l1𝜷l\hat{\boldsymbol{\mathcal{B}}}_{i}=\sum_{l\in\mu(i)}\text{Ad}_{\boldsymbol{T}_{i,l}^{-1}}^{*}\boldsymbol{\beta}_{l}
5:         𝚿i=(𝑺iT𝓘^i𝑺i)1\boldsymbol{\Psi}_{i}=\left(\boldsymbol{S}_{i}^{T}\hat{\boldsymbol{\mathcal{I}}}_{i}\boldsymbol{S}_{i}\right)^{-1}
6:         𝚷i=𝓘^i𝓘^i𝑺iΨi𝑺iT𝓘^i\boldsymbol{\Pi}_{i}=\hat{\boldsymbol{\mathcal{I}}}_{i}-\hat{\boldsymbol{\mathcal{I}}}_{i}\boldsymbol{S}_{i}\Psi_{i}\boldsymbol{S}_{i}^{T}\hat{\boldsymbol{\mathcal{I}}}_{i}
7:         𝜶i=δi,j𝑺iT𝓑^i\boldsymbol{\alpha}_{i}=\delta_{i,j}-\boldsymbol{S}_{i}^{T}\hat{\boldsymbol{\mathcal{B}}}_{i}, where δi,j={1if i=j0,otherwise\delta_{i,j}=\begin{cases}1&\text{if }i=j\\ 0,&\text{otherwise}\end{cases}
8:         𝜷i=𝓑^i+𝓘^i𝑺i𝚿i𝜶i\boldsymbol{\beta}_{i}=\hat{\boldsymbol{\mathcal{B}}}_{i}+\hat{\boldsymbol{\mathcal{I}}}_{i}\boldsymbol{S}_{i}\boldsymbol{\Psi}_{i}\boldsymbol{\alpha}_{i}
9:     end for
10:     for i=1 to ni=1\text{ to }n do
11:         [𝑴1]i,j=𝚿i(𝜶i𝑺iT𝓘^iAd𝑻λ(i),i1𝑽˙λ(i))\left[\boldsymbol{M}^{-1}\right]_{i,j}=\boldsymbol{\Psi}_{i}\left(\boldsymbol{\alpha}_{i}-\boldsymbol{S}_{i}^{T}\hat{\boldsymbol{\mathcal{I}}}_{i}\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\dot{\boldsymbol{V}}_{\lambda(i)}\right)
12:         𝑽˙i=Ad𝑻λ(i),i1𝑽˙λ(i)+𝑺i[𝑴1]i,j\dot{\boldsymbol{V}}_{i}=\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\dot{\boldsymbol{V}}_{\lambda(i)}+\boldsymbol{S}_{i}\left[\boldsymbol{M}^{-1}\right]_{i,j}
13:     end for
14:end for
Algorithm 2 Derivative of the recursive inverse dynamics for 𝑴𝒒𝑴1𝒛\frac{\partial{\boldsymbol{M}}}{\partial{\boldsymbol{q}}}\boldsymbol{M}^{-1}\boldsymbol{z}
1:for i=1 to ni=1\text{ to }n do
2:     𝑽˙i=Ad𝑻λ(i),i1𝑽˙λ(i)+𝑺i[𝑴1𝒛]i\dot{\boldsymbol{V}}_{i}=\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\dot{\boldsymbol{V}}_{\lambda(i)}+\boldsymbol{S}_{i}\left[\boldsymbol{M}^{-1}\boldsymbol{z}\right]_{i}
3:     for j=1 to nj=1\text{ to }n do
4:         𝑽˙i𝒒j=Ad𝑻λ(i),i1𝑽˙λ(i)𝒒jad𝒉i𝒒jAd𝑻λ(i),i1𝑽˙λ(i)\frac{\partial{\dot{\boldsymbol{V}}_{i}}}{\partial{\boldsymbol{q}_{j}}}=\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\frac{\partial{\dot{\boldsymbol{V}}_{\lambda(i)}}}{\partial{\boldsymbol{q}_{j}}}-\text{ad}_{\frac{\partial{\boldsymbol{h}_{i}}}{\partial{\boldsymbol{q}_{j}}}}\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\dot{\boldsymbol{V}}_{\lambda(i)}
5:         +𝑺i𝒒j[𝑴1𝒛]i~{}~{}~{}~{}~{}~{}~{}~{}+\frac{\partial{\boldsymbol{S}_{i}}}{\partial{\boldsymbol{q}_{j}}}\left[\boldsymbol{M}^{-1}\boldsymbol{z}\right]_{i}
6:     end for
7:end for
8:for i=n to 1i=n\text{ to }1 do
9:     𝑭i=𝓘i𝑽˙i+lμ(i)Ad𝑻i,l1𝑭l\boldsymbol{F}_{i}=\boldsymbol{\mathcal{I}}_{i}\dot{\boldsymbol{V}}_{i}+\sum_{l\in\mu(i)}\text{Ad}_{\boldsymbol{T}_{i,l}^{-1}}^{*}\boldsymbol{F}_{l}
10:     for j=1 to nj=1\text{ to }n do
11:         𝑭i𝒒j=𝓘i𝑽˙i𝒒j𝑭g𝒒j\frac{\partial{\boldsymbol{F}_{i}}}{\partial{\boldsymbol{q}_{j}}}=\boldsymbol{\mathcal{I}}_{i}\frac{\partial{\dot{\boldsymbol{V}}_{i}}}{\partial{\boldsymbol{q}_{j}}}-\frac{\partial{\boldsymbol{F}^{\text{g}}}}{\partial{\boldsymbol{q}_{j}}}
12:         +lμ(i)Ad𝑻i,l1(𝑭l𝒒jad𝒉i𝒒j𝑭l)~{}~{}~{}~{}~{}~{}~{}~{}~{}+\sum_{l\in\mu(i)}\text{Ad}_{\boldsymbol{T}_{i,l}^{-1}}^{*}\left(\frac{\partial{\boldsymbol{F}_{l}}}{\partial{\boldsymbol{q}_{j}}}-\text{ad}_{\frac{\partial{\boldsymbol{h}_{i}}}{\partial{\boldsymbol{q}_{j}}}}^{*}\boldsymbol{F}_{l}\right)
13:         [𝑴𝒒𝑴1𝒛]i,j=𝑺i𝒒jT𝑭i+𝑺iT𝑭i𝒒j\left[\frac{\partial{\boldsymbol{M}}}{\partial{\boldsymbol{q}}}\boldsymbol{M}^{-1}\boldsymbol{z}\right]_{i,j}=\frac{\partial{\boldsymbol{S}_{i}}}{\partial{\boldsymbol{q}_{j}}}^{T}\boldsymbol{F}_{i}+\boldsymbol{S}_{i}^{T}\frac{\partial{\boldsymbol{F}_{i}}}{\partial{\boldsymbol{q}_{j}}}
14:     end for
15:end for
Algorithm 3 Derivative of the recursive inverse dynamics for 𝒄𝒒\frac{\partial{\boldsymbol{c}}}{\partial{\boldsymbol{q}}} and 𝒄𝒒˙\frac{\partial{\boldsymbol{c}}}{\partial{\dot{\boldsymbol{q}}}}
1:for i=1 to ni=1\text{ to }n do
2:     𝑽i=Ad𝑻λ(i),i1𝑽λ(i)+𝑺i𝒒˙i\boldsymbol{V}_{i}=\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\boldsymbol{V}_{\lambda(i)}+\boldsymbol{S}_{i}\dot{\boldsymbol{q}}_{i}
3:     𝑽˙i=Ad𝑻λ(i),i1𝑽˙λ(i)+ad𝑽i𝑺i𝒒˙i+𝑺˙i𝒒˙i\dot{\boldsymbol{V}}_{i}=\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\dot{\boldsymbol{V}}_{\lambda(i)}+\text{ad}_{\boldsymbol{V}_{i}}\boldsymbol{S}_{i}\dot{\boldsymbol{q}}_{i}+\dot{\boldsymbol{S}}_{i}\dot{\boldsymbol{q}}_{i}
4:     for j=1 to nj=1\text{ to }n do
5:         𝑽i𝒒j=Ad𝑻λ(i),i1𝑽λ(i)𝒒jad𝒉i𝒒jAd𝑻λ(i),i1𝑽λ(i)+𝑺i𝒒j𝒒˙i\frac{\partial{\boldsymbol{V}_{i}}}{\partial{\boldsymbol{q}_{j}}}=\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\frac{\partial{\boldsymbol{V}_{\lambda(i)}}}{\partial{\boldsymbol{q}_{j}}}-\text{ad}_{\frac{\partial{\boldsymbol{h}_{i}}}{\partial{\boldsymbol{q}_{j}}}}\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\boldsymbol{V}_{\lambda(i)}+\frac{\partial{\boldsymbol{S}_{i}}}{\partial{\boldsymbol{q}_{j}}}\dot{\boldsymbol{q}}_{i}
6:         𝑽i𝒒˙j=Ad𝑻λ(i),i1𝑽λ(i)𝒒˙j+𝑺ik\frac{\partial{\boldsymbol{V}_{i}}}{\partial{\dot{\boldsymbol{q}}_{j}}}=\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\frac{\partial{\boldsymbol{V}_{\lambda(i)}}}{\partial{\dot{\boldsymbol{q}}_{j}}}+\boldsymbol{S}_{i}^{k}
7:         𝑽˙i𝒒j=Ad𝑻λ(i),i1𝑽˙λ(i)𝒒jad𝒉i𝒒jAd𝑻λ(i),i1𝑽˙λ(i)\frac{\partial{\dot{\boldsymbol{V}}_{i}}}{\partial{\boldsymbol{q}_{j}}}=\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\frac{\partial{\dot{\boldsymbol{V}}_{\lambda(i)}}}{\partial{\boldsymbol{q}_{j}}}-\text{ad}_{\frac{\partial{\boldsymbol{h}_{i}}}{\partial{\boldsymbol{q}_{j}}}}\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\dot{\boldsymbol{V}}_{\lambda(i)}
8:         +ad𝑽i𝒒j𝑺i𝒒˙i+ad𝑽i𝑺i𝒒j𝒒˙i+𝑺˙i𝒒j𝒒˙i~{}~{}~{}~{}~{}~{}~{}~{}+\text{ad}_{\frac{\partial{\boldsymbol{V}_{i}}}{\partial{\boldsymbol{q}_{j}}}}\boldsymbol{S}_{i}\dot{\boldsymbol{q}}_{i}+\text{ad}_{\boldsymbol{V}_{i}}\frac{\partial{\boldsymbol{S}_{i}}}{\partial{\boldsymbol{q}_{j}}}\dot{\boldsymbol{q}}_{i}+\frac{\partial{\dot{\boldsymbol{S}}_{i}}}{\partial{\boldsymbol{q}_{j}}}\dot{\boldsymbol{q}}_{i}
9:         𝑽˙i𝒒˙j=Ad𝑻λ(i),i1𝑽˙λ(i)𝒒˙j\frac{\partial{\dot{\boldsymbol{V}}_{i}}}{\partial{\dot{\boldsymbol{q}}_{j}}}=\text{Ad}_{\boldsymbol{T}_{\lambda(i),i}^{-1}}\frac{\partial{\dot{\boldsymbol{V}}_{\lambda(i)}}}{\partial{\dot{\boldsymbol{q}}_{j}}}
10:         +ad𝑽i𝒒˙j𝑺i𝒒˙i+ad𝑽i𝑺ik+𝑺˙i𝒒˙j𝒒˙i+𝑺˙ik~{}~{}~{}~{}~{}~{}~{}~{}+\text{ad}_{\frac{\partial{\boldsymbol{V}_{i}}}{\partial{\dot{\boldsymbol{q}}_{j}}}}\boldsymbol{S}_{i}\dot{\boldsymbol{q}}_{i}+\text{ad}_{\boldsymbol{V}_{i}}\boldsymbol{S}_{i}^{k}+\frac{\partial{\dot{\boldsymbol{S}}_{i}}}{\partial{\dot{\boldsymbol{q}}_{j}}}\dot{\boldsymbol{q}}_{i}+\dot{\boldsymbol{S}}_{i}^{k}
11:     end for
12:end for
13:for i=n to 1i=n\text{ to }1 do
14:     𝑭i=𝓘i𝑽˙iad𝑽i𝓘i𝑽i𝑭ig+lμ(i)Ad𝑻i,l1𝑭l\boldsymbol{F}_{i}=\boldsymbol{\mathcal{I}}_{i}\dot{\boldsymbol{V}}_{i}-\text{ad}_{\boldsymbol{V}_{i}}^{*}\boldsymbol{\mathcal{I}}_{i}\boldsymbol{V}_{i}-\boldsymbol{F}_{i}^{g}+\sum_{l\in\mu(i)}\text{Ad}_{\boldsymbol{T}_{i,l}^{-1}}^{*}\boldsymbol{F}_{l}
15:     for j=1 to nj=1\text{ to }n do
16:         𝑭i𝒒j=𝓘i𝑽˙iqjad𝑽iqj𝓘𝑽iad𝑽i𝓘i𝑽i𝒒j𝑭g𝒒j\frac{\partial{\boldsymbol{F}_{i}}}{\partial{\boldsymbol{q}_{j}}}=\boldsymbol{\mathcal{I}}_{i}\frac{\partial{\dot{\boldsymbol{V}}_{i}}}{\partial{q_{j}}}-\text{ad}_{\frac{\partial{\boldsymbol{V}_{i}}}{\partial{q_{j}}}}^{*}\boldsymbol{\mathcal{I}}\boldsymbol{V}_{i}-\text{ad}_{\boldsymbol{V}_{i}}^{*}\boldsymbol{\mathcal{I}}_{i}\frac{\partial{\boldsymbol{V}_{i}}}{\partial{\boldsymbol{q}_{j}}}-\frac{\partial{\boldsymbol{F}^{\text{g}}}}{\partial{\boldsymbol{q}_{j}}}
17:         +lμ(i)Ad𝑻i,l1(𝑭l𝒒jad𝒉i𝒒j𝑭l)~{}~{}~{}~{}~{}~{}~{}~{}~{}+\sum_{l\in\mu(i)}\text{Ad}_{\boldsymbol{T}_{i,l}^{-1}}^{*}\left(\frac{\partial{\boldsymbol{F}_{l}}}{\partial{\boldsymbol{q}_{j}}}-\text{ad}_{\frac{\partial{\boldsymbol{h}_{i}}}{\partial{\boldsymbol{q}_{j}}}}^{*}\boldsymbol{F}_{l}\right)
18:         𝑭i𝒒˙j=𝓘i𝑽˙i𝒒˙jad𝑽i𝒒˙j𝓘𝑽iad𝑽i𝓘i𝑽i𝒒˙j\frac{\partial{\boldsymbol{F}_{i}}}{\partial{\dot{\boldsymbol{q}}_{j}}}=\boldsymbol{\mathcal{I}}_{i}\frac{\partial{\dot{\boldsymbol{V}}_{i}}}{\partial{\dot{\boldsymbol{q}}_{j}}}-\text{ad}_{\frac{\partial{\boldsymbol{V}_{i}}}{\partial{\dot{\boldsymbol{q}}_{j}}}}^{*}\boldsymbol{\mathcal{I}}\boldsymbol{V}_{i}-\text{ad}_{\boldsymbol{V}_{i}}^{*}\boldsymbol{\mathcal{I}}_{i}\frac{\partial{\boldsymbol{V}_{i}}}{\partial{\dot{\boldsymbol{q}}_{j}}}
19:         +lμ(i)Ad𝑻i,l1𝑭l𝒒j~{}~{}~{}~{}~{}~{}~{}~{}~{}+\sum_{l\in\mu(i)}\text{Ad}_{\boldsymbol{T}_{i,l}^{-1}}^{*}\frac{\partial{\boldsymbol{F}_{l}}}{\partial{\boldsymbol{q}_{j}}}
20:         [𝒄𝒒]i,j=𝑺i𝒒jT𝑭i+𝑺iT𝑭i𝒒j\left[\frac{\partial{\boldsymbol{c}}}{\partial{\boldsymbol{q}}}\right]_{i,j}=\frac{\partial{\boldsymbol{S}_{i}}}{\partial{\boldsymbol{q}_{j}}}^{T}\boldsymbol{F}_{i}+\boldsymbol{S}_{i}^{T}\frac{\partial{\boldsymbol{F}_{i}}}{\partial{\boldsymbol{q}_{j}}}
21:         [𝒄𝒒˙]i,j=𝑺iT𝑭i𝒒˙j\left[\frac{\partial{\boldsymbol{c}}}{\partial{\dot{\boldsymbol{q}}}}\right]_{i,j}=\boldsymbol{S}_{i}^{T}\frac{\partial{\boldsymbol{F}_{i}}}{\partial{\dot{\boldsymbol{q}}_{j}}}
22:     end for
23:end for