Download vector - Games @ UCLAN

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Four-dimensional space wikipedia , lookup

Affine connection wikipedia , lookup

Duality (projective geometry) wikipedia , lookup

Cartesian coordinate system wikipedia , lookup

Cross product wikipedia , lookup

Line (geometry) wikipedia , lookup

Relativistic angular momentum wikipedia , lookup

CR manifold wikipedia , lookup

Lie derivative wikipedia , lookup

Tensor operator wikipedia , lookup

Dual space wikipedia , lookup

Curvilinear coordinates wikipedia , lookup

Metric tensor wikipedia , lookup

Tensors in curvilinear coordinates wikipedia , lookup

Riemannian connection on a surface wikipedia , lookup

Covariance and contravariance of vectors wikipedia , lookup

Transcript
CO1301: Games Concepts
Lecture 6
Vectors
Dr Nick Mitchell (Room CM 226)
email: [email protected]
Material originally prepared by Gareth Bellaby
Essential Reading on Vectors
 Rabin, Introduction to Game Development:
4.1:
"Mathematical Concepts"
 Van Verthe, Essential Mathematics for Games:
Chapter
2: "Vectors and Points"
Lecture Outline
Maths Preliminaries
Coordinates
Vectors
Vector arithmetic
Vector length
The direction vector and scalars
Topic 1: Some Maths Preliminaries
Order 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.
2  3 * 4  14
?
12 / 3  2  6?
2 * 3  4  10
?
12  3 / 2  13?.5
Right-angled triangles
 Pythagoras’ Theorem
 With
a right-angled triangle the length of the
longest side (called the hypotenuse) is equal to
the square root of the sum of the squares of the
other two sides.
a b c
2
a
2
2
b c
2
2
Right-angled triangles
a b c
2
2
2
Topic 2:
Coordinates
Coordinates
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.
Coordinates
• We use a 3D 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.
• In the TL-Engine we are using left-handed axes.
Z
• X is thumb
• Y index finger
• Z middle finger
Y
X
Coordinates
Z
Y
Y
X
Z
X
The origin of the grid is at ( 0, 0, 0 )
Topic 3: Vectors
Vectors
 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.
Vector 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)
Sometimes written P1 P2
Note that P1 is subtracted from P2
Vectors
 The arrow of
movement from P1
to P2 can be
represented as the
vector V(20,10).
The vector V from point
P1(x1, y1) to P2(x2, y2) is
V( x2 - x1, y2 - y1)
= (30, 30) - (10, 20)
?
= (20, 10)
Vector 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…
(10, 20) - (30, 30)
= (-20,? -10)
Vectors
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.
Vectors
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.
Vector 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)
Vector 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)
From (2, 2, 0) to (0, 0, 0)
= ( 2 - 0, 2 - 0, 0 - 0)
= ( 0 - 2, 0 - 2, 0 - 0)
= ( 2, 2, 0 )
= ( -2,- 2, 0 )
Upwards and to the right
Downwards and to the left
Topic 4: Vector Arithmetic
Vector 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.
Vector Addition
 V(1, 3) +
W(3, 1) =
(4,? 4)
Vector 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)
Vector Arithmetic
 The order of operations is irrelevant.
V+W=W+V
 V-W=W-V

Topic 5: Vector Length
Length of a vector
 If we have a vector V(x, y, z) then we can use
Pythagoras’ Theorem to show that:
length of V 
X Y  Z
2
2
2
Length 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.
Maths symbol for length
The length of a vector v(x, y, z) is given by:
| v | x  y  z
2
2
2
Topic 6: The direction vector
and scalars
Direction 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
Direction 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 of the models.
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.
The direction vector
 The direction vector is found by:
Calculating the length of the vector
2. Dividing each component of the vector by its
length
1.
 For example, given a vector V( 1, 3, 6 )
length
 12  32  6 2
 6.78
direction vector
1
3
6
(
,
,
)
6.78 6.78 6.78
 (0.15,0.44,0.88)
The direction vector
 Here is a 2D example of the process:
length
 4 2  32
5
direction vector
4 3
( , )
5 5
 (0.8,0.6)
3D example
 Vector pointing straight
forward: ( 0, 0, 7.5 )
Vector pointing straight
forward: ( 0, 0, 3.1 )
length
length
 0 2  0 2  7.52
 7.5
 0 2  0 2  3.12
 3.1
direction vector
0
0 7.5
(
,
,
)
7.5 7.5 7.5
 (0,0,1)
direction vector
0
0 3 .1
(
,
,
)
3 .1 3 .1 3 .1
 (0,0,1)
"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 (using pythagoras)
 The direction vector is VDir (x / l, y / l, z / l)
Maths for normalised vector
 A normal is any vector whose length is 1
 Represented
with a ^ symbol:
v̂
 A vector is converted to a normal (normalised) by
dividing the components by the length:
if vx, y, z  then the normalised
vector is vˆ x / v , y / v , z / v 
Scalar
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 )
Scalar
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.