Presentation is loading. Please wait.

Presentation is loading. Please wait.

CO1301: Games Concepts Lecture 21 Timing + Quads + Numbers

Similar presentations


Presentation on theme: "CO1301: Games Concepts Lecture 21 Timing + Quads + Numbers"— Presentation transcript:

1 CO1301: Games Concepts Lecture 21 Timing + Quads + Numbers
Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby

2 Topic 1: Timing The use of an absolute value for movement each frame is obviously flawed - the speed of your game depends on the speed of the computer. Things would get much worse if you introduced any more complex calculations which require time, e.g. acceleration. The solution is to take time into account.

3 Games Development 1 There are different ways to accomplish timing
We are just introducing one approach in this module. More detail next year with Laurent. See Games Development 1, Week 14: "Overview, Timing and the Game Loop". In this module you are using the variable time approach.

4 Variable Time The approach is to discover how long it took the last frame to render. Adjust the timings in the current frame. The TL-Engine has a command Timer() Timer() returns the amount of time in seconds since it was last called. Call it once per frame: at the beginning of the frame before you do any processing. This obtains the frame time. The frame time is the time taken to process 1 frame. Frames per second (FPS) or frame rate is: frame rate = 1 / frame time

5 Variable Time Need to multiply all time dependent variables through by the adjustment. Movement is a dependent variable. So it rotation, so is acceleration... There may well be others. Measure everything in units per second. Record the frame time (the time taken to render the previous frame). Multiply all measurements by the frame time.

6 Variable Time Example An object is moving at a velocity of 5 units per second. A particularly slow computer takes 0.1 seconds to render a frame. The frame time is 0.1 seconds. movement = 5 * 0.1 = 0.5 The object moves 0.5 units per frame. Each frame takes 1/10th second to render. So over the course of 1 second it will indeed move 5 units.

7 Variability The timing is maintained between computers.
The slow computer takes 0.1 seconds to render a frame. A faster machine takes 0.02 seconds to render it. On the slow machine the frame time is 0.1 5 * 0.1 = 0.5 units per frame One the faster machine the frame time is 0.02 5 * 0.02 = 0.1 units per frame

8 Variability The slower machine renders 1 frame for every 5 frames of the faster machine so the object moves the same distance in the same time on both machines. The method copes with variability of frame processing. Our calculations will always be relative to the amount of time taken to render the previous frame, with the measurement taken at the beginning of the current frame, therefore uneven timing will be automatically dealt with.

9 Units The 3D world has abstract units built into it. We talk about moving 1 unit per frame. However, the units could represent anything. A unit could be 1 centimetre, 1 metre, 1 kilometre, and so on. It is necessary to decide what units you are working in. The modelling packages also set up measurements and this will allow you to determine speeds, distances, etc. It particularly important when talking about velocities, acceleration, etc. in your program.

10 Topic 2: Quads Short for "quadrilateral”: A four sided polygon.
"Quad" is a common phrase for a simple rectangle. It is the simplest 4 sided polygon. A quad has no depth. A quad is made up of two triangles.

11 Quads A simple shape which can be constructed on-the-fly on the CPU or GPU. A texture can mapped onto the quad. Not to be confused with a sprite. A sprite is a 2D image. Uses screen coordinates. It is separate from the 3D world.

12 Billboards Quads can be used for "billboards". A billboard is a simple technique whereby a 2D texture painted onto a quad and used to give the illusion of 3D. One use is a set of billboards placed in a pattern in order to give the illusion of "bulk" or complexity e.g. a complex object such as the twigs and leaves of a tree. The example on the next slide is from GTA IV. The quads will placed on different planes.

13 Billboards

14 Billboards Billboards can also be used for imposters.
A 2D model is used in place of a 3D model, e.g. the billboarding for trees in the distance or for clouds. In this case the quad is always oriented towards the camera.

15 Imposters

16 Particle systems Billboards are also used for particle systems.
A particle system is a graphics technique in which an effect is modelled as a set of discrete particles, e.g. fire, smoke, rain. A particle system is typically made up of lots of particles (hundreds, even millions). It is expensive to render so many 3D models. Typically billboards are used instead. The quad is always oriented towards the camera and so the illusion of depth and 3 dimensionality is maintained. The number of particles means that duplication goes unnoticed.

17 Particle systems "Building a Million Particle System", Lutz Latta

18 Note This approach is being superseded and modified.
DirectX now has "point sprite" as a primitive type. A point sprite is a quad generated on-the-fly on the graphics card. Increased processing power means that billboarding is less necessary. Techniques such as "instancing" used in place of (or alongside) billboards.

19 Topic 3Numbers Mathematics distinguishes between several types of number: Natural Numbers (non-negative whole numbers) 0,1,2…, only positive numbers Integers 0,1,-1,2,-2,3,-3…, (+ve or -ve whole numbers) Real numbers 0.02, , 124.0, , any number written with a decimal point (possibly infinitely long) As well as others… A single number (in any form) is called a scalar .

20 Numbers in C++ Equivalent types of number in C++:
int - integers (+ve or –ve) unsigned int - whole numbers (+ve only) float - real numbers Each type has finite storage Exact amount depends on the compiler They have limitations not present in maths: Maximum and minimum values e.g. typical int (4-bytes) has max/min of ±2 billion So large values may be out of range for an int

21 Floating point concerns
Floating point numbers cannot all be accurately represented. The float and double data types rarely hold exactly the numbers you assign or would expect them to. Calculations can cause difference to magnify i.e. error propagation. Difference will be greater for larger values. Never use ==, except for small integers.

22 Test against zero Always assume a margin of error, e.g. test against a range: -epsilon < x < epsilon, where epsilon is a small number. For example, a test against zero. const float kfEpsilon = 0.5e-6f; bool IsZero( const float x ) { return fabs( x ) < kfEpsilon; }

23 Number conversion C++ can automatically convert number types
But doesn’t always: 5 / 10 = 0, Correct: The compiler usually issues a warning when it does perform a conversion, e.g. Don’t ignore warnings – ensure it is what you want float / double are rounded down to int f = 5.0 / 10.0; // Correct int i = 9.0/10.0; // Warning likely (i = 0)

24 Number conversion To round to the nearest int:
N.B. 0.5 is a double, 0.5f is a float In order to convert number types you use casting. The modern C++ casting style is: int NearInt = MyFloat + 0.5f; float f = static_cast<float>(MyInteger);


Download ppt "CO1301: Games Concepts Lecture 21 Timing + Quads + Numbers"

Similar presentations


Ads by Google