Skip to content

Latest commit

 

History

History
475 lines (323 loc) · 13.6 KB

lecture05.adoc

File metadata and controls

475 lines (323 loc) · 13.6 KB

Simulation

Overview

  • Where to put assets

  • Simulation

Where to put assets

  • Your game will contain a number of non-code assets

  • Your program will need to access these at run-time

Where to put assets

  • Where does an executable look for external files?

    • e.g. for "Sdl-logo.bmp"??

  • ???

Where to put assets 2

  • Where does an executable look for external files?

    • e.g. for "Sdl-logo.bmp"??

  • In the "Working Directory"

    • wherever that is?

    • where the executable was loaded from

    • not, necessarily where the executable is !!!! [frown o]

Where to put assets 3

  • The "Working Directory" is OUT OF YOUR CONTROL

    • your user/player could start your program from anywhere

    • for example, they could start it like this, from C:\Windows

C:\Windows\>C:\Users\aUser\gamesProgramming\build\bin\myAwesomeGame.exe
Working Directory

C:\Windows\

Exe Location

C:\Users\aUser\gamesProgramming\build\bin\myAwesomeGame.exe

Visual Studio sets the working directory, to somewhere!!

  • Where to??

Visual Studio sets the working directory, to somewhere!! 2

Where to put assets

Getting the location of your executable

  • As the working directory is out of your control, you should look for files relative to your executable

  • argv[0] is frequently the full path to your executable, but not always

  • SDL has a function to get the path of your executalbe

  • You could use this to make sure your program stills find its assets even when run from other working directories

Simulation

  • For our games to do anything, we need to change its state

  • Unlike many other systems, Games tend to change their state over time

    • even when there is no human interaction

    • e.g. a ball keeps falling

What is Simulation?

  • Changing a model over time

    • Our world has some state to one time

    • A timestep later the world state has changed

    • We have to determine what should change and how

Simulation and reality

  • In general all game simulation is fakery

    • i.e. a "model" of how things change - not 100% correct

  • We have choices about how much we fake things

  • And choices about efficient algorithms etc

  • Why always fakery?

Why always fakery

Constraints

  • Simulating in real-time means making larger approximations

    • CPU/GPU constraints

    • We may be happy with larger errors for lower computational cost

  • Simulation inevitably requires some mathematics

    • \$=>\$ programmer mathematics ability may influence the choice/implementation of techniques

Technical terms

  • Time

  • Length

  • Position

  • Velocity

  • Speed

  • Acceleration

  • Mass

  • Force

Time

  • A measure of duration

  • A scalar (single dimensional / not a vector / has no direction)

  • Units are seconds: (\$s\$)

  • Usual symbol is \$t\$

  • e.g. \$t = 4.3s\$

Length

  • A measure of space

  • Units are metres: (\$m\$)

  • e.g. \$l = 3.6m\$

Position

  • Where in space a point is, relative to some base point

  • Units are metres: (\$m\$)

  • A vector (\$m\$ on each axis)

  • Usual symbol is \$\vec{p}\$ or \$\vec{r}\$

  • e.g. \$\vec{p} = (2.3, 4.5)\$

Velocity

  • The rate of change of position over time (derivative)

  • Units are metres per second: \$\frac m s\$ (also \$m s^{-1}\$)

  • A vector (\$m s^{-1}\$ on each axis)

  • Usual symbol is \$\vec{v}\$

  • e.g. \$\vec{v} = (0.4, 2.3){m s^{-1}}\$

Speed

  • Magnitude of velocity: \$|v|\$

    • i.e. independent of the direction

  • Units are also metres per second: \$\frac m s\$ (also \$m s^{-1}\$)

  • A scalar (has no direction)

  • e.g. \$speed = 2.33{m s^{-1}}\$

  • Can compute from velocity - (and reverse if have the direction)

Computing Speed

  • Magnitude of a vector is its length

  • How do we compute the length of vector?

    • Square root of the sum of the squares

Computing Speed 2D

  • if \$\vec{v} = (x, y)\$

    • \$|v| = \sqrt{x^2 + y^2}\$

  • if \$\vec{v} = (0.4, 2.3)\$

    • \$|v| = \sqrt{0.4^2 + 2.3^2} = 2.33\$

Computing Speed 3D

  • if \$\vec{v} = (x, y, z)\$

    • \$|v| = \sqrt{x^2 + y^2 + z^2}\$

  • if \$\vec{v} = (0.4, 2.3, 3.2)\$

    • \$|v| = \sqrt{0.4^2 + 2.3^2 + 3.2^2} = 3.96\$

Acceleration

  • The rate of change of velocity over time (derivative)

  • Units are metres per second per second: \$\frac m {s^2}\$ (also \$m s^{-2}\$)

  • A vector (\$m s^{-2}\$ on each axis)

  • Usual symbol is \$\vec{a}\$

  • e.g. \$\vec{a} = (0.4, 2.3){m s^{-2}}\$

Mass

  • A measure of resistance to change of motion when a force is applied

  • Is NOT the weight of an object (that depends on gravitational pull)

  • Units are \$kg\$

  • A scalar (has no direction)

  • Usual symbol is \$m\$

  • e.g. \$m = 45.3kg\$

Force

  • Causes objects to change velocity (when unopposed)

  • Units are Newtons: (\$N\$)

  • A vector - has direction as well as magnitude

  • Usual symbol is \$F\$

  • e.g. \$F = 153.3N\$

Vectors

  • Vectors are a good representation

  • Easy to understand

  • Moving from 2D to 3D is easier

  • Efficient for processing

Magnitude and direction

  • You can always computed magnitude and direction from a vector

  • Using magnitude and direction is hard work

  • Using magnitude and direction frequently means using \$sin\$ and \$cos\$

    • which are relatively expensive operations

Units

  • Use SI Units

    • International System of Units (Système international d’unités, SI)

  • Will make your life MUCH easier

    • avoid inches, miles

Newton’s Laws of Motion

  • What are they?

Newton’s First Law of Motion

  • Every object in a state of uniform motion tends to remain in that state of motion unless an external force is applied to it

    • also indicates that if an object is at rest (not moving) it will remain at rest

Newton’s Second Law of Motion

  • A force is applied only to the concept we commonly call acceleration

  • An object’s mass, acceleration, and the applied force may be represented by

    • \$\LARGE F = ma\$

Newton’s Third Law of Motion

  • For every action there is an equal and opposite reaction`

  • If two objects bump into each other they will react by moving apart

    • in an interaction between two objects, apply the same (but opposite direction) forces to each

      • can simplify for infinite masses, or things we don’t want to move

Using acceleration in your game

  • Some representation of velocity in your game is vital for objects to move (new positions calculated from old positions, velocity and time)

  • You don’t have to use acceleration

    • your game could set velocity values directly

    • this is unrealistic (compared to real world), but frequently doesn’t matter

      • e.g. changing from not moving to travelling at \$5{m s^{-1}}\$ instantly is an infinite acceleration

Simulation steps (finally)

  • If we know the position and velocity of an object we can calculate its position some time later

    • \$\vec{p'} = \vec{p} + \vec{v} * \Delta t\$

    • this is called integration

  • This works just fine as long a \$\vec{v}\$ is constant throughout \$\Delta t\$

Simulation steps (simple code)

position = position + velocity * dt;

Smaller simulation steps

  • Assuming velocity is constant through a simulation step is frequently (usually) wrong

  • The impact of the incorrect assumption can be mitigated by:

    1. reducing the duration of the time step

    2. using a more sophisticated form of integration (e.g. Runge-Kutta 4)

Changing velocity over time

  • Just as we can change position according to velocity we can change velocity according to acceleration

    • \$\vec{v'} = \vec{v} + \vec{a} * \Delta t\$

  • Assumes that \$\vec{a}\$ is constant throughout \$\Delta t\$

Changing velocity, Changing position

  • Usual practice is to use acceleration to calculate new velocities

  • THEN use those new velocities to calculate new positions

Time and time-steps

  • It’s important to be able to obtain the real time in games

    • so that we can make sure we simulate and render appropriately

Seconds vs. Milliseconds

  • many many engines/libraries use milliseconds as their base unit of time

    • including SDL2 - grrr - Smiley green alien GRRR

  • Why?

    • for many purposes milliseconds is enough precision

    • for many purposes time in \$ms\$ can be represented by an integer

Use Seconds

  • Using \$seconds\$ everywhere is my strong recommendation

  • possibly worth wrapping functions/methods that use \$ms\$ to use \$seconds\$

    • in some situations the precision of a 32-bit float may not be enough

    • consider using a 64-bit float

      • double - double precision floating point type.

        • usually IEEE-754 64 bit floating point type

SDL_GetTicks

  • SDL_GetTicks() gives you the number of milliseconds since the SDL library initialization (as 32 bit int)

  • How many milliseconds per frame

    • at 60fps?

    • at 100fps?

    • at 120fps?

C++11 - chrono

#include <iostream>
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;

int main()
{
    auto t1 = Clock::now();
    auto t2 = Clock::now();
    std::cout << "Delta t2-t1: "
              << std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count()
              << " nanoseconds" << std::endl;
}

Simulate at render-rate?

  • Your speed of render will be variable

    • across machines

    • over time

  • Track how long it has been since the last render and simulate that length of time

    • what happens if no-vsync

    • ??

Fix your time step???

Simulation correctness (the rare case)

  • Only in some situations the future may be calculated analytically

    • for very simple models (or parts of a more complex model)

    • e.g. determine analytically when a ball will hit the floor

Equations for a falling body

  • Assuming constant acceleration

  • Assuming no air resistance

Equations for a falling body 2

  • Distance \$d\$ travelled by an object falling for time \$t\$:

\$d=\frac{1}{2}(g*t^2)\$

  • Time \$t\$ taken for an object to fall distance \$d\$:

\$t =\ \sqrt {\frac{2d}{g}} \$

Distance (d) travelled by an object falling for time (t)

  • let’s assume t is 2 seconds

  • what is g?

\$d=\frac{1}{2}(g*t^2)\$

  • ⇒ d = ???

Distance (d) travelled by an object falling for time (t) 2

  • let’s assume t is 2 seconds

  • what is g?

    • force/acceleration due to gravity

    • = 9.81 meters per second per second

Distance (d) travelled by an object falling for time (t) 3

  • t = 2.00

  • g = 9.81

\$d=\frac{1}{2}(g*t^2)\$

  • ⇒ d = 19.62 meters !!

Time (t) taken for an object to fall distance (d)

  • g = 9.81

  • d = 54 meters (height of the Leaning Tower of Pisa)

\$t =\ \sqrt {\frac{2d}{g}} \$

  • ⇒ t = ???

Time (t) taken for an object to fall distance (d) 2

  • g = 9.81

  • d = 54 meters (height of the Leaning Tower of Pisa)

\$t =\ \sqrt {\frac{2d}{g}} \$

  • ⇒ t = sqrt(2 × 54) / (9.81) = 11.01)

  • ⇒ t = 3.32

Simulation incorrectness (the normal case)

  • the vast majority of game simulations cannot be solved analytically

  • so we solve instead by taking time steps

    • and assuming (usually incorrectly) that some properties don’t change during that step

    • this means that our simulations are INCORRECT

Problems due to simulation incorrectness

  • incorrect gain (or loss) of energy in the system

    • leading to instability / blowing up

Other things you may want to simulate

  • There may be other things in your game which change or you want to change

    • these should be changed in update also

    • perhaps in a separate part of update

      • i.e. do all "physics", then do all other updates

  • What might this include???

Other things you may want to simulate 2

  • Camera movement

  • Animations (Sprite Animations, Character Animations)

  • HUD/menu things

  • …​