maths fundamentals games fundamentals © by jarek francik kingston university, london 2008-2013
TRANSCRIPT
Maths Fundamentals
Games Fundamentals© by Jarek Francik
Kingston University, London
2008-2013
Trigonometry
Trigonometry
a
a
b
c
sin
coscot
cos
sintan
cos
sin
a
bb
ac
bc
a
22
22
sincos2cos
cossin22sin
1cossin
Detection of Collisions
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
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...
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
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’
w
(X, Y)h
Detection of Collisions
s.w
(s.X, s.Y)
s.h
Detection of Collisions
w
(X, Y
)
h
s.w
(s.X, s.Y)s.h
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’’
Detection of Collisions
w
(X, Y)
h
s.w
(s.X, s.Y)
s.hgeneral case is like this...
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);
Thales’ Theoremand Perspective in 3D
A D B
E
C
Thales Theorem
Perspective in 3D
Perspective in 3D
f
z
x’
x
xz
fx
z
x
f
x
'
'
z
x
f
xtg
'
Notice also that:
Z
f
Y
z
y’
yscreen
image
yz
fy
f
y
z
ytg '
'
a
Perspective in 3D
Linear Function&
Linear Equation
Linear Function & Linear Equation
y = ax + b
Ax + By + C = 0
x = x0 + at
y = y0 + at
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
Linear Function & Linear Equation
y = ax + b
Ax + By + C = 0
x = x0 + at
y = y0 + at
a
cb
c
Linear Function & Linear Equation
y = ax + b
Ax + By + C = 0
x = x0 + at
y = y0 + bt
a
(x0, y0)
a
b
a
btg
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);
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
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
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
Vectorsand
Vector Algebra
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
Vector
• In Physics:Vectors:– velocity– acceleration– force– momentum
Scalars:– speed– mass– power– energy 22 yxv
),( yxv
x
y
Normal Vector
• Vector perpendicular to a surface
• In CG widely used to model the shapes, e.g.:– lighting & 3D rendering– calculating the collisions
Adding Vectors
),(
),(
bb
aa
yxB
yxA
B
A
A + B
),( baba yyxxBA
Multiplication of 2D Vectors
• Multiplication of a Vector by a Number
),(
),(
nynxnA
yxA
A
2A
3A
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
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/
Multiplication of 2D Vectors
Quick calculation:
• Dot Product:
• Cross Product:
N is the Normal Vector to A and B
baba yyxxBA
NxyyxBA baba )(
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
Multiplication of 3D Vectors
• Multiplication of a Vector by a Number
• Dot Product of Vectors
• Cross Product of Vectors
),,( nznynxnA
bababa zzyyxxBA
),,( babababababa xyyxzxxzyzzyBA
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)
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)