220 likes | 232 Views
This lecture covers the concepts of vectors and the dot product in game development. Topics include vector revision, model positioning, and the importance of the dot product. Examples and use cases are discussed.
E N D
CO1301: Games Concepts Lecture 10 Dot Product Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby
Vectors • Rabin, Introduction to Game Development: • 4.1: "Mathematical Concepts" • Van Verthe, Essential Mathematics for Games: • Chapter 1: "Vectors and Points"
Topics • Reminder about vectors • Model positioning • The dot product
Topic 1: • Reminder about Vectors
Vectors (Revision) • A vector is a directed edge. In 3 dimensions a vector has three components: v(x, y, z) For example the vector a = (2, 2, 0)
Length of a vector (Revision) • The length of a vector can be calculated from its components.
A normalised vector is a vector whose length is 1. Also known as the unit vector. A vector can be normalised by dividing each of its components by its length: The normalised vector (Revision)
Topic 2: • Model positioning
Model Positioning • A 3D model has its own local space: • three 3D vectors X,Y & Z • These define the local rotation of the model, e.g. rotating around its local y-axis, etc. • It also defines what is meant by movement local to its own axis, e.g. MoveLocalZ() means move along the direction of the model's z-axis.
Model Positioning • In order to be able to use a model you also need its position. The position and rotation of a 3D model can be specified by defining its local space: • Position: a 3D vertex P • Rotation: three 3D vectors X,Y & Z • Note the way in which the local axes of a model are described as vectors.
Model Positioning • Vectors also provide a scaling. The length of the vectors X, Y & Z can define the scaling in that axis • 1.0 = normal, 2.0 = double size etc. • Effectively scaling local space • This is an example of why normalising vectors is important. • Note: there are other ways to define model’s position • In particular, rotation can be held differently
Topic 3: The dot product
The dot product • The dot product gets its name from the symbol used: a single dot between two vectors. • Pronounced "V dot W", or say "the dot product of V and W". • The dot product takes two vectors and produces a single (numerical) result.
Why is it important to you? • The reason why the dot product is so important is because of the following equation: • Given any two vectors we can derive the angle between them. • What happens if v and w are normalised?
If the vectors are normalised • The length of a normalised vector is 1. • The equation therefore simplifies as follows:
Example • This example also brings out the way in which vectors and coordinates are intimately related. • You had a monster which attacked if you moved close to it. It would be nice if you could sneak up behind the monster. • The monster is looking in a particular direction. The centre of its vision is pointing in a particular direction. Therefore this can be considered to be a vector, specifically a direction vector. • Actually this is known as the "facing vector". It is the direction of movement if the moved along its local z-axis in the positive direction.
Example • Calculate the vector from the monster to the player, i.e. the location of the player minus the location of the monster. We now have two vectors. If we calculate the dot product of them this will tell us the angle between the vectors. In this manner we can determine whether the monster can currently see the player.
Some uses of the dot product • Showed the values of cos in earlier lecture • cos(0o)=1 • The dot product allows a quick test to see whether two vectors are pointing in the same direction. • cos(90o)=0 • So two vectors are perpendicular only if their dot products are 0.
Some uses of the dot product • Two vectors whose dot product yields 0 are known as orthogonal. • This is very useful in graphics. • You may have heard about “normal mapping”, e.g. in the context of graphics. The normalto a surface is a vector which is orthogonal to a surface. • Note: This is not the sameas talking about a normalisedvector! • More from Laurent next year…
Some uses of the dot product • The dot product of a vector with itself always produces a positive number. This number is equal to the square of the normalised vector.
Summary of the Argument • There is a relationship between angle of a triangle and the length of its edges. • The edge of a triangle can be considered to be a vector. • Two vectors connected together form a triangle. • The relationship between the vectors can be calculated using the dot product. • The dot product of two vectors expresses the angle between them.