Download Rigid Body Simulation (1)

Document related concepts

N-body problem wikipedia , lookup

Hamiltonian mechanics wikipedia , lookup

Four-vector wikipedia , lookup

Lagrangian mechanics wikipedia , lookup

Specific impulse wikipedia , lookup

Fictitious force wikipedia , lookup

Virtual work wikipedia , lookup

Newton's theorem of revolving orbits wikipedia , lookup

Jerk (physics) wikipedia , lookup

Hooke's law wikipedia , lookup

Analytical mechanics wikipedia , lookup

Symmetry in quantum mechanics wikipedia , lookup

Velocity-addition formula wikipedia , lookup

Laplace–Runge–Lenz vector wikipedia , lookup

Force wikipedia , lookup

Derivations of the Lorentz transformations wikipedia , lookup

Classical mechanics wikipedia , lookup

Theoretical and experimental justification for the Schrödinger equation wikipedia , lookup

Mass versus weight wikipedia , lookup

Photon polarization wikipedia , lookup

Computational electromagnetics wikipedia , lookup

Accretion disk wikipedia , lookup

Matter wave wikipedia , lookup

Tensor operator wikipedia , lookup

Angular momentum operator wikipedia , lookup

Precession wikipedia , lookup

Routhian mechanics wikipedia , lookup

Angular momentum wikipedia , lookup

Moment of inertia wikipedia , lookup

Torque wikipedia , lookup

Center of mass wikipedia , lookup

Relativistic mechanics wikipedia , lookup

Momentum wikipedia , lookup

Centripetal force wikipedia , lookup

Classical central-force problem wikipedia , lookup

Kinematics wikipedia , lookup

Inertia wikipedia , lookup

Newton's laws of motion wikipedia , lookup

Equations of motion wikipedia , lookup

Relativistic angular momentum wikipedia , lookup

Rigid body dynamics wikipedia , lookup

Transcript
Fundamentals of
Computer Animation
Rigid Body Simulation (1)
Rigid Body Simulation
• Realistic-looking motion
• Physically based reaction of rigid bodies to
forces such as gravity, viscosity, friction,
and collisions.
Equations
of Motion
Animation
System
Automatic
calculation of
reactions
Rigid Body Simulation
Collision Detection
Collision Response
Rigid Body Simulation:
forces due to:
• Relative positioning of objects
– Gravity
– Collisions, …
• Object velocity
– Viscosity, …
• Absolute positions of objects in userspecified vector-fields
– Wind, …
Forces applied to objects =
• Linear accelerations
based on the mass of object
• Angular accelerations
based on mass distribution of the object
Rigid body simulation update cycle
Forces
Changes in object
positions and orientations
Integrated over a
delta time step
Time Step
Linear and angular
accelerations
(time derivative of velocities)
Changes in object velocities
(linear and angular)
Integrated over a
delta time step
Rigid Body Simulation Update Cycle
Equations of Motion
Current State
Equations of
Motion
Position and velocity
Integrate
Forces
Integrate
Velocities
Accelerations
Integrate
Using Equations of Motion
• Standard Physics
– Analyzing for times at which significant events
happen (i.e. objects hitting the ground)
• Computer Animation
– Modeling the motion of objects at discrete
time steps
– The significant events and their aftermath
– Discrete time sampling  numerical issues!
Bodies in Free Fall
•
•
•
•
•
•
•
•
•
•
Basics
Example
Numeric Approximation
Equations of Motion
Orientation and Rotational Movement
Center of Mass
Forces
Momentum
Inertia Tensor
Equations
Bodies in Free Fall : Basics
Bodies in Free Fall
•
•
•
•
•
•
•
•
•
•
Basics
Example
Numeric Approximation
Equations of Motion
Orientation and Rotational Movement
Center of Mass
Forces
Momentum
Inertia Tensor
Equations
Time Step #1
Time Step #2
Time Step #3
Time Step #1
Time Step #2
Time Step #3
Time Step #1
Time Step #2
Time Step #3
Bodies in Free Fall : Example
Bodies in Free Fall : Example
Bodies in Free Fall : Example
Bodies in Free Fall : Example
Bodies in Free Fall
•
•
•
•
•
•
•
•
•
•
Basics
Example
Numeric Approximation
Equations of Motion
Orientation and Rotational Movement
Center of Mass
Forces
Momentum
Inertia Tensor
Equations
Numeric Approximation
Euler Integration
Numeric Approximation
Physically Based Modeling: Principles and Practice
(SIGGRAPH '97 Course notes)
©1997 Andrew Witkin and David Baraff
A Canonical Differential Equation
Midpoint Method
A.k.a. 2nd order Runga Kutta
Magnitude of the error term
F (ti+0.5)
F ‘ (ti)
F (ti)
F ’ (ti+0.5)
F (ti+1) = F (ti) + F ‘ (ti+0.5)*Dt
4th Order Runga Kutta
F (ti)
F (ti+1) = k1/6 + k2/3 + k3/3 + k4/6
Since x may be vectors, the solver must
know their length, to allocate
storage, perform vector arithmetic ops, etc.
The solver must be able to install new values at the
end of a step. In addition, a multi-step method must
set x and t to intermediate values in the course of
performing derivative evaluations.
Evaluate f at the current x and t.
Bodies in Free Fall
•
•
•
•
•
•
•
•
•
•
Basics
Example
Numeric Approximation
Equations of Motion
Orientation and Rotational Movement
Center of Mass
Forces
Momentum
Inertia Tensor
Equations
Equations of Motion
Linear force
Angular force: torque
Linear momentum
Angular momentum
Resistance to linear force: mass
Resistance to angular force: inertia tensor.
Bodies in Free Fall
•
•
•
•
•
•
•
•
•
•
Basics
Example
Numeric Approximation
Equations of Motion
Orientation and Rotational Movement
Center of Mass
Forces
Momentum
Inertia Tensor
Equations
Orientation and Rotational Movement
• Linear attributes:
– position,
– velocity,
– Acceleration
• 3D objects:
– orientation,
– angular velocity,
– angular acceleration
• Object orientation  rotation matrix R(t)
Position and Orientation
• A rigid body occupies a volume of space and
has a particular shape.
• Because a rigid body can undergo only rotation
and translation, we define the shape of a rigid
body in terms of a fixed and unchanging space
called body space.
• Given a geometric description of the body in
body space, we use x(t) and R(t) to transform
the body-space description into world space
Objects are defined in body space and transformed
by the position and orientation into world space
p(t )  R(t ) p 0  x(t )
The center of mass is transformed to the point x(t) in world space, at time t.
Center of mass  point in the
rigid body that lies at the
geometric center of the body.
x  Rt x, y  Rt  y, z  Rt z
pt   Rt  p0  xt 
Physical interpretation of the orientation matrix R(t).
At time t, the columns of R(t) are the world-space directions that
the body-space x, y, and z axes transform to.
Linear Velocity
• For simplicity, we’ll call x(t) and R(t)
the position and orientation of the
body at time t.
• How the position and orientation
change over time?
• If we imagine that the orientation of
the body is fixed, then the only
movement the body can undergo is
a pure translation.
• The quantity v(t) gives the velocity of
this translation.
vt   xt 
Velocity of the center of
mass in world space.
Linear Velocity
The change of the center of mass over time
x (t )  v (t )
For a pure translation ( R (t ) constant), all points
move with velocity v (t )
Angular Velocity
• In addition to translating, a rigid body can also
spin.
• Imagine however that we freeze the position of
the center of mass in space.
• Any movement of the points of the body must
therefore be due to the body spinning about
some axis that passes through the center of
mass. (Otherwise the center of mass would itself
be moving).
Angular Velocity :
Rate at which the object is rotating irrespective to its linear velocity
 t 
Direction  orientation of the axis about which the object is rotating
Magnitude  speed of the rotation in revolutions per unit of time
For a given number of rotations per unit of time,
the angular velocity is the same whether the
axis of rotation is near or far away.
Physical interpretation of the orientation matrix R(t).
At time t, the columns of R(t) are the world-space directions that
the body-space x, y, and z axes transform to.
Angular Velocity
The change in orientation over time
ω(t )
Encodes both the axis and speed of the rotation
 direction encodes the axis
 magnitude encodes the speed (rad/s)
But, how are ω(t ) and R (t ) related?
Let’s remind ourselves of the physical meaning of R(t)
Columns of Rt  
The directions of the transformed
x, y and z body axes at time t.
Describe the velocity with which
the x, y, and z axes are being
transformed.
Let’s examine how the change of an arbitrary vector
in a rigid body is related to the angular velocity
First in 2D  Linear Velocity from Angular Velocity
Perpendicular vector to r
V  r
B
OB

The velocity of a point (V) on a rotating
B

O
r
OB
Vector from the origin of our body O (i.e. center
of mass) to point B
body (B) is calculated by scaling
the perpendicular vector from the origin to
the point (rOB) by the angular velocity
(omega).
The velocity vector’s direction must be
perpendicular to the radius vector.
Intuitively: a point rotating around another
fixed point can only move perpendicularly
to the vector between the points at any
instant; it can’t move closer or farther
away, or the movement wouldn’t be a
simple rotation.
Angular Velocity
ω
For a given vector
r
The columns of R (t ) represent the transformed axes
e2
R. 2
R.1
e1
e3
R. 3
Now in 3D  The rate of change of a rotating vector.
The rate of change of a vector
At time t, we know that the
direction of the x axis of the rigid
body in world space is the first
column of R(t)
At time t, the derivative of the first column of R(t) is
just the rate of change of this vector: using the
cross product rule we just discovered, this change is
This means that we can write
Let’s simplify things:
If a and b are 3-vectors,
then a X b is the vector
Given the vector a, let us
define a* to be the matrix
Then
This construct can be derived by considering what’s known as
infinitesimal rotations. Refer to chapter 4.8 of
Herbert Goldstein. Classical Mechanics. Addison Wesley, Reading, MA, 1950.
for a more complete derivation of the a* matrix.
Using the * notation, we can rewrite
Mass of a Body
• We’ll need to (conceptually) perform some integrations
over the volume of our rigid body.
• Let’s imagine that a rigid body is made up of a large
number of small particles (indexed from 1 to N).
• The mass of the ith particle is mi
• Each particle has a (constant) location r0i in body space.
• The location of the ith particle in world space at time t:
Velocity of a Point (or Particle)
Since a point can be represented at any time by
r(t )  R(t ) r0  x(t )
Total velocity can then be expressed as
Which can be rewritten as
The velocity of the ith point (particle) of a rigid body in world space
Linear term
Angular term
Bodies in Free Fall
•
•
•
•
•
•
•
•
•
•
Basics
Example
Numeric Approximation
Equations of Motion
Orientation and Rotational Movement
Center of Mass
Forces
Momentum
Inertia Tensor
Equations
Mass distribution:
Typical approach  a mass value to each of the object’s vertices
Center of mass coordinate system  body space
x(t) = location of the center of mass at time t
Rigid Body Simulation
UpdateRigidBody(float time)
{
Force=ComputeTotalForce();
Torque=ComputeTotalTorque();
Momentum=Momentum+Force*time;
Velocity=Momentum/Mass;
Position=Position+Velocity*time;
AngMomentum=AngMomentum+Torque*time;
Matrix34 I=Matrix*RotInertia*Matrix.Inverse();
AngVelocity=I.Inverse()*AngMomentum;
Matrix.Rotate(AngVelocity*time);
}
Rigid Body Simulation
UpdateRigidBody(float time)
{
Force=ComputeTotalForce();
Torque=ComputeTotalTorque();
Momentum=Momentum+Force*time;
Velocity=Momentum/Mass;
Position=Position+Velocity*time;
AngMomentum=AngMomentum+Torque*time;
Matrix34 I=Matrix*RotInertia*Matrix.Inverse();
AngVelocity=I.Inverse()*AngMomentum;
Matrix.Rotate(AngVelocity*time);
}
Bodies in Free Fall
•
•
•
•
•
•
•
•
•
•
Basics
Example
Numeric Approximation
Equations of Motion
Orientation and Rotational Movement
Center of Mass
Forces
Momentum
Inertia Tensor
Equations
Force
We can apply forces to the object at any point
F2 (t )
F1 (t )
Total force on an object is simply
F(t )   Fi (t )
No information about where the forces are applied
Force acting on a rigid body due to some external influence
(e.g. gravity, wind, contact forces)
f  ma
a f m
F t    fi t 
Acceleration due to the earth's gravity can be approximated by a constant
acceleration, g, that is 32 f/sec/sec.
Given an object's initial position (i.e., height) and the assumption that it
was at rest at time t=0, then for any point in the future time we can plug the
time into the equations above to get its acceleration, velocity and distance
that it's fallen.
vt1   vt0   at1  t0 
pt1   pt0   vt0 t1  t0   1 2at1  t0 2
Gravity between two objects is a known force.
Given two objects with comparable masses,
M1 and M2, the force of gravity between two
masses is given by:
f = G*M1*M2/D
where
G = 6.67x10-11, a universal constant
D = distance between centers of mass
Gravity Animation
This animation shows motion produced by setting initial positions and velocities of
two bodies and then letting gravity do the rest.
object 1
mass: 1.0x10**13
radius: 60
initial position: 0 0 0
initial velocity: 0 0 0
object 2
mass: 1.0x10**11
radius: 30
initial position: 400 -200 0
initial velocity: -1.5 -0.125 0
500 time slices were computed with every tenth time slice displayed
(i.e., ten samples per frame).
The motion was produced by, for each time slice for both objects,
updating the force, computing acceleration, calculating average velocity,
updating position, and then updating velocity.
Simulation Loop
Check course’s
website for more
details, including other
simulation codes…
Rigid Body Simulation
UpdateRigidBody(float time)
{
Force=ComputeTotalForce();
Torque=ComputeTotalTorque();
Momentum=Momentum+Force*time;
Velocity=Momentum/Mass;
Position=Position+Velocity*time;
AngMomentum=AngMomentum+Torque*time;
Matrix34 I=Matrix*RotInertia*Matrix.Inverse();
AngVelocity=I.Inverse()*AngMomentum;
Matrix.Rotate(AngVelocity*time);
}
Torque
Torque describes the “rotational force”
τ i (t )  (ri (t )  x(t ))  Fi (t )
x(t )
F2 (t )
r1 (t )
F1 (t )
Total torque on an object is simply
τ(t )   τi (t )   (ri (t )  x(t ))  Fi (t )
Tells us about the force distribution over the object
Forces & Torques
• τ=dL/dt
• A torque is a change in angular
momentum (similar to a force which is a
change in linear momentum)
Offset Forces
• Torque resulting from offset force: τ=r×f
• Total force:
f cg   fi
• Total torque:
 cg   (ri  fi )
Rigid Body Simulation
UpdateRigidBody(float time)
{
Force=ComputeTotalForce();
Torque=ComputeTotalTorque();
Momentum=Momentum+Force*time;
Velocity=Momentum/Mass;
Position=Position+Velocity*time;
AngMomentum=AngMomentum+Torque*time;
Matrix34 I=Matrix*RotInertia*Matrix.Inverse();
AngVelocity=I.Inverse()*AngMomentum;
Matrix.Rotate(AngVelocity*time);
}
Bodies in Free Fall
•
•
•
•
•
•
•
•
•
•
Basics
Example
Numeric Approximation
Equations of Motion
Orientation and Rotational Movement
Center of Mass
Forces
Momentum
Inertia Tensor
Equations
Linear Momentum
Linear momentum of a particle is
pmv
Linear momentum of a rigid body is then
density
integration over the body
Linear Momentum
Linear momentum can be simplified as follows
Assuming constant mass gives
Angular Momentum
Angular momentum of a rigid body
L(t )  I (t ) ω(t )
inertia tensor
Taking the time derivative
 (t )  τ(t )
L
Angular momentum is conserved for no torque
Angular Momentum
• L=Iω = AI0A-1ω
L=angular momentum
I=rotational inertia
ω=angular velocity
A=3x3 orientation matrix
Rigid Body Simulation
UpdateRigidBody(float time)
{
Force=ComputeTotalForce();
Torque=ComputeTotalTorque();
Momentum=Momentum+Force*time;
Velocity=Momentum/Mass;
Position=Position+Velocity*time;
AngMomentum=AngMomentum+Torque*time;
Matrix34 I=Matrix*RotInertia*Matrix.Inverse();
AngVelocity=I.Inverse()*AngMomentum;
Matrix.Rotate(AngVelocity*time);
}
Bodies in Free Fall
•
•
•
•
•
•
•
•
•
•
Basics
Example
Numeric Approximation
Equations of Motion
Orientation and Rotational Movement
Center of Mass
Forces
Momentum
Inertia Tensor
Equations
Inertia Tensor
Describes how mass is distributed in the body
Analogous to mass in linear velocity
Measures the preferred axis of rotation
Expensive to compute this at every time step
Rotational Inertia
Bodies in Free Fall
•
•
•
•
•
•
•
•
•
•
Basics
Example
Numeric Approximation
Equations of Motion
Orientation and Rotational Movement
Center of Mass
Forces
Momentum
Inertia Tensor
Equations
Rigid Body Equations of Motion
State of the Object
(change over time)
 x(t ) 
R (t )

S (t )  
 P(t ) 


 L(t ) 
Position
Orientation
Linear Momentum
Angular Momentum
P(t )  M v(t )
L(t )  I (t ) ω(t )
Rigid Body Equations of Motion
Other Attributes
(do not change over time)
Mass
M   mi
Inertia Tensor
I object
 I xx

  I yx
 I zx

Time-varying Inertia Tensor
I xy
I yy
I zy
I xz 

I yz 
I zz 
I (t )  R(t ) I object R(t )
T
Angular Velocity
Linear Velocity
P (t )
v (t ) 
M
 (t )  I (t ) L(t )
1
Rigid Body Equations of Motion
Time Derivative of the Object’s State Vector
Combining the equations
 x(t ) 
 v (t ) 
R (t )
ω* R (t )
d
d 
  

S (t ) 
 F (t ) 
dt
dt  P(t ) 




 L(t ) 
 τ (t ) 
Discretize these continuous equations and integrate
Using Quaternions
Use quaternions to represent orientation
 cos( / 2) 
q(t )  

sin(  / 2) axis
The update rule is then
1
q (t )  ω(t ) q(t )
2
Rigid Body Equations of Motion
Using quaternions gives
 x(t ) 
 q(t ) 
d 

dt  P (t ) 


L(t )
 v (t ) 
 1 ω q(t )

 2
 F (t ) 


 τ (t ) 
Discretize these continuous equations and integrate
Simulation Algorithm
Algorithm with collisions and contact
current state
compute new state
next state
detect collisions and backtrack
collision state
compute and apply impulses
post-collision state
compute and apply constraint forces
Simulation Loop
Check course’s
website for more
details, including other
simulation codes…