Title: CO1301: Games Concepts
1CO1301 Games Concepts
Lecture 8 Vectors
- Dr Nick Mitchell (Room CM 226)
- email npmitchell_at_uclan.ac.uk
- Material originally prepared by Gareth Bellaby
2Essential Reading on Vectors
- Rabin, Introduction to Game Development
- 4.1 "Mathematical Concepts"
- Van Verthe, Essential Mathematics for Games
- Chapter 1 "Vectors and Points"
3Lecture Outline
- Maths Preliminaries
- Coordinates
- Vectors
- Vector arithmetic
- Vector length
- The direction vector and scalars
4- Topic 1 Maths Preliminaries
5Order of precedence
- Mathematical operations have an order of
precedence. - Multiplication and division have equal
precedence. - Multiplication and division are always carried
out before addition and subtraction.
6Right-angled triangles
- With a right-angled triangle the length of the
longest side is equal to the square root of the
sum of the squares of the other two sides.
7Right-angled triangles
8Topic 2 Coordinates
9Coordinates
- A coordinate represents a point in space.
- A point in 2D is represented using coordinates
(X, Y) - In 3D it is represented using coordinates (X, Y,
Z) - The convention is
- use round brackets
- a comma separated list, e.g. ( 2, 4, 7 )
- always in the sequence x, y, z.
- Called Cartesian geometry after Rene Descartes.
10Coordinates
- We use a grid to identify points in space.
- Can choose the direction to point Z.
- Use your hand to indicate the geometry. This
gives the "handedness" of the geometry. We are
using left-handed axes.
Z
Y
- X is thumb
- Y index finger
- Z middle finger
X
11Coordinates
Y
Z
Z
Y
X
X
- The origin of the grid is at ( 0, 0, 0 )
12Topic 3 Vectors
13Vectors
- We often need to identify a movement or direction
in 3D - this can also be represented using values
(X, Y, Z) and is called a vector.
Vectors are more easily shown in 2D using where
we only need (X, Y) values.
14Vector between two points
- Calculated by subtracting one point from the
other. - Subtract the x values to produce one new x, the y
values to produce a new y and then the z values
to produce a new z. - The vector V from P1(X1, Y1) to P2(X2, Y2) is
- V( X2 - X1, Y2 - Y1)
- Note that P1 is subtracted from P2
15Vectors
- The arrow of movement from P1 to P2 can be
represented as the vector V(20,10).
V(X2 - X1, Y2 Y1) (30, 30) - (10, 20)
(20, 10)
The vector V from point P1(X1, Y1) to P2(X2, Y2)
is
16Vector between two points
- Notice how the values in the vector are found by
subtracting the source point coordinates from the
destination point coordinates. This means that
the direction of the vector is important the
vector in the reverse direction, from P2 to P1,
is (-20, -10). - (10, 20) - (30, 30)
- (-20, -10)
17Vectors
- 2D extends simply to 3D. The vector V from point
P1(X1, Y1, Z1) to P2(X2, Y2, Z2) is - V(X2 - X1, Y2 Y1, Z2 - Z1).
- Points and vectors are represented in the same
way. A vector can be directly applied to a point. - Location and movement can all be considered to be
the same thing. - A point is represented by P(X, Y, Z)
- A vector is represented by V(X, Y, Z)
- A vector of force is represented in the same way,
e.g. the effect of wind.
18Vectors
- The line between two points is the path you need
to take to travel directly from one point to
another. - The vector is the same no matter what its
coordinates are. - At the origin this is obviously true.
- But you can also see this will be true wherever
the points are you can move the vector over
the grid and it stays the same.
19Vector direction
- Which point is subtracted from which is
important. - The order gives us the direction of movement.
P1(1, 1) to P2 (3, 3) (3 - 1, 3 - 1) (2, 2)
P2(3, 3) to P1 (1, 1) (1 - 3, 1 - 3) (-2, -2)
20Vector direction
- If you ever need a reminder about the order when
calculating the vector between two points just
think about the direction of the movement. - Easiest to do this if you imagine one of the
points being at the origin.
From (0, 0, 0) to (2, 2, 0) ( 2 - 0, 2 - 0, 0 -
0) ( 2, 2, 0 ) Upwards and to the right
From (2, 2, 0) to (0, 0, 0) ( 0 - 2, 0 - 2, 0 -
0) ( -2,- 2, 0 ) Downwards and to the left
21Topic 4 Vector Arithmetic
22Vector Addition
- We visualise vectors by drawing a line with an
arrowhead at one end.
Two vectors V and W are added by placing the
beginning of W at the end of V.
23Vector Addition
24Vector Subtraction
- Subtraction works in the same way as addition.
- The direction in which the subtracted vector
points is effectively reversed. - V(1, 3) - W(3, 1) (-2,
2)
25Vector Arithmetic
- The order of operations is irrelevant.
- V W W V
- V - W W - V
26Topic 5 Vector Length
27Length of a vector
- If we have a vector V(X, Y, Z) then we can use
Pythagoras Theorem to show that
28Length of a vector
Easiest with a 2D example.
- A vector makes a right-angled triangle.
- The shortest sides of the triangle are the x and
y values. - 3D is exactly the same - but with the addition of
tee z axis.
29Maths symbol for length
The length of a vector v(x,y,z) is given by
30Topic 5 The direction vector and scalar
31Direction of movement
- The direction between two points is only partly
useful. - The problem is that its size depends on the
distance between the two points.
Both vectors are pointing in the same direction,
but the black vector is half the size of the red
vector
32Direction of movement
- As you know, the units we use within 3D graphics
are arbitrary. Sometimes in the TL-Engine a
distance of 0.1 is large, sometimes it is small.
It all depends on the scale. - Direction should be independent of scale.
- The direction vector is a vector which is
independent of scale. - The direction vector has a length of 1.
- A vector whose length is 1 is said to be
normalised.
33The direction vector
- The direction vector is found by
- Calculating the length of the vector
- Dividing each component of the vector by its
length - For example, given a vector V( 1, 3, 6 )
direction vector
length
34The direction vector
- Here is a 2D example of the process
length
direction vector
353D example
- Vector pointing straight forward ( 0, 0, 7.5 )
Vector pointing straight forward ( 0, 0, 3.1 )
length
length
direction vector
direction vector
36"Pointing at" or "Looking at"
- Vectors can be used to represent a direction (in
the sense of North or North-West). - To do this we create a vector that points in
the direction we want, and has a length of 1
unit. - We often need to find the direction vector that
points from one position to another. The
process we use is - Get the vector between the two positions V(X, Y,
Z) - Find the length of this vector L
- The direction vector is VDir(X/L, Y/L, Z/L)
37Maths for normalised vector
- A normal is any vector whose length is 1
- Represented with a symbol
- A vector is converted to a normal (normalised) by
dividing the components by the length
38Scalar
- Direction vectors are always length 1.
- They are always the unit length.
- In order to describe an actual movement you
combine the direction vector with a scalar. - The scalar is the distance to be moved.
- Imagine that you want to move something by a
given distance in a particular direction. We
multiply the direction vector by the distance we
want to move. - For example, move the normalised vector ( 2, 4,
5 ) by 12 units results in ( 24, 48, 60 )
39Scalar
- A normalised vector can be seen as the direction
of a vector - A vector can be broken up into normalised vector
and length, e.g. - Movement Normalised vector distance
- Velocity Normalised vector speed
- The scalar changes the magnitude of a vector.
- The root word is "scale".
- The operation of the scalar is to scale the
vector. - This also reminds us why normalised vectors are
useful.