modelling : physics II

# multiple body interaction

Whenever one body hits another we can apply Newton's laws to get an accurate and realistic behaviour
of these bodies.

We have to find the force equations to simulate specific and different physical behaviours. This means
that each interaction of two or more bodies rely on a fundamental physical law. To keep things simple,
we always will use classical mechanics.

In the next sections I will discuss some of the fundamental laws.

# gravitation

The force-equation for gravitation is as follows:

Def.: Given two objects with mass *m*_{1} and *m*_{2} at distance **r**.
Object one will exert a force **F**_{12} on object two and object two
will exert an equal but opposite force **F**_{21} on object one (3rd Newton's law).

(1.1)

*F* = *G**m*_{1}*m*_{2}/r^{2},

(1.2)

**F**_{12} = -**F**_{21} = *G**m*_{1}*m*_{2}**r**/r^{3},

where

*G* is the gravitational constant, that is approximately
*G* = 6.67 × 10^{-11} N m^{2} kg^{-2},
- and
*r* is the absolute value of **r**.

The absolute value of the forces **F**_{12} and **F**_{21} is *F* and described by equation 1.1.
The vector value of the forces are described by equation 1.2, the *r*^{3} part comes from the fact that
the vector **r** is not an unit vector by default.

The law of gravitation and Newton's 3rd law describe the behaviour of objects on earth. If we jump we will be accelerated towards
earth due to gravitation. If we rest on the surface, we will exert a force on earth and earth will respond with the same but
opposite force. So we do not fall into earth's core. That is the fact why things are heavy.

With this simple equation we can simulate a planet surrounded by a moon, or a solar system, or even
a galaxy with millions of stars.

What we need are the starting conditions (position, velocity, and mass of each object) and than we can iterate through
the process. In each step we compute the net force on each object and than apply the acceleration to each object.

In pseudo code:

```
struct Object { vector pos, vel, acc; float mass; };
```

Object objects[num];

vector F, r;

do

for each obj in objects do obj.acc = 0; end;

for i = 1..num-1, Object &a = object[i]; do

for j = i..num, Object &b = object[j]; do

r = b.pos-a.pos;

F = G*a.mass*b.mass*r/length(r*r*r);

a.acc += F/a.mass;

b.acc -= F/b.mass;

end;

end;

for each obj in objects do

obj.vel += obj.acc * dt;

obj.pos += obj.vel * dt;

end;

end;

# gravitation - pitfalls

When choosing the wrong iteration step time *dt* the simulation will mess up. *dt* depends on various
parameters, like number of objects, strength of force, mass of objects, and so forth.

You have to tune the parameters to get a good result, but do not overtune. To short time intervals will slow
down the simulation, to long time intervals will lead to wrong computations.

The shown code uses Euler method. There are other iteration schemes like Runge-Cutta which are more accurate.
There are so many articles about Runge-Cutta iteration, so search around and find the most fitting for you.

# springs

Springs are a fascination thing and in computer graphics a fundamental tool to simulate flags, clothing, and water effects.
The force equation of a spring is simple.

Def.: Given a spring with stiffness *C* and a object attached to the spring with mass *m*:

(2.1)

*F* = *C*(*x*-*l*),

(2.2)

**F** = *C*(**x**- (*l***x**/*x*)).

where

*C* is the spring constant with unit [kg s^{-2}],
*l* is the rest length of the spring, and
*x* is the absolute value of **x**.

Equation 2.1 is the force equation with its absolute value and equation 2.2 the vector equation of a spring.

Code to simulate a spring with a mass object hanging on it:

```
vector pos,vel,acc;
```

float mass;

vector F;

do

F = C*(pos - l*pos/length(pos));

acc += F/mass;

vel += acc*dt;

pos += vel*dt;

end;

Springs are good for many topics like

- Car simulation to get accurate suspension reactions,
- Cloth simulation. Connect springs over a 2d mesh and you get a very pleasent tissue.
Search the web for
**cloth simulation [verlet integration]** to see how this works,
or have a look at hugo elias' website.
- Water surface waves. Use a cloth simulation and reduce damping as good as possible and
you will get a wavy water surface.

# springs - pitfalls

But springs have their pitfalls, too. Let's take equation 2.1 and feed it back in our position equation.

(3.1)

*F* = *C*(*x*-*l*),

(3.2)

d^{2}*x*/dt^{2} = *F*/*m* = *C*/*m* (*x*-*l*).

This differential equation leads to an oscillating system with a frequency of

(3.3)

*f* = √(*C*/*m*).

As we see directly from the last equation if we increase the value of the spring's stiffness *C*, the frequency is increased.
On the other hand, if we increase the weight of the object hanging on the spring, frequency is decreased.

If we choose huge time steps *dt* than the spring simulation overacts with very huge forces resulting in a simulation
where the mass bodie will diverge from its resting position into infinity.

Here the Nyquist criterion plays an important role. The lowest iteration frequency is twice the spring's frequency. Thus the
time steps must meet the criterion of *dt* ≤ 0.5 × √(*m*/*C*).

# momentum and energy

... to be continued ...

# the real truth - damping

In reality nothing will run for ever. A spring system will not oscillate endlessly and a car will not roll on forever.

To simulate damping we just have to add damping forces.
We compute and sum all forces that acting on an object including friction forces.

There are different kinds of friction and resistance

- static friction occurs when two objects are not moving relative to each other (pen on a book).
- dynamic friction occurs when two objects are moving relative to each other and rub together.
- rolling friction occurs when two objects are moving relative to each other and one is rolling on the other.
- air friction, wind resistance occurs when one object is moving in a viscous aether.

(4.1)

**F**_{f} = *μ*_{f} **F**_{p},

(4.2)

**F**_{air} = -*c***v**.

(4.3)

**F**_{air} = -*c*_{1}**v** -*c*_{2}**v**^{2} -... .

Equations 4.1 is the formula for static, dynamic and rolling friction.
Where **F**_{f} is the resulting friction force, *μ*_{f} is the coefficient of friction, and
**F**_{p} is the normal force, i.e. the force perpendicular to the contact surface.

Equations 4.2 and 4.3 is the formula for air friction.
Where **F**_{air} is the resulting friction force, *c* is the resistance coefficient, and
**v** is the velocity of the object moving through the gas or liquid. Equation 4.2 is a simple model of an air friction.
If speed is increased higher terms of velocity begin to play a role in friction, thus you can add **v**^{n} with
decreasing resistance coefficients *c*_{n}, see equation 4.3.

The resistance coefficient *c* can contain the object's geometry or not, depending on the model's complexity.

For beginners the different types of friction forces are not always clear and some mistake *dynamic friction* with
*rolling friction*.

As an example, i will take a pen on a book to make the difference of *static and dynamic friction* more clear.
When a pen rest on a book, *static friction* inhibits the pen from moving.
You can hold the book at varying angles without moving the pen.
But after a certain angle is reached the pen will begin to move and *static friction* fade to *dynamic friction*.
*Static friction* is the force that keeps a car on its track when turning, breaking, or accelerating.
If the forces that occure during acceleration, breaking, or turning exeeds a specific amount, the car will leave the
driver's intended track and go off.

If we accelerate too much, break to hard, or make a turn to tight, *static friction* can no longer resist the occuring
forces and fade to *dynamic friction*.
During acceleration we recognize this as a wheelspin.
When breaking or turning to tight the car becomes no longer controlable and moves on in a straight line.

*Rolling friction* is the force acting when on object is rolling on another one.
A simple example with a car.
If you steer your car on a moody ground and you do not accelerate or break, the car's mass will keep on moving.
But the mud will stop the car from moving endlessly.
This force that acts again a rolling object is *rolling friction*.
The *rolling friction* of concrete is much less than that of mud or sand.
Thus the more softer the objects are the higher the rolling friction is.
So, wheels and a road of steel (railway) has the tiniest *rolling friction*.

*Air friction* is another kind of friction and occurs whenever an object is moving through gas or liquids of any kind.
*Air friction* depends on the speed of the object and its geometry.
The simplest formula of air friction disregards geometry.