Processing math: 100%
+ - 0:00:00
Notes for current slide
Notes for next slide

Creación de Videojuegos

Vectors

1 / 24

Math

Cady: I like math.

Damian: Eww. Why?

Cady: Because it's the same in every country.

2 / 24

Math

Cady: I like math.

Damian: Eww. Why?

Cady: Because it's the same in every country.

Mean Girls (2004)

3 / 24

Math

  • In video games we need a lot of math

  • In particular, we need a lot of vector math

  • Generally, game objects will be located somewhere, move, collide, etc.

  • Today we are going to revisit how vectors work

4 / 24

Vectors

5 / 24

Vectors

  • How do we represent positions, movements, etc.? Vectors!

  • A vector has 2 (or 3) components, called x and y (and z)

  • Addition and subtraction work component-wise

  • You can also multiply a vector with a real number

6 / 24

Vectors

(xy)+(ab)=(x+ay+b)

a(xy)=(axay)

(xy)(ab)=(xayb)

7 / 24

Vector Operations

  • A vector describes a change in position

  • As such, a vector does not itself have a position

  • That means, the vector that moves a point a by 3 units in x direction and 2 units in y direction is the same as the vector that moves a point b by 3 units in x direction and 2 units in y direction

(32)

8 / 24

Vector Operations

  • For two points a and b, b - a gives you a vector: the change required to go from a to b

  • t(ba) gives you a "part" of that change

  • a+t(ba) gives you a point on the line from a to b. t determines where on the line that point is:

    • 0: The point is a
    • 0.5: The point is halfway between a and b
    • 1: The point is b
    • greater than 1: The point is on the opposite side of b than a
    • etc.
  • You can interpret t as a "percentage"

9 / 24

Example

  • The player is currently at (7,10)

  • The enemy is at (1,2)

  • Which direction does the enemy have to walk in, to get to the player?

  • What point is a quarter (0.25) of the way from the enemy to the player?

10 / 24

Velocity?

  • Now we know the "direction" the enemy has to walk in

  • But usually we want our game objects to move with constant velocity

  • How do we ensure that?

11 / 24

Velocity?

  • Now we know the "direction" the enemy has to walk in

  • But usually we want our game objects to move with constant velocity

  • How do we ensure that?

  • Unit vectors!

12 / 24

The length of a Vector

|(xy)|=x2+y2

|(xyz)|=x2+y2+z2

A unit vector is a vector with length 1.

13 / 24

Normalizing a Vector

  • A unit vector is useful to give us a general direction

  • We can later multiply it with the desired distance

  • To turn a vector into a unit vector, we divide it by its length

vn=v|v|

14 / 24

Example

  • The player is currently at (7,10)

  • The enemy is at (1,2)

  • The enemy is moving with 2 units/s towards the player

  • Where is the enemy after 2 seconds?

  • When will the enemy reach the point that is 80% of the way to the player?

15 / 24

Vector Rotation

  • To turn a vector by 90 degrees: Exchange the coordinates and switch the sign of one, i.e. (x,y) becomes (-y,x)

  • How could we return by other angles?

  • We'll talk about that next week

  • But maybe we could start by calculating the angle between two vectors?

16 / 24

Vector Operations: The Dot Product

AB=(x1y1)(x2y2)=x1x2+y1y2AB=cos(α)|A||B|

  • Very useful for a variety of applications!

  • Simplest: Calculate the angle between two vectors.

  • Calculate the projection of a point onto a vector (e.g. distance from a line)

17 / 24

Sin and Cos

18 / 24

Projection

AB=(x1y1)(x2y2)=x1x2+y1y2AB=cos(α)|A||B|

What if B is a unit vector?

AB=cos(α)|A|

19 / 24

Dot Product: Projection

image/svg+xml

If B is a unit vector:

AB=cos(α)|A|

20 / 24

Dot Product: Example 1

  • An AI controlled enemy is at (11)

  • They are currently looking in direction (20)

  • The player is at (54)

  • The enemy has a field of view of 45 degrees in each direction. Can it see the player? (cos(45)=220.7)

21 / 24

Dot Product in 3 Dimensions

AB=(x1y1z1)(x2y2z2)=x1x2+y1y2+z1z2AB=cos(α)|A||B|

22 / 24

Next Time: 3D Graphics

23 / 24

Math

Cady: I like math.

Damian: Eww. Why?

Cady: Because it's the same in every country.

2 / 24
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow