] > Lecture: Game Physics

Lecture: Game Physics

Note: this web page uses MathML, mathematics markup for the web. We have installed mathjax which should render the MathML, but if the maths rendering looks broken, try Firefox, as it has strong MathML support. Chrome/Chromium support is being introduced.

Mechanics, Statics and Dynamics

The behaviour of objects subject to forces has long been studied in physics, in the area known as mechanics. Within mechanics are sub-areas of statics, where objects are fixed, and dynamics where objects move.

Dynamics is the most relevant to games and graphics.

There are two broad categories of motion studied in dynamics: linear motion and rotational motion.

Laws of Motion

Dynamics includes the laws of motion. These are physical laws which describe how the universe works.

Newton's Laws of Motion

Newton's laws of motion are the foundation for much of mechanics, from [1]:

Constant Velocity

By Newtons first law when there is no force, i.e. F=0 the velocity remains constant. Assuming an object is moving in the x direction:

vx0 = dx dt

We can solve for x by integrating both sides of the equation:

x (t) = vx0t+x0

where vx0 is the initial velocity in the x direction.

This equation allows the calculation of x for any value of t, i.e. xt ,

Constant Acceleration

Constant acceleration is a special case which occurs when a force is constant, and assuming mass is constant, Newton's second law says acceleration is constant.

One situation in which constant acceleration occurs is objects close to the earth's surface subject falling due to gravity, which all undergo the same acceleration in the y direction:

a = d2y dt2 = -g

To solve, integrate twice to get

y (t) = - 12 g t2 + vy0 t + y0

where vy0 and y0 are the initial velocity and position in the y direction respectively.

This equation allows the calculation of y for any value of t, i.e. yt

Projectile Motion

Projectile motion is the type of motion a projectile, such as a cannon ball, undertakes when subject to the force of gravity. Projectile motion has the shape of a parabola

y=x^{2}.svg

Because gravity acts only in the vertical direction, and (it is assumed that) there are no forces in the horizontal direction, the only acceleration is in the y direction.

Thus projectile motion can be separated into constant acceleration in the y direction, as above, and constant velocity in the x direction, by Newton's first law.

The parametric equations for projectile motion are then just those above:

x (t) = vx0t+x0 y (t) = - 12 g t2 + vy0 t + y0

Often the symbol r (t) = r ( x(t) , y(t ) ) is used for the position, or position vector, of a point given by parametric equations.

The initial velocities in the x and y directions are known if the initial velocity vector is in kept in component form, i.e. v0 = < vx0 , vy0 > or can be calculated using the cos and sin of the angle the initial velocity vector makes with the x axis vx0= v0cosθ and vy0= v0sinθ

The time of flight of a projectile can be calculated by working out the time at which the projectile hits the ground. For example, the simplest case is where a projectile is fired from and hits flat ground at y=0

y (t) = - 12 g t2 + vy0 t + y0 0 = - 12 g t2 + vy0 t = t ( - 12 g t + vy0 ) t = 2 vy0 g

Equations of Motion

Equations like the ones above which describe how an object moves, including differential equations, initial positions and velocities, are called equations of motion. Then we solve the equations of motion to give position as a function of time of the objects involved. The position of objects over time is usually the most important aspect of the solution.

Solving Equations of Motion

Differential equations, such as the one above, are solved by integration. There are two main approaches: analytical and numerical. Some cases, such as projectile motion above, can be solved analytically. But in many or even most cases numerical integration must be used, as analytical solution is not possible. One example is particles which interact with each by forces such as gravity, or in general where ever there are collisions. Most physics in games uses numerical integration.

Numerical Integration

Numerical integration amounts to working out small changes, or deltas, in one variable given small changes in another - usually time if objects are moving. Approximate estimated values can be found by linearization, or more simply linear interpolation using gradients, and remembering that gradients are derivatives.

For example, given velocity v and a small change in time dt a new position can be found:

xn+1 = xn + vn ×dt

Similarly, given acceleration a and dt a new velocity can be be found as:

vn+1 = vn + an ×dt

Putting both of those together, and skipping over many issues about accuracy, we get:

xn+1 = xn + vn ×dt vn+1 = vn + an ×dt

Notice that position is updated first, and then velocity, although other techniques may update variables in different orders.

Also notice that for the case of constant acceleration, where the acceleration never changes, can simplify as:

an = an+1 = a

Euler Integration

The above numerial approach is called Euler integration. It is simple, but fairly inaccurate.

Other Numerical Integration Techniques

More accuracy can be obtained by different ways of estimating values. For example, a better estimate of velocity over the time interval t, t+dt might be at the midpoint:

vn+1 = vn + an × dt 2

This approach is known as the improved Euler method, and also as the midpoint method.

The velocity Verlet integration approach incorporates an acceleration term when updating the position, and uses the average of the acceleration at the start and at the end of the time step to update velocity:

xn+1 = xn + vn ×dt + 12 an × dt2 vn+1 = vn + ( an + an+1 ) 2 × dt

Another approach is to use not linear interpolation, but quadratic or cubic interpolation - using not just 2 values, but 3 or 4.

There is a very extensive body of knowledge about numerical methods, which are particularly important in physics simulations. Game physics are increasingly using the same techniques, however accuracy is usually not as important and for example may be traded for performance.

Example Program: Projectile Motion

A simple C program for projectile motion which calculates motion both analytically and numerically

Block on a Ramp/Inclined Plane

Often in games objects slide on surfaces. A basis for computing their motion can be found in the block on a ramp or inclined plane problem:

ramp.png

Assume the only force acting is gravity, and there is no friction. The force due to gravity, usually known as weight, is F = W = mg.

The weight force can be resolved into vectors perpendicular ⟂ and parallel ∥ to the plane:

F=Wcosθ F=Wsinθ

The block sits and slides on the surface of the plane, but does not fall through. By Newton's third law equal and opposite forces in the direction normal i.e. perpendicular to the ramp cancel each other out - the force of the block on the plane in the direction of the normal due to gravity, and the equal and opposite reaction or normal force FN that the plane exerts on the block:

FN = F

So the net or resultant force acting on the block is F and by Newton's second law gives F = ma

In order to numerically integrate the motion need the accelerations ax and ay in the x and y directions respectively. They can be obtained by resolving the (resultant) force in the x and y directions

F∥x=Wsinθcosθ=max F∥y=Wsinθsinθ=may

Drag

Objects moving in a fluid or gas encounter a drag force, which resists, i.e. acts opposite to the motion. Typically the drag force increases with an object's speed. A simple model for drag uses the square of the velocity and a coefficent of drag:

Fdrag= 12 cd v2

Friction

Objects in contact encounter friction which resists sliding or relative motion. There are two types: static and dynamic or kinetic. Static friction occurs before objects start to slide, after which dynamic friction occurs.

A simple model of dynamic friction uses a coefficent of drag which increases with how much force is being applied between the sliding surfaces, i.e. the normal force:

Fdf= μ FN

For objects which are smooth friction can be quite low, and as their relative speed increases drag is usually the more important force.

Example Program: Block on a Ramp

A simple example C program for a block on a ramp.

GPU Physics

One way which is being pursued to achieve both higher performance and more accuracy is to use the GPU to perform physics calculations.

Using the GPU to perform non-graphics calculations is referred to as GPU computing. GPUs are being used for biology, cryptography, weather simulation etc.

References

[1] Wikipedia: Newton's law