University of Wales Trinity Saint David
ACCB5017 - Games Architecture and Mechanics

Kinematics

Overview

This worksheet turns the language of the Motion worksheet into equations that predict where an object will be and when it will get there. It covers rates of change, the constant acceleration equations, vectors in two dimensions, and projectile motion, and finishes by working out the gravity a game needs for a jump to feel right.

It closes with a short C++ console program that simulates a projectile one small time step at a time and checks the result against the exact answer.

Learning Objectives

By the end of this lab you will be able to:

Concept

In the Motion worksheet, velocity was the gradient of a position-time graph and displacement was the area under a velocity-time graph. Calculus gives those two ideas names. The gradient at a single instant is the derivative, written dx/dtdx/dt, and the area under a curve is the integral. You will not need to calculate any derivatives by hand here. You only need to read the notation as "the rate of change of x with respect to time".

When the acceleration is constant, the area under a velocity-time graph is a simple trapezium, and that one picture produces all five of the constant acceleration equations, often called the SUVAT equations after their symbols. Each equation leaves out one of the five quantities, so the trick to any problem is to list what you know and choose the equation that leaves out the one quantity you neither know nor need.

The most powerful idea in this worksheet is that perpendicular directions are independent. A ball thrown across a field moves horizontally at a constant velocity, because nothing pushes it sideways, while it accelerates vertically under gravity. Solving the two directions separately, then combining them at the end, turns any projectile problem into two one-dimensional problems that you already know how to solve.

Tasks

Task 1 - Rates of change

Hook

A speed camera works out your speed by timing you between two lines painted on the road. A speedometer shows your speed at this exact instant. How can a speed exist at a single instant, when speed is distance divided by time and no time passes in an instant?

Intuition

Start with an average over an interval, the slope of the straight line joining two points on a position-time graph. This line is called a secant. Now shrink the interval. The second point slides toward the first, and the secant swings round until it just touches the curve at a single point. That touching line is the tangent, and its slope is the instantaneous velocity.

Shrink the interval below and watch the two readings converge.

Formalise

The derivative is the limit of the average rate of change as the interval shrinks to zero. Velocity is the derivative of position, and acceleration is the derivative of velocity, which makes it the second derivative of position. Going the other way, the area under a velocity-time graph, the integral, gives displacement.

v=dxdt=lim⁡Δt→0ΔxΔta=dvdt=d2xdt2Δx=∫v dtv = \frac{dx}{dt} = \lim_{\Delta t \to 0} \frac{\Delta x}{\Delta t} \qquad a = \frac{dv}{dt} = \frac{d^2x}{dt^2} \qquad \Delta x = \int v\,dt

Worked example

The curve in the diagram is x(t)=0.4t2+0.5tx(t) = 0.4t^2 + 0.5t. At t=3 st = 3\text{ s}, compare the average velocity over the next 2.5 s, 0.1 s and 0.01 s.

  1. Over 2.5 s. x(5.5)−x(3)=14.85−5.1=9.75x(5.5) - x(3) = 14.85 - 5.1 = 9.75, so vˉ=9.75/2.5=3.9 m/s\bar{v} = 9.75 / 2.5 = 3.9\text{ m/s}.
  2. Over 0.1 s. x(3.1)−x(3)=5.394−5.1=0.294x(3.1) - x(3) = 5.394 - 5.1 = 0.294, so vˉ=2.94 m/s\bar{v} = 2.94\text{ m/s}.
  3. Over 0.01 s. vˉ=2.904 m/s\bar{v} = 2.904\text{ m/s}.

The averages settle on 2.9 m/s, the instantaneous velocity at 3 s.

Your turn

What does dvdt\dfrac{dv}{dt} represent?

It is the rate of change of velocity with respect to time, which is acceleration.

On a velocity-time graph, what does the integral, the area under the curve, give?

Adding up velocity times small time intervals gives the total change in position.

On a position-time graph, the tangent at a point is horizontal. What is the object doing at that instant?

A horizontal tangent has zero slope, so the instantaneous velocity is zero, like a ball at the top of its flight.

Explain in words what d2xdt2\dfrac{d^2x}{dt^2} means and give its SI unit.

It is the second derivative of position, the rate of change of the rate of change of position. In other words, how quickly velocity changes, which is acceleration. Its unit is metres per second squared.

Task 2 - The constant acceleration equations

Hook

A car brakes from 25 m/s with a constant deceleration. How far does it travel before it stops? You know the start speed, the end speed and the acceleration, but not the time. Is there an equation that skips the time completely?

Intuition

With constant acceleration, a velocity-time graph is a straight line from the initial velocity uu to the final velocity vv over a time tt. Its gradient is aa. The area underneath, a trapezium, is the displacement ss. Writing down the gradient and the area in different ways gives five equations linking the five quantities s,u,v,a,ts, u, v, a, t.

EquationLeaves out
v=u+atv = u + atss
s=12(u+v)ts = \tfrac{1}{2}(u + v)taa
s=ut+12at2s = ut + \tfrac{1}{2}at^2vv
s=vt−12at2s = vt - \tfrac{1}{2}at^2uu
v2=u2+2asv^2 = u^2 + 2astt

Formalise

The gradient of the line gives the first equation, and the area of the trapezium gives the second. Substituting one into the other produces the rest.

v=u+ats=12(u+v)ts=ut+12at2v2=u2+2asv = u + at \qquad s = \tfrac{1}{2}(u + v)t \qquad s = ut + \tfrac{1}{2}at^2 \qquad v^2 = u^2 + 2as

Worked example 1

The braking car. u=25 m/su = 25\text{ m/s}, v=0v = 0, a=−5 m/s2a = -5\text{ m/s}^2. Find the stopping distance and the time.

  1. Distance, which needs no time. 0=252+2(−5)s0 = 25^2 + 2(-5)s, so s=625/10=62.5 ms = 625 / 10 = 62.5\text{ m}.
  2. Time. 0=25+(−5)t0 = 25 + (-5)t, so t=5 st = 5\text{ s}.

Worked example 2

A ball is dropped from rest 20 m above the ground. Take down as positive, so a=+9.81 m/s2a = +9.81\text{ m/s}^2.

  1. Time. 20=0+12(9.81)t220 = 0 + \tfrac{1}{2}(9.81)t^2, so t=40/9.81=2.02 st = \sqrt{40 / 9.81} = 2.02\text{ s}.
  2. Landing speed. v2=0+2(9.81)(20)=392.4v^2 = 0 + 2(9.81)(20) = 392.4, so v=19.8 m/sv = 19.8\text{ m/s}.

Your turn

You know uu, aa and tt and want ss. Which equation?

It contains exactly the three knowns and ss, and leaves out vv.

You know uu, vv and ss and want aa. Which equation?

It is the only one without tt, which you do not know.

You know uu, vv and tt and want ss. Which equation?

It uses the average of the start and end velocities and leaves out aa.

When are the SUVAT equations valid?

They come from a straight-line velocity-time graph, which only happens when acceleration is constant.

A car accelerates from 10 m/s at 2 m/s² for 5 s. Its final speed is m/s and it travels m.

A ball is thrown straight up at 15 m/s. It reaches a maximum height of m after s, both to 3 significant figures.

Task 3 - Vectors in two dimensions

Hook

A boat points straight across a river and motors at 4 m/s. The river flows sideways at 3 m/s. How fast is the boat actually moving, and in which direction?

Intuition

Any vector in a flat plane can be split into two perpendicular components, one along x and one along y. The vector is the diagonal of the rectangle they form. Splitting a vector is called resolving it, and it is what makes two-dimensional problems manageable. To add two vectors, either place them tip to tail, or simply add their x components and their y components separately.

Drag the tip of vector A below. Then switch to "Add a second vector" and drag the tip of B.

Formalise

For a vector of size vv at angle θ\theta above the positive x axis.

vx=vcos⁡θvy=vsin⁡θ∣v∣=vx2+vy2θ=tan⁡−1 ⁣(vyvx)v_x = v\cos\theta \qquad v_y = v\sin\theta \qquad |v| = \sqrt{v_x^2 + v_y^2} \qquad \theta = \tan^{-1}\!\left(\frac{v_y}{v_x}\right)

Worked example

The boat. Take x as downstream and y as straight across.

  1. Components. vx=3 m/sv_x = 3\text{ m/s}, vy=4 m/sv_y = 4\text{ m/s}.
  2. Size. 32+42=5 m/s\sqrt{3^2 + 4^2} = 5\text{ m/s}.
  3. Direction. tan⁡−1(4/3)=53.1∘\tan^{-1}(4/3) = 53.1^\circ from the bank, which is 36.9∘36.9^\circ downstream of straight across.

Your turn

Give angles in degrees from the positive x axis.

A velocity of 10 m/s at 30° has components vx = m/s and vy = m/s, to 3 significant figures.

A vector with components (6, 8) has magnitude and direction °, to 3 significant figures.

Adding the vectors (2, 5) and (4, -1) gives (, ).

A force of 50 N at 150° has an x component of N, to 3 significant figures.

A vector points down and to the left. What are the signs of its components?

Left is negative x and down is negative y.

A calculator gives tan⁻¹(4 / -3) = -53.1°. The vector is (-3, 4). What is its true direction?

The vector points up and left, in the second quadrant. Add 180° to the calculator answer. Code avoids this trap by using atan2(y, x).

Task 4 - Projectile motion

Hook

At the same moment, one ball is fired horizontally from a table and another is simply dropped from the same height. Which lands first?

Intuition

They land together. Gravity acts only vertically, so it changes only the vertical velocity. Both balls start with zero vertical velocity and gain it at the same rate, so they fall the same distance in the same time. The fired ball also moves sideways, at a constant horizontal velocity, but that has no effect on how quickly it falls.

Fire the launcher below. The teal arrows show the horizontal velocity at regular times, and they never change. The red arrows show the vertical velocity, which shrinks to zero at the top and then grows downward. Try angles of 30° and 60°, and then find the angle that gives the longest range.

Formalise

Split the launch velocity uu at angle θ\theta into ux=ucos⁡θu_x = u\cos\theta and uy=usin⁡θu_y = u\sin\theta. Horizontally there is no acceleration. Vertically the acceleration is −g-g. For a launch and landing at the same height, setting y=0y = 0 gives the flight time, and the other results follow.

x=uxty=uyt−12gt2T=2uygR=u2sin⁡2θgH=uy22gx = u_x t \qquad y = u_y t - \tfrac{1}{2}gt^2 \qquad T = \frac{2u_y}{g} \qquad R = \frac{u^2 \sin 2\theta}{g} \qquad H = \frac{u_y^2}{2g}

Worked example

A ball is launched at 20 m/s at 30° above level ground.

  1. Components. ux=20cos⁡30∘=17.32 m/su_x = 20\cos 30^\circ = 17.32\text{ m/s}, uy=20sin⁡30∘=10 m/su_y = 20\sin 30^\circ = 10\text{ m/s}.
  2. Flight time. T=2×10/9.81=2.04 sT = 2 \times 10 / 9.81 = 2.04\text{ s}.
  3. Range. R=17.32×2.04=35.3 mR = 17.32 \times 2.04 = 35.3\text{ m}.
  4. Maximum height. H=102/(2×9.81)=5.10 mH = 10^2 / (2 \times 9.81) = 5.10\text{ m}.

Because sin⁡2θ\sin 2\theta is largest at 2θ=90∘2\theta = 90^\circ, the range on level ground is greatest at 45°. Angles that add to 90°, such as 30° and 60°, give the same range.

Your turn

In the hook, which ball lands first?

Both have zero initial vertical velocity and the same vertical acceleration, so they take the same time to fall.

What happens to the horizontal velocity of a projectile during its flight, ignoring air resistance?

No horizontal force acts, so the horizontal velocity never changes.

At the highest point of its flight, what is a projectile's acceleration?

Only the vertical velocity is zero at the top. Gravity still acts, so the acceleration is g downward throughout.

A ball is thrown horizontally at 12 m/s from the top of a 20 m cliff. It takes s to land and lands m from the foot of the cliff, both to 3 significant figures.

Just before landing, its vertical speed is m/s and its total speed is m/s, both to 3 significant figures.

Task 5 - Designing a jump

Hook

A level designer wants the player character to jump exactly 3 m high, the height of three tiles, and to reach the top of the jump in 0.4 s so it feels snappy. What value of gravity, and what launch speed, does the game need?

Intuition

This is the SUVAT problem run backwards. Designers choose how high and how fast the jump should feel, and the physics then tells them what gravity and launch velocity to put in the code. At the top of the jump the vertical velocity is zero, which gives the extra piece of information needed.

Formalise

Taking up as positive, with v=0v = 0 at the apex, s=hs = h and a=−ga = -g, the equations v=u+atv = u + at and s=vt−12at2s = vt - \tfrac{1}{2}at^2 give the following.

g=2htapex2v0=2htapexg = \frac{2h}{t_{apex}^2} \qquad v_0 = \frac{2h}{t_{apex}}

Worked example

With h=3 mh = 3\text{ m} and tapex=0.4 st_{apex} = 0.4\text{ s}.

  1. Gravity. g=2×3/0.42=37.5 m/s2g = 2 \times 3 / 0.4^2 = 37.5\text{ m/s}^2.
  2. Launch speed. v0=2×3/0.4=15 m/sv_0 = 2 \times 3 / 0.4 = 15\text{ m/s}.

That gravity is almost four times Earth's. Real people jumping 3 m would hang in the air for much longer, which feels floaty and slow in a game. Many platformers go further and use a larger gravity on the way down than on the way up, and cut the upward velocity when the jump button is released, so a tap gives a short hop and a hold gives a full jump.

Your turn

A jump should reach 4 m in 0.5 s. The game needs g = m/s² and a launch speed of m/s.

With that gravity and launch speed, the whole jump from take-off back to the ground lasts s.

In a game engine where screen y increases downward, how do the signs of gravity and the jump velocity change compared with the worked example?

Down is positive, so gravity becomes a positive acceleration of plus g and the jump launch velocity becomes negative, minus v0, because the character moves up the screen, which is the negative y direction. The sizes are the same and only the signs flip.

Task 6 - Simulating a projectile frame by frame

A game does not use the projectile equations directly. It steps the motion forward in small time steps, the same frame-by-frame update from the Motion worksheet, now with two components. In this task you write that loop in C++ and compare it with the exact answer from Task 4.

Create a new Console App (C++) project in Visual Studio named Projectile, and replace the contents of the generated source file with the skeleton below, saved as Projectile.cpp. It uses only the standard library.

  1. Inside the loop, add acceleration scaled by dt to velocity, for both components.
  2. Then add the new velocity scaled by dt to position, for both components. Updating velocity before position is called semi-implicit Euler.
  3. After t and step are advanced, break out of the loop if position.y has dropped below zero.
  4. After the loop, calculate the analytic flight time and range using the formulas from Task 4 and print them next to the simulated values. You will need to keep a copy of the launch velocity before the loop changes it.

Success condition: with dt = 0.001 the program prints a table of positions every 0.1 s, and the simulated range is within 0.05 m of the analytic 35.3 m. Then change dt to 0.1, run it again, and record how far the range is off.

Projectile.cpp
#include <cmath>
#include <iomanip>
#include <iostream>

struct Vec2
{
    double x;
    double y;
};

int main()
{
    const double pi = 3.14159265358979;
    const double g = 9.81;
    const double speed = 20.0;
    const double angleDegrees = 30.0;
    const double dt = 0.001;

    const double angle = angleDegrees * pi / 180.0;
    Vec2 position{ 0.0, 0.0 };
    Vec2 velocity{ speed * std::cos(angle), speed * std::sin(angle) };
    const Vec2 acceleration{ 0.0, -g };

    const int printEvery = static_cast<int>(std::round(0.1 / dt));
    int step = 0;
    double t = 0.0;

    std::cout << std::fixed << std::setprecision(3);
    std::cout << "t (s)     x (m)     y (m)\n";

    while (t < 10.0)
    {
        if (step % printEvery == 0)
        {
            std::cout << t << "     " << position.x << "    " << position.y << "\n";
        }

        // TODO: add acceleration scaled by dt to velocity, both components

        // TODO: add velocity scaled by dt to position, both components

        t += dt;
        step++;

        // TODO: stop the loop once the projectile has dropped below y = 0
    }

    // TODO: calculate the analytic flight time T = 2 * uy / g and range
    // R = ux * T from the launch velocity, then print them next to the
    // simulated landing time t and landing distance position.x

    return 0;
}

Why does the larger time step of 0.1 s give a less accurate landing point than 0.001 s?

Each step treats the velocity as constant for the whole step, but gravity is really changing it continuously. With a large step that approximation is held for longer, so the error per step is bigger and the path drifts further from the true parabola. The loop also only checks for landing at the end of each step, so the landing can be detected up to a whole step late or early.

Solution

Try the tasks above before revealing the solution.