A lot of maths I use tends to be abstracted away either in libraries I use, or inside the application. I’m going to go back to basics starting with vector maths, and moving onto matrices – these, in my opinion are the back bone to doing what we do. I’ll cover from the ground up and then go into some more complex areas: determinants, inverse multiplication, decomposition etc. I’ll be learning a bunch of this stuff along the way. Lets get started:
Vectors
So a vector is basically a direction from the origin. [1, 2, 3] basically means we have a point thats moved 1 in the X direction, 2 in the Y and 3 the Z direction.
Vectors can be added together simply by adding the parts of each together. [1, 2, 3] + [4, 5, 6] = [(1+4), (2+5), (3+6)]. Subtraction follows a similar process.
Vectors can be multiplied against a scalar (float) value by multiplying each part by it: [1, 2, 3] * 5 = [(1*5), (2*5), (3*5)].
We can get the length of a vector by firstly, powering each part by 2, then summing (adding up) these parts, and finally getting the square root of the total. This looks like this len([1, 2, 3]) = sqrt((1^2) + (2^2) + (3^2)).
Using this length we can get the normal of the vector. Normalizing a vector keeps its direction, but its length becomes 1.0. This is important in finding angles, unit vectors and matrix scale. To do this we first get the vectors length, and then divide each part of the vector by it:
normal([1, 2, 3]) =
length = sqrt((1^2) + (2^2) + (3^2))
normal = [1/length, 2/length, 3/length]
The dot product of two 3d vectors (x, y, z), basically returns the magnitude of one vector projected onto another. If we have two vectors [1, 0, 0] and [1, 1, 0]; when we project the latter onto the former, the value along the formers length is the dot product. To get the dot product of two vectors we simply multiply the parts together:
[1, 2, 3] . [4, 5, 6] = (1*4) + (2 * 5) + (3 * 6)
We can use the dot product to get the angle between two vectors too. If we first normalize each vector, we can get the angle by getting the inverse cos (or acos) of this dot. This will return a radian, so we can convert it into degrees by multiplying it by (180 / pi):
cos(norm([1,2, 3] . norm([4, 5, 6]) )^-1 * (180/pi)
Next cross products..
Hey Charles. Thanks for the post . . . love it when people get “back to basics”. I’m always looking for good mathy references . . .
Alas, I think you’ve got a typo though in the dot product formula ([1,2,3]dot[4,5,6] should be (1*4) + (2*5) + (3*6)). You’re missing the +’s 🙂
We’ll spotted Zeth! Thank you, I was in the land of nod probably thinking about the cross product and how it returns a vector and not a single value.
Thanks,
yeah, figured as much. (Definitely wasn’t thinking you didn’t know it:) Thanks again!