Today: A modelling example#

Tom Ranner

A model for the trajectory of an object#

../_images/dd2449aa340cf0e2252c64da1a4ca61b2bfa277d13346b8527c7d24d24a810f0.png

Newton’s laws of motion#

  • Important in many computer games is the physics engine

  • This is often one of the computationally expensive parts of running a game

  • Models often derived by working out the forces on an object and applying Newton’s laws!

Force = mass \(\times\) acceleration

Newton’s 2nd law of motion states that the accerlation of an object in each direction is equal to the applied force in that direction divided by the mass of the object.

-> once we know the acceleration of the object, we can solve the differential equation to calculate the objects velocity and position…

Example - a projectile#

Consider an object being projected from the top of a tall building

  • We know its initial postion and speed in each direction and wish to predict is trajectory!

../_images/dd2449aa340cf0e2252c64da1a4ca61b2bfa277d13346b8527c7d24d24a810f0.png

Project example - the model#

We consider a similar model to the freefall example from lecture 12.

The important forces are:

  • gravity acts in the negative \(y\) direction at a constant acceleration \(g = 9.81 \, \mathrm{m} / \mathrm{s}^2\)

  • air resistance acts in the opposite direction to way the projectile is moving - in both the \(x\) and \(y\) directions. We assume the size of the force is proportional to the speed is each direction

Projectile example - equations#

Using the following notation

  • \(X(t)\) is the horizontal distance of the object at time \(t\)

  • \(Y(t)\) is the vertical distance of the object at time \(t\)

  • \(U(t)\) is the horizontal speed of the object at time \(t\)

  • \(V(t)\) is the vertical speed of the object at time \(t\)

The above model leads to the following two differential equations:

\[\begin{split} \begin{aligned} U'(t) & = -k U(t) \\ V'(t) & = -k V(t) - g. \end{aligned} \end{split}\]

We also know, from the definition of speed, that

\[\begin{split} \begin{aligned} X'(t) & = U(t) \\ Y'(t) & = V(t). \end{aligned} \end{split}\]

Projectile example - initial conditions#

In addition to the 4 differential equations, we need 4 starting values.

For example at \(t = 0\),

\[\begin{split} \begin{aligned} U & = 20 \\ V & = 0 \\ X & = 0 \\ Y & = 100 \\ \end{aligned} \end{split}\]

(We’re not going to write down the units any more but they are still there!)

Projectile example - results#

../_images/e48ff6294127a4a058e53db9bd5cee0a2daf8b1532c1d77b4e3ccd5cb74bb54d.png

Euler’s method for systems of equations#

In general, a system of four differential equations will take the form:

\[\begin{split} \begin{aligned} Y_1'(t) & = F_1(t, Y_1, Y_2, Y_3, Y_4) \\ Y_2'(t) & = F_2(t, Y_1, Y_2, Y_3, Y_4) \\ Y_3'(t) & = F_3(t, Y_1, Y_2, Y_3, Y_4) \\ Y_4'(t) & = F_4(t, Y_1, Y_2, Y_3, Y_4). \end{aligned} \end{split}\]
  • How does this relate to the projectile example above?

  • It is possible to apply Euler’s method or the midpoint method to apply Euler’s method or the midpoint method to a general system such as this

Solution using Euler’s method#

Consider the following system of differential equations:

\[\begin{split} \vec{y}'(t) = A \vec{y}(t), \quad \text{ subject to } \quad \vec{y}(0) = \begin{pmatrix} 0 \\ 1 \end{pmatrix}, \end{split}\]

where \(\vec{y}(t) = \begin{pmatrix} y_1(t) \\ y_2(t) \end{pmatrix}\) and \(A = \begin{pmatrix} -1 & 1 \\ 1 & -2 \end{pmatrix}\).

  • Approximate the solution using 2 steps of Euler’s method with \(\mathrm{d}t = 0.5\).

The Python version#

Using Python notation each time step would be given by

y1[i + 1] = y1[i] + 0.5 * (-y1[i] + y2[i])
y2[i + 1] = y2[i] + 0.5 * (y1[i] - 2 * y2[i])
t[i + 1] = t[i] + 0.5

or

y[i + 1, :] = y[i, :] + 0.5 * A @ y[i, :]
t[i + 1] = t[i] + 0.5

Python results#

../_images/624b74ad197c345ac5c963d97683a281cba303f5c838f9f21b8fe2f112b52c28.png

What about errors?#

We can use the Euclidean norm as before to calculate the error against an exact solution:

\[ \text{Abs. error} = \| \vec{y}_\text{exact} - \vec{y}(T) \| \]
Results of using Euler's method on a system of equtaions
dt abs. error ratio
0.500000 0.017606 ---
0.250000 0.008555 0.485941
0.125000 0.004219 0.493091
0.062500 0.002095 0.496579
0.031250 0.001044 0.498299

Summary#

  • Many useful models take the form of a single differential equation.

  • However, many important models require systems of coupled differential equations to be solved.

  • In the latter case we may generalise the standard techniques, such as Euler’s method or the midpoint method, to get effective computational models.

  • Only 2 computational schemes have been introduced here (Euler and midpoint) - there are many more that we haven’t considered…