The practical part then consists mainly in a generic algorithm which can automatically build ... Where K is the system kinetic energy, qi the i th ..... E n e r g y. Kinetic energy. Total energy. Potential energy. Time. Figure 4 - Runge-Kutta 4, dt=0.01.
A GENERIC ALGORITHM FOR THE GENERATION OF PHYSICAL MOVEMENTS OF SOLID SYSTEMS Didier GILLARD, Jean-Michel NOURRIT, Laurent LUCAS, Yannick REMION
Laboratoire d’Etudes et de Recherches en Informatique Université de Reims Champagne-Ardenne Rue des Crayères, BP 1035, 51687 REIMS Cedex 2 Tél : (33) 03.26.05.30.88; Fax : (33) 03.26.05.30.55 Email : {didier.gillard, laurent.lucas, jm.nourrit, yannick.remion}@univ-reims.fr
ABSTRACT This paper presents an engine for dynamic animation of solid systems based upon Lagrangian equations. The goal is to animate in an automatic way any scene composed with rigid objects defined in hierarchical structures. Animation control is provided by the description of the various degrees of freedom linking the objects, the inertial elements of these solids, the forces applied to bodies, as well as the supplementary constraints imposed upon objects. The advantages of using object oriented programming and particularly polymorphism to implement these items are briefly discussed. The physical theoretical background is based on an explained derivation of the Partial Differential Equations System formed by Lagrangian equations into an Ordinary second order Differential Equations System. The practical part then consists mainly in a generic algorithm which can automatically build this accurate ODE system from the minimal decription of any scene. Dealing with its resolution some examples are shown in order to compare the merits of some classical numerical methods and to discuss numerical stability problems. Keywords :dynamic animation, Lagrangian equations, simulation.
INTRODUCTION The dynamic animation algorithm presented takes part in a broader project, whose purpose is the dynamic animation of a virtual human body, with as much realism as possible. This project assumes that the best way to achieve natural simulation is to use data and mecanisms mostly from the real world. This can be done on two levels : · A human body model will be constructed from IRM or X ray tomography. This
provides not only the external shape of human body (using segmentation and 3D reconstruction of volume data), but also internal data, whether anthropometric or mechanical (mass, inertia matrix, gravity center), which will be used for animation control. This animation will be applied to rigid objects in a hierarchical organisation which are static approximations of the various parts of a human body (arms, forearms, legs, torso, head, …).
· For mechanical modelisation, in order to create a smooth and natural animation, it seems necessary to write a dynamic animation algorithm of solids systems based upon the known physical laws. There are three possible ways to achieve dynamic animation of rigid bodies : · The first one consists in simulating the movement from the initial data about objects and the forces and torques applied to them. [Wilhems-Barski85,Armstrong-Green85] use this principle and generate the movement equations from Newton's and Euler's equations. It is difficult here to handle the forces and torques to obtain some specific movement.
First of all the technical choices on which this animation engine is built (mainly use of Lagrangian formalism and general handling method for Lagrange's equations) are presented. A mathematical development of Lagrange’s equations then provides the equivalent and accurate ordinary differential equations (ODE) system used. This system is numerically built by the generic algorithm next described. The numerical resolution is provided by conventional ODE solving methods whose merits are compared. Finally, results and future developments are discussed. TECHNICAL CHOICES Lagrangian Formalism
· The second way [Barzel-Barr88] associates kinematics (inverse dynamics) and dynamics. The control of movement comes from kinematics as geometrical constraints upon objects. [Isaacs-Cohen88] use this model and generate the movement equations from the Lagrangian formalism.
The Lagrangian formalism with constraints has been choosen. In order to discuss its merits and thus justify this choice, let’s have a look to its expression [Germain86] :
· The third way mixes keyframing and the dynamic model. The user specifies some positions (keyframes) and the computer calculates the mechanical evolution of the objects during the time interval between these positions. [Witkin-Kass88] use some physical formulation based upon Lagrange's equations and add a motion of timepositioning constraint. This one is handled as a minimisation of the energy needed to go from one spatial position to the next.
Where K is the system kinetic energy, qi the ith parameter for the system's positioning and Qi the additional strains power (i.e. other than those needed to hold the intrinsic links) in the unitary movement ( q& i = 1 ) instilled by qi.
Some specific animation engines are also dedicated to the human body. Human walk has been especially studied ([Bruderlin88, Bruderlin-Calvert89,McKenna-Zeltzer90]). This paper’s intent is to show and explain the mechanisms involved in the creation of the movement equations and their numerical resolution. The proposed algorithm achieves generic creation and resolution of movement equations, and may be applied to many mechanical solids systems.
m d ∂K ∂K − = Qi + ∑ λjLji i ∈[1, n] dt ∂q& i ∂qi j=1
(1)
Furthermore, λj is linked to the strain needed to hold the jth constraint and Lji is the jth constraint influence on qi's evolution. The advantages of this equations system's are : · straight animation of the parameter naturally used by the user to point out the positions of the objects. · automatic handling of the forces intrinsic to the system's parameters (strains holding associated links, a priori unknown). · Accurate implicit realization of intrinsic links · engine genericity (thanks to constraints, it is possible to animate any solid system, even if the links create cycles). The objects used in our engine are defined in a tree, and linked by a sequence of elementary reference frame switching depending at most
on a scalar (or quaternion for 3D rotation) position parameter. Links cycles are modelled by adding constraints. Usable constraints are usually classified into three groups [Germain86] : · holonomial constraints, i.e. equations : h j (q ) = 0 (2.1) · unilateral u j (q ) ≥ 0
constraints, i.e. inequations : (2.2)
· kinematical constraints, i.e. equations : c jq& + d j = 0 (2.3) Lagrangian Equations handling Usually generic solid systems animation engines based upon Lagrangian equations proceed in one of the two following ways [Arnaldi et al.89, Arnaldi94] : · Straightforward numerical resolution of the PDE system (Eq. 1) which implies multiple evaluations of the kinetic energy function K. This leads to an approximated solution of an approximated system. · Numerical resolution of a system of equations obtained by symbolic computation of the PDE system (Eq. 1) which first requires the computation of the formal expression of K in the specific scene studied. This leads to an accurate system, the only methodological approximations lying in the numerical resolution stage which is unavoidable in a generic engine as very few scenes can yield to a formal solution. The advantages of symbolic computation over direct numerical resolution are mainly : · Accuracy of the numerical system solved. · Possible symbolic simplification of the equations, based upon built-in abilities of the required symbolic computation kernel. and the main drawbacks are [Arnaldi94,Cozot et al.96] : · high computation time and memory requirements.
· need to develop an effective and rather specific symbolic computation kernel. · need to develop a dedicated code generator to obtain efficiency for each specific scene. This paper presents a new way to use Lagrangian equations in a generic solids systems animation engine that offers the accuracy of the numerical system to be solved, no need for a symbolic computation kernel and some minimal simplifications of computation. It is based upon a mathematical derivation given in next chapter of the PDE system (Eq. 1) as an Ordinary second order Differential Equations system. If the structure of this ODE system is rather traditional and well-known in the computer graphics community [Bruderlin88, Bruderlin-Calvert89, IsaacsCohen88, Platt92], as the authors know it, the computation of its terms is usually either specific to the studied problem [Bruderlin88] or not mentionned of [Isaacs-Cohen88, Platt92]. As a generic engine was intended the shaping of the ODE system will be extended deep enough to allow for a generic computation of all the terms from basic description of the scene by the new generic algorithm provided for the ODE system creation. The numerical resolution of the system is done by classical methods. Object Oriented Programming usefulness OOP and particularly polymorphism allowed to develop an engine which deals with abstract types (for solids, strains, constraints and links) by using virtual functions, implemented in a specific and efficient way for each instantiation type. The main advantages obtained are : · minimal interaction between engine and specific objects (solids and so on) developments. · easy integration of new objects in the library · efficient handling of each specific object (which provides the minimal simplifications earlier mentioned mainly consisting in avoiding operations involving neutral or absorbant elements).
MATHEMATICAL DERIVATION OF THE SYSTEM
The absolute velocity of any point p of Sk and the kinetic energy are given by1 2 :
Global description
Vk (q& , q , p) =
Wishing to find a general algorithm for numerical resolution, one needs to find a way to derive numerical equations from formal and automatically computable expressions.
d dt
n
Φ k (q , p) = ∑ Φ ik (q , p) ⋅ q& i (6) i=0
N
K(q& , q ) =
1 2
∑ ∫∫∫ µ
k
( p) Vk (q& , q, p)2 dp
(7)
k = 1 p ∈S k
∂Φ k ∂q i
The movement equations come straightforward from a system built by Lagrange's equations with multipliers (Eq. 1) and constraints (Eq. 2.1 2.2 2.3). Some successive manipulations lead to a vectorial and differential ( &&q linearity) expression of Lagrange's equations. It is convenient there to derivate the constraints in order to have a system providing &&q linearity. The system to solve becomes :
Using (Eq. 6, 7) the kinetic energy becomes :
A (q ) && q + B(q& , q ) = Q(q& , q ) + λT L(q ) L(q ) && q + E(q& , q ) = 0
A jg (q ) = A gj (q ) =
(3.1) (3.2)
where A is the generalized mass matrix, &&q the system parameters second order derivatives vector, B the inertial forces handling vector. The system's unknown quantities are &&q and the l's. We can write (Eq. 3.1) and (Eq. 3.2) as : A' x + B' = Q'
(4.1)
where : A A' = L
q&& B Q − Lt x = B' = Q' = λ E 0 0
(4.2)
With the partial derivatives noted : Φ ik =
K(q& , q ) = 21 q& t A (q ) q&
(8)
where A (q) is a symetric square matrix whose elements are : N
∑A
k jg
(q )
(9)
k =1
k A kjg (q ) = A gj (q )
= ∫∫∫ µ k ( p) ⋅ Φ kj (q , p) • Φ gk (q , p) ⋅ dp (10) p∈S k
Lagrange's equations (Eq. 1), applied to the kinetic energy formula (Eq. 8) for j ∈[1, n ] , leads to (Eq. 3.1), which is a system built with n second order differential equations, providing && q linearity where B(q& , q ) ’s components are : n
∂A ig
j,g = 0
∂q j
Bi (q& , q ) = ∑ q& j q& g (
(q ) −
1 2
∂A jg ∂q i
(q )) (11)
Then, the system's solution comes from : x = A' −1 (Q' − B' )
Expressions for the terms A, B, Q (5)
Details on the shaping of the ODE system Let’s suppose the system is composed with N objects Sk, whose positions are given by n parameters q i . To make things easier let’s add the time, say t, as the number 0 coordinate of q. There are then N position functions Φ k (one per object), parametrized by q. These functions give the absolute position of every point linked to Sk.
The formulas in (Eq. 10) need integrations on the solids domains, which means timeconsuming operations, which have to be done during each step of the resolution, for they make use of time depending terms. It could be interesting to extract the variable terms which are linked to the movement from the integrals. This would mean only one time evaluation for each numerical (or formal for mathematical objects) integration. 1
with the convention q& 0 = &t = 1
2
where µ k ( p ) is S k ‘s density at p
The velocities formula (Eq. 6) leads to a physical interpretation of Φ ik (q , p) . As a matter of fact these terms represent the velocities of the point p linked to Sk, in the virtual movements of the system where only qi varies, with unitary velocity (i.e. q& j = δ ij , even for j=0). Thus, they are the moments of kinematical wrenches wholly known by their reduction elements at Sk’s gravity center gk whose formal expressions can be derived from Φ k and its derivatives as [Remion95] : · resultant, instant rotation vector 3 : Φ ik (q , y) − Φ ik (q , o) • Z k Ω ik (q ) = Φ ik (q , z) − Φ ik (q , o) • X k (12) Φ i ( q , x) − Φ i ( q , o) • Y k k k
( ( (
) ) )
· moment, velocity of g k : Vki (q ) = Φ ik (q , g k ) Thanks to these reduction elements the variable terms can be extracted from the integrals in (Eq. 10), leading to the following formula : A kjg (q ) = m k ⋅ Vkj (q ) • Vkg (q ) + Ω kj (q ) ⋅ Ι k ⋅ Ω gk (q )
(13)
where mk is Sk’s mass and Ik its inertia matrix at its gravity center, gk So, for each object, precalculating its mass, its gravity center and its inertia matrix allows to get rid of the costly integration operations. Knowing Φ k and its first order partial derivatives in q for each Sk and each step, the matrix A can be created according to (Eq. 9) and (Eq. 13). From (Eq. 11), (Eq. 9) and (Eq. 13), it is obvious that knowing also Φ k ’s second order partial derivatives allows for B’s calculation.
3
where x=(1,0,0), y=(0,1,0), z=(0,0,1), o=(0,0,0) are points linked to Sk with the specified coordinates in the reference frame linked to Sk and Xk , Xk , Zk the axis vectors of this reference frame expressed in the globlal reference frame.
In Lagrange’s equations (Eq. 1), the Q i (q& , q ) terms represent the additional strains power (i.e. other than those needed to hold the intrinsic links) in the above virtual movements. Knowing these movements kinematical wrenches and the strains definitions is enough to compute Q. GENERIC ALGORITHM FOR THE SYSTEM CREATION General principle The rigid objects are organised in a tree with relative positioning to the father, obtained with the help of a sequence of elementary reference frame switching depending at most on one scalar or quaternion parameter (for quaternion representation of 3D rotations avoids ambiguities inherent to Euler’s angles [Hanson94, Keller et al.94]). It stands out that, in addition to these elementary reference frame switching, each Sk must store its absolute positioning function Φ k and its first and second order partial derivatives. As positioning an object seldom depends on the whole set of parameters, it seems to be useful to identify the q k i relevant positioning parameters for each Sk, in order to save storage space and computation time. Partial derivatives will only be calculated and stored for these parameters. Moreover, it will be judicious to store the instant rotation vectors Ω kk i (q) for Q’s calculation. The equation creation is done (after resetting A’, B’, Q’ to 0) in three steps : · hierarchical scan on the objects to compute the position function and its derivatives, and update of A and B in A’ and B’ · linear scan on the additional strains to fill Q in Q’, by using the derivatives and instant rotation vectors stored in the objects · linear scan on the constraints to fill L in A’ and E in B’
the object for the forces). Their value is a vector, which may depend on the parameters.
Hierarchical scan on the objects For each object Sk, the process is divided into two steps : · update the various reference frame switching, starting from the father’s, by sequentially advancing in the elementary reference frame switching list. The reference frame switching are calculated using conventional methods for combination and derivation. Restricting elementary reference frame switching to only one scalar or quaternion parameter allows these updates for low combinational operations. · loop the ki, indices of the active parameters of Φ k and calculate : * instant rotation vectors, Ω kk i (q) , stored in Sk * A
k kik j
terms from (Eq. 13), added to
A[ k i , k j ] according to (Eq. 9) *
and update of B, according to (Eq. 11) :
and
· update the force vector F · loop on the ka i , indices of the active parameters of Φ ka , to add to Q[ ka i ] the force power of F in the virtual movement of S ka instilled by q ka i : ka
F • Φ ka i (q , Pa ) , derivative stored in S ka . · same loop for S ks , Ps and -F if the strain is internal (action/reaction principle). The process is similar for each torque : · update the torque vector C · loop on the ka i , indices of the active ka
∂ A kk k terms (derivation of (Eq. 13)), i g ∂q k j
add
For each force, the followings have to be done:
q& k j ⋅ q& k g ⋅
∂ A kk i k g ∂q k j
to B[ k i ]
− 21 q& k i ⋅ q& k g ⋅
∂ A kk i k g ∂q k j
to B[ k j ]
* Sk’s weight power : − m k ⋅ g ⋅ Z • Vkk i (q ) added to Q[ k i ] At the end of the scanning of the objects, A and B are fully created, and Q holds the objects weight power for the required virtual movements. Sequential scan on additional strains The additional strains can be of two types : forces or torques. They are always applied to an object S ka (with an action point Pa, linked to the object for the forces). They can be external to the system or internal (i.e. from an other object S ks , with an action point Ps, linked to
parameters of Φ ka , to add C • Ω k i (q ) to Q[ ka i ] (rotation vector stored in S ka ) · same loop for S ks and -C if the strain is internal to the system. Constraints handling The Lij terms in Lagrange’s equations (Eq. 1) and their reformulations (Eq. 3.1, 3.2, 4.2) are obtained as the q& factors in the linear constraints expressions involving velocities : · holonomial constraints, h j (q ) = 0 lead to n ∂h j ∂h j q& i = 0 implying L ji = . ∑ ∂q i i = 0 ∂q i · kinematic constraints, c jq& + d j = 0 imply directly L ji = c ji . · non realized unilateral constraints, u j (q ) > 0 do not constraint movement at time t and thus imply L ji = 0 .
realized unilateral constraints, u j (q ) = 0 n
lead to ∑
∂u j
i = 0 ∂q i
q& i = 0 if the constraint is
persistent ( > 0 otherwise) so L ji =
∂u j ∂q i
.
So, velocities constraints build the following equations system : >
L(q ) • q& + D(q ) = 0
(13)
Some tests on potential energy systems (in which every strain is derived from a potential which implies that the total mechanical energy of the system must be constant) show that Runge-Kutta methods yield much better results. The global mechanical energy is « a lot more constant », does not suffer from accumulation drift and provides very low amplitude fluctuations, (see fig 1 to 4 below for an example). Roundabout example :
=0 for holonomial, kinematic and persistent realized unilateral constraints
Z
>0 for non persistent realized unilateral constraints
Y
Stable constraints on accelerations (Eq. 3.2) are obtained by derivating (Eq. 13), restricted to holonomial, kinematic and persistent realized unilateral constraints, adding restoring forces as in [Baumgarte72, Platt92]. Unilateral constraints are dealt with as in [Platt92] by piecewise ODE resolution with specific treatment of realizations of these constraints by impulses. SYSTEM RESOLUTION
DoF 2
s1
X
DoF 1 s3
s2
DoF : Degre of Freedom DoF 1 : rotation around Z axis, initiated with 10 rad/s DoF2, DoF3 : free rotation around Y axis
Figure 1 -Scene modellisation 7
E n e r g y
Kinetic energy Total energy
4
Knowing how the accurate differential equations system is numerically created, it is time to emphasize on its resolution. ∆ q &
q && Written as y& = F(y ) , with y = , y& = and q q& A' (y) −1 io (Q' (y) − B' (y)) F( y ) = q& ∆
DoF3
3
2
Potential energy
1
0
-1 0
0.5
(14)
the system (Eq. 5) gets numerically solved by conventional methods [Press et al. 91]. Three of these methods have initially been studied in order to compare the quality of the approximation as well as the complexity of the computation : · Euler’s method (error O(dt2)) · Second order Runge-Kutta (error O(dt3)). · Fourth order Runge-Kutta (error O(dt5)).
1.5
2
2.5
3
3.5
4
Time
5
Figure 2 - Euler, dt=0.01
[ ]
i ∈ 1, n
1
7
E n e r g y
Kinetic energy Total energy
4
3
2
Potential energy
1
0
-1 0
0.5
1
1.5
2
2.5
3
3.5
4
Time
Figure 3 - Runge-Kutta 2, dt=0.01
5
7
E n e r g y
As a consequence of this observation, an adaptative fourth order Runge-Kutta method [Press et al. 91] has been used with success.
Kinetic energy Total energy
4
3
2
Potential energy 1
0
-1 0
0.5
1
1.5
2
2.5
3
3.5
4
Time
5
Figure 4 - Runge-Kutta 4, dt=0.01 However, the precision of the Fourth order Runge-Kutta method was not always sufficient even with a rather small time step (dt=0.001). As a matter of fact, some movements which imply internal impulses between objects can make position parameters or velocities vary too quickly. This happens in the following example where a cube falls until a bar seized in a horizontal slide roughly stops its fall.
This method is based upon time step variations according to the current approximation’s precision. Not only does this method solve the quick variation problems (Figure 7) but it also speeds up computation by constantly tuning the time step according to the precision required and the movement currently obtained (Figure 8). E n e r g y
Kinetic energy Total energy Potential energy
Shock example :
Time
Figure 7 - Energy, adaptative Runge-Kutta 4 Z slide Y
D t
Ctr 1
s2 X DoF2 s1 DoF1
DoF1 : translation along Z, initiated with 0 DoF2 : Rotation around Y, initiated with PI/2 Ctr 1 : Constraint to force Z=0
Time
Figure 8 -Time step, adaptative Runge-Kutta 4
Figure 5 -Scene modellisation Potential energy
E n e r g y
Total energy
Kinetic energy
Time
Figure 6 - Energy, Runge-Kutta 4, dt=0.0001
RESULTS The engine is now operational and can be applied to systems including several geometrical and homogeneous objects like cylinders, cubes and so on, but also any solid known by its inertial elements (see Figure 9 for an example of the interface of wire visualization for an intermediate state of an obtained animation).
CPU execution time in s 800 700 600
n independent bars dt 0.001,RK4 adaptative RK4 n-pendulus dt 0.001,RK4 adaptative RK4
500 400 300 200
3
7
n
Figure 10 -Extreme total execution times CONCLUSION AND PROSPECT Figure 9 -Visualization example The adaptative fourth order Runge-Kutta algorithm seems the choice resolution method for it automatically keeps the quality of the animation in the desired range and greatly speeds up animation computation. Thirteen seconds of CPU time are needed to calculate a 10 seconds animation of the scene described in Figure 6. With the non adaptative fourth order Runge-Kutta method, one has to fix the time step dt to 0.00005 in order to obtain the same quality of animation for the same example and so 525 seconds of CPU time are needed, which means waiting 45 times longer. Moreover the hierarchical scan on objects is the process of higher complexity o(n.p3) where n is the number of objects and p is the maximum number of dof each object depends upon. According to the complexity of the scene this can lead to a real complexity varying between o(n) for n independently positioned solids (tree of depth 1, p