Week 3 Day 2 - Linear Algebra for Video Games: Dot Product, Cross Product, Vector Length
Today we started our exploration of math inside of 3D video games. 3D video games are all about vectors and matrices - topics you learn about in a linear algebra class, but I'll give you a simple version here.
Vectors in 3D games are three numbers, written either like this: {10,20,30} or this: (-16,42,10.73)
Your position, velocity, rotation, scale, etc., are usually all held in vectors (often combined together to form a matrix).
What can we do with a vector?
1) Well, we can compute the length of it using the Pythagorean formula, called the "Vector Length" node in UE4. sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2) if you remember that from algebra.
2) You can see if two normalized vectors (a normalized vector has a length of 1) are pointing in roughly the same direction. That is done using something called the dot product: x1x2-y1y2. If the dot product equals 1, then they are pointed the same way, if it equals 0 they are at right angles to each other, and if the dot product is -1 they are pointed opposite to each other. These numbers range smoothly between -1 and 1, with any positive number meaning they're pointed at least vaguely in the same direction, and any negative number meaning they're pointed at least slightly in opposite ways.
The dot product is also the cosine of the angle between the two vectors, so if you want to do something like have a sentry gun target anyone within 45 degrees of where it is looking you do the following:
A) Compute the displacement vector, which tells you how far away and in what direction the target is from the sentry gun. This is done by subtracting the target's position by the sentry gun's position.
B) Normalize the displacement vector. This throws away the range, and just gives you the direction.
C) Dot product the normalized displacement vector with the forward vector of the sentrygun (the way it is looking).
D) If the result is over 0.707 (which is the cosine of 45 degrees) then the target is within a 45 degree arc of the way the sentrygun is looking, and we can start blasting.
3) The scalar cross product is the same as the dot product, except it returns the sin of their angles, so it is useful for figuring out if one vector is to the right or the left of the other vector. One side will give you a positive number, the other side gives you a negative number - which is which depends on which vector you do first, but in general the second vector is to the right if the result is negative, and to the left will give a positive number.
Between vector length, dot product, and cross product, combined with tracelines (that we talked about yesterday) you can do a lot of interesting things, like see if a security guard has line of sight to the player (using line trace by channel), see if the player is within range (using vector length) and see if the player is within 45 degrees of the way the guard is looking (using dot product).
These things are the fundamental building blocks of 3D games, and need to be understood by everyone.
Other Videos By Bill Kerney
Other Statistics
Vector Statistics For Bill Kerney
There are 179 views in 2 videos for Vector. There's close to 3 hours worth of content for Vector published on his channel, making up less than 0.39% of the total overall content on Bill Kerney's YouTube channel.