CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email:...

30
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: [email protected] Material originally prepared by Gareth Bellaby Lecture 8 Basic Trigonometry Hipparchos the “father” of trigonometry (image from Wikipedia)

Transcript of CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email:...

Page 1: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

CO1301: Games Concepts

Dr Nick Mitchell (Room CM 226)email: [email protected]

Material originally prepared by Gareth Bellaby

Lecture 8

Basic Trigonometry

Hipparchos the “father” of trigonometry

(image from Wikipedia)

Page 2: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

References

Rabin, Introduction to Game Development, Chapter 4.1

Van Verth & Bishop, Essential Mathematics for Games, Appendix A and Chapter 1

Eric Lengyel, Mathematics for 3D Game Programming & Computer Graphics

Frank Luna, Introduction to 3D Game Programming with Direct 9.0c: A Shader Approach, Chapter 1

Page 3: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Lecture Structure Introduction

Trigonometric functions: sine, cosine, tangent

Circles

Useful trigonometric laws

Page 4: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Why study Trigonometry?

Why is trigonometry relevant to your course? Games involve lots of geometrical calculations:

Rotation of models; Line of sight calculations; Collision detection; Lighting.

For example, the intensity of directed light changes according to the angle at which it strikes a surface.

You require a working knowledge of geometry.

Page 5: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Mathematical Functions A mathematical function defines a relationship between

one variable and another. A function takes an input (argument) and relates it to

an output (value) according to some rule or formula. For instance, the sine function maps an angle (as

input) onto a number (as output). The set of possible input values is the functions

domain. The set of possible output values is the functions

range. For any given input, there is exactly one output:

The 32 cannot be 9 today and 8 tomorrow! Mathematical Laws

I'll introduce some laws. I'm not going to prove or derive them. I will ask you to accept them as being true.

Page 6: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Greek letters It is a convention to use Greek letters to

represent angles and some other mathematical terms:

α alpha

β beta

γ gamma

θ theta

λ lambda

π pi

Δ (capital) Delta

Page 7: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Trigonometry Trigonometry arises out of an observation

about right angled triangles... Take a right angled triangle and consider one

of its angles (but NOT the right angle itself). We'll call this angle α.

• The opposite side to α is y.

• The shorter side adjacent to (next to) α is x.

• The longest side of the triangle (the hypotenuse) is h.

o

a

Page 8: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Trigonometry There is a relationship between the angle and the lengths

of the sides. This relationship is expressed through one of the trigonometric functions, e.g. sine (abbreviated to sin).

sin(α) = o / h o

a

Page 9: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Values of sinedegrees

sin (degrees)

0 0

15 0.26

30 0.5

45 0.71

60 0.87

75 0.97

90 1

105 0.97

120 0.87

135 0.71

150 0.5

165 0.26

degrees

sin (degrees)

180 0

195 -0.26

210 -0.5

225 -0.71

240 -0.87

255 -0.97

270 -1

285 -0.97

300 -0.87

315 -0.71

330 -0.5

345 -0.26

Page 10: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Trigonometry

Function Name

Symbol Definition

sine sin sin(α) = o / h

cosine cos cos(α) = a / h

tangent tan tan(α) = o / a

= sin(α) / cos(α)

You need to be aware of three trigonometric functions: sine, cosine and tangent.

o

a

Page 11: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Radians You will often come across angles measured in

radians (rad), instead of degrees (deg)...

A radian is the angle formedby measuring one radiuslength along the circumferenceof a circle.

There are 2 radians in acomplete circle ( = 360°)

deg = rad * 180° / rad = deg * / 180°

Page 12: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Trigonometry

Page 13: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Trigonometric Functions

Sine, cosine and tangent are mathematical functions.

There are other trigonometric functions, but they are rarely used in computer programming.

Angles can be greater than 2 or less than -2. Simply continue the rotation around the circle.

You can draw a graph of the functions. The x-axis is the angle and the y-axis is (for example) sin(x). If you graph out the sine function then you create a sine wave.

Page 14: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Sine Wave and Cosine Wave

Image taken from Wikipedia

Page 15: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Tangent Wave

Image taken from Wikipedia

Page 16: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

C++ C++ has functions

for sine, cosine and tangent within its libraries. Use the maths or

complex libraries:

The standard C++ functions use radians, not degrees.

#include <cmath>

using namespace std;

float rad;

float result;

result = sin(rad);

result = cos(rad);

result = tan(rad);

Page 17: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

PI Written using the Greek letter . Otherwise use the English transliteration "Pi".

is a mathematical constant. 3.14159 (approximately).

is the ratio of the circumference of a circle to its diameter. This value holds true for any circle, no matter

what its size. It is therefore a constant.

Page 18: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Circles The constant is derived from

circles so useful to look at these.

Circles are a basic shape. Circumference is the length

around the circle. Diameter is the width of a

circle at its largest extent, i.e. the diameter must go through the centre of the circle.

Radius is a line from the centre of the circle to the edge (in any direction).

Page 19: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Circles A tangent is a line drawn

perpendicular to (at right angles to) the end point of a radius. You may know these

from drawing splines (curves) in 3ds Max.

You'll see them when you generate splines in graphics and AI.

A chord is line connecting two points on a circle.

Page 20: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Circles

A segment is that part of a circle made by chord, i.e. a line connecting two points on a circle.

A sector is part of a circle in which the two edges are radii.

sector

Page 21: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Circle Using Cartesian coordinates.

Centre of the circle is at (a, b). The length of the radius is r. The length of the diameter is d.

rd

dncecircumfere

rncecircumfere

2

2

2

Page 22: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Points on a Circle Imagine a line

from the centre of the circle to (x,y)

is the angle between this line and the x-axis.

)sin(

)cos(

ry

rx

Page 23: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Identities

tan-tan

cos-cos

sin -sin

Page 24: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Trigonometric Relationships This relationship is for right-angled

triangles only:

1cossin 22

sin sin sin 2 Where

Page 25: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Trigonometric Relationships These relationships

are for right-angled triangles only:

cos

sin tan

2sin cos

2cos sin

Page 26: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Properties of triangles This property holds for

all triangles and not just right-angled ones.

The angles in a triangle can be related to the sides of a triangle.

cba

sin

sin

sin

:sines of law

Page 27: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Properties of triangles These hold for all triangles

cos 2

:cosines of law222 abbac

tan

-tan

: tangentsof law

21

21

ba

ba

Page 28: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Inverses Another bit of terminology and convention you

need to be familiar with. An inverse function is a function which is in the

opposite direction. An inverse trigonometric function reverses the original trigonometric function, so that If x = sin(y) then y = arcsin(x)

The inverse trigonometric functions are all prefixed with the term "arc": arcsine, arccosine and arctangent.

In C++: asin() acos() atan()

Page 29: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Inverses

The notation sin-1, cos-1 and tan-1 is common.

We know that trigonometric functions can produce the same result with different input values, e.g. sin(75o) and sin(105o) are both 0.97.

Therefore an inverse trigonometric function typically has a restricted range so only one value can be generated.

Page 30: CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uknpmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby.

Inverses

numbers

2,2 real tan

,0 11 cos

2,2 11 sin

1

1

1

,-

,-

Function

Domain Range