maths fundamentals games fundamentals © by jarek francik kingston university, london 2008-2013

40
Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Upload: dylan-randall

Post on 19-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Maths Fundamentals

Games Fundamentals© by Jarek Francik

Kingston University, London

2008-2013

Page 2: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Trigonometry

Page 3: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Trigonometry

a

a

b

c

sin

coscot

cos

sintan

cos

sin

a

bb

ac

bc

a

22

22

sincos2cos

cossin22sin

1cossin

Page 4: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Detection of Collisions

Page 5: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Detection of Collisions

w

(X, Y)

(p.x, p.y)

Condition for collision

abs(p.x – X) < w/2 && abs(p.y – Y) < h/2

public boolean hitTest(Point pt);

h

Page 6: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

w

(X, Y)

Condition for collision

abs(p.x – X) < w/2 + R && abs(p.y – Y) < h/2 + R

public boolean hitTest(Point pt);

h

(p.x, p.y)

R

R

R

Detection of Collisionssmall artifact...

Page 7: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

w

(X, Y)

Condition for collision

abs(r.X – X) < (w + r.w)/2 && abs(r.Y – Y) < (h + r.h)/2

public boolean hitTest(Rectangle rect);

h

Detection of Collisions

r.w

(r.X, r.Y)r.h

Page 8: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Condition for collision (not complete)

abs(s.X – X) < (w + w’)/2 && abs(s.Y – Y) < (h + h’)/2

w

(X, Y)h

Detection of Collisions

s.w

(s.X, s.Y)

s.h

w’

h’

Page 9: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

w

(X, Y)h

Detection of Collisions

s.w

(s.X, s.Y)

s.h

Page 10: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Detection of Collisions

w

(X, Y

)

h

s.w

(s.X, s.Y)s.h

Page 11: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Detection of Collisions

w

(X, Y

)

h

s.w

(s.X, s.Y)s.h

Condition for collision (not complete)

abs(s.X – X) < (s.w + w’’)/2 && abs(s.Y – Y) < (s.h + h’’)/2

w’’

h’’

Page 12: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Detection of Collisions

w

(X, Y)

h

s.w

(s.X, s.Y)

s.hgeneral case is like this...

Page 13: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Detection of Collisions

• In general case:1. rotate around centre of R1

so that edges of R1 are parallel to the axes

2. abs(X1 – X2) < (w1 + w’2)/2 && abs(Y1 – Y2) < (h1 + h’2)/2

3. rotate around centre of R2 so that edges of R2 are parallel to the axes

4. abs(X1 – X2) < (w’1 + w2)/2 && abs(Y1 – Y2) < (h’1 + h2)/2

5. if both conditions are true we have a collision

public boolean hitTest(Sprite sprite);

Page 14: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Thales’ Theoremand Perspective in 3D

Page 15: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

A D B

E

C

Thales Theorem

Page 16: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Perspective in 3D

Page 17: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Perspective in 3D

f

z

x’

x

xz

fx

z

x

f

x

'

'

z

x

f

xtg

'

Notice also that:

Page 18: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Z

f

Y

z

y’

yscreen

image

yz

fy

f

y

z

ytg '

'

a

Perspective in 3D

Page 19: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Linear Function&

Linear Equation

Page 20: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Linear Function & Linear Equation

y = ax + b

 

Ax + By + C = 0

 

x = x0 + at

y = y0 + at

Page 21: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Linear Function & Linear Equation

y = ax + b

 

Ax + By + C = 0

 

x = x0 + at

y = y0 + at

ab

a=tg a

cannot represent a vertical linea

Page 22: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Linear Function & Linear Equation

y = ax + b

 

Ax + By + C = 0

 

x = x0 + at

y = y0 + at

a

cb

c

Page 23: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Linear Function & Linear Equation

y = ax + b

 

Ax + By + C = 0

 

x = x0 + at

y = y0 + bt

a

(x0, y0)

a

b

a

btg

Page 24: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Linear Motion

x = x0 + at

y = y0 + bt

direction may be determined by either:

pair tangent angle

if t is time, a and b determine velocity

a

b

(x,y)

(x+a,y+b)

a

btg ),( ba

Sprite *p = new CSpriteXxx(...);

p->SetDirection(angle);p->SetDirection(a, b);

p->SetSpeed(v);

p->SetVelocity(a, b);

Page 25: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Dx

Dy

(x5,y5)

Dx

Dy

(x3,y3)

can be applied iteratively:

xn = xn-1 + Dx

yn = yn-1 + Dy(x0,y0)

Dx

Dy

(x1,y1)

Dx

Dy

(x2,y2)Dx

Dy

(x4,y4)Linear Motion

Page 26: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

vx

vy

(x5,y5)

vx

vy

(x3,y3)

Deltas are the Velocity:

xn = xn-1 + vx

yn = yn-1 + vy

(x, y) position

(vx, vy) velocity vector

speed (scalar)

(x0,y0)

vx

vy

(x1,y1)

vx

vy

(x2,y2)vx

vy

(x4,y4)

p->GetXVelocity();p->GetYVelocity();p->GetSpeed();

p->SetVelocity (a, b);p->SetSpeed(v);22

yx vvv

Linear Motion

Page 27: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

vx3

vy3

(x3,y3)

increase velocity in each step:

vxn = vxn-1 + ax

vyn = vyn-1 + ay

xn = xn-1 + vx

yn = yn-1 + vy

(ax, ay) acceleration vector

acceleration (vector)

(x0,y0)

vx1

vy1

(x1,y1)

vx2

vy2

(x2,y2)

x = p->GetXVelocity();y = p->GetYVelocity();x += ax;y += ay;p->SetVelocity(x, y)

// another methodv = p->GetSpeed();v += a;p->SetSpeed(v);

22yx aaa

Accelerated Motion

Page 28: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Vectorsand

Vector Algebra

Page 29: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Vector

Geometrical object that:– has magnitude (length)– has direction– can be added to other vectors

• In 2D plane:

• In 3D space:

• In n-dimensional space),,( zyxv

),,,( 21 nxxxv

),( yxv

22 yxv

),( yxv

x

y

Page 30: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Vector

• In Physics:Vectors:– velocity– acceleration– force– momentum

Scalars:– speed– mass– power– energy 22 yxv

),( yxv

x

y

Page 31: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Normal Vector

• Vector perpendicular to a surface

• In CG widely used to model the shapes, e.g.:– lighting & 3D rendering– calculating the collisions

Page 32: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Adding Vectors

),(

),(

bb

aa

yxB

yxA

B

A

A + B

),( baba yyxxBA

Page 33: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Multiplication of 2D Vectors

• Multiplication of a Vector by a Number

),(

),(

nynxnA

yxA

A

2A

3A

Page 34: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Multiplication of 2D Vectors

• Multiplication of a Vector by a Number• Cross Product of Vectors

cross product ABis a vector perpendicular

to both A and B

(in 3D space)

AB

A

B

BA = - AB

NBABA sin

Interactive demo:http://physics.syr.edu/courses/java-suite/crosspro.html

Page 35: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Multiplication of 2D Vectors

• Multiplication of a Vector by a Number• Cross Product of Vectors• Dot Product of Vectors

B

A

cosBABA

Interactive demo:http://www.falstad.com/dotproduct/

Page 36: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Multiplication of 2D Vectors

Quick calculation:

• Dot Product:

• Cross Product:

N is the Normal Vector to A and B

baba yyxxBA

NxyyxBA baba )(

Page 37: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Multiplication of 2D Vectors

AB AB

number (scalar) vector

|A||B|cos |A||B|sin Na = 0 max 0 > 0 < 90a

a = 90 0 max

quick formula: xa xb + ya yb xa yb - ya xb

Page 38: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Multiplication of 3D Vectors

• Multiplication of a Vector by a Number

• Dot Product of Vectors

• Cross Product of Vectors

),,( nznynxnA

bababa zzyyxxBA

),,( babababababa xyyxzxxzyzzyBA

Page 39: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Example

Am I approaching him or not?

yes, if: V•D > 0

V•D = VXX + VYY

D = X, Y = XE – XR, YE – YR V = VX, VY

double x = enemy.GetX() – rocket.GetX();double y = enemy.GetY() – rocket.GetY();double vx = rocket.GetXVelocity();double vy = rocket.GetYVelocity();if (vx * x + vy * y > 0)

Page 40: Maths Fundamentals Games Fundamentals © by Jarek Francik Kingston University, London 2008-2013

Example

Is it on my left or my right?

right, if: VxD > 0

VxD = VXY - VYX

D = X, Y = XE – XR, YE – YR V = VX, VY

double x = enemy.GetX() – rocket.GetX();double y = enemy.GetY() – rocket.GetY();double vx = rocket.GetXVelocity();double vy = rocket.GetYVelocity();if (vx * y - vy * x > 0)