Alright, to begin with, I'm going to discuss what magnitude is and how it works, and then I will move on to discuss some application in the Roblox world. I will use two dimensional vectors to begin with, because they are easier to explain. All the skill you will need for this tutorial is basic geometry and a knowledge of the Pythagorean theorem
Quoting from a book called Mathematics From the Birth of Numbers: "Geometrically, a vector is represented by an arrow (a line segment) with a specific direction, its length corresponding to the magnitude of the vector." Here we have something to go on: the length of the vector is it's magnitude. Here I have an image of a vector
The magnitude is clearly seen as the distance from (0, 0) to (2, 1) because of the definition we saw earlier ("its length corresponding to the magnitude of the vector."). That distance is the hypotenuse of the triangle that has a base of two and a height of one. Thus, to solve for the magnitude of this two dimensional vector, we just have to use the Pythagorean theorem. a^2 + b^2 = c^2, where c is the hypotenuse. So the magnitude of this simple two dimensional vector is math.sqrt(2^2 + 1^2).
Ok, so now we have an idea of how to calculate the magnitude of a two dimensional vector. What if we wanted to get the distance between two different two dimensional vectors? Well, first we have to create a new, two dimensional vector whose magnitude is the distance between those two vectors. We can do this by subtracting the first vector from the second. Quoting the book again: "Geometrically the difference between the two vectors a and b is attained by translating the vector (-b) so as to make its initial point coincide with the terminal point of vector a, and apply the triangle law of addition." This is all very complex sounding, so a visual would be helpful. Here is what the triangle law of addition looks like
and here is what the triangle law of subtraction looks like. Really, it is using the triangle law of addition, but with the additive inverse of b (or negative b)
Now that we have seen how to subtract and add vectors graphically, let us do it algebraically. If you are adding, just add the two different x and y values together (if you are subtracting, just subtract the two different x and y values). If we two vectors, (1, 3) and (3, 5) and we wanted to add them, the resulting vector would be (1+3, 3+5) or (4, 8 ). The same goes for subtracting. If we wanted to subtract (1, 3) from (3, 5), the resulting vector would be (3-1, 5-3) 0r (2, 2). That's not too bad, is it?
Now that we have a base for adding and subtracting vectors, let's get back to my earlier question: "What if we wanted to get the distance between two different two dimensional vectors?" Well, as I said earlier, we need to subtract the first vector from the second, and get the magnitude (length) of the resulting vector. Assume that these are our two different vectors
(2, 2) and (5, 1) if we subtract (2, 2) from (5, 1) we get (5-2, 1-2) or (3, -1). Graphically, to do this subtraction problem, we would shift the red vector (2, 2) over so that it's pointing down and to the left (it's pointing down and to the left because remember (where b is the (2, 2) vector), it is the additive inverse of b (aka negative b) and b was originally pointing up and to the right) and its starting point is the ending point of the second vector (5, 1). Here is what it looks like
and the resultant vector looks like this (it is the green one)
Finally, we must get the magnitude of this resultant vector to see what the distance between the two vectors is. Again, we must use the Pythagorean theorem. The base of the triangle whose hypotenuse is the magnitude of the green vector is three units long, and the height of it is one unit long. So, using the theorem, our answer is math.sqrt(3^2 + 1^2).
The only thing remaining to do is finding out how to do this with three dimensional vectors, and you are good to go. As you know, in Roblox, you can easily create a three dimensional vector using local v = Vector3.new(x, y, z). The only difference between a three dimensional vector and a two dimensional vector, is that there is an additional axis, the z axis. For our purposes, I will be using this simple diagram for the remainder of the tutorial
As you can see, there is one additional axis, the z axis. Again, we see a triangle whose hypotenuse is the magnitude of the vector, p. Now, there are two unknowns: the base and the hypotenuse. To find the base, we need to realize that the distance from O to where the lines originating at the three and two meet is also a Pythagorean theorem problem. The square, when divided in two by that line originating at O, is two triangles and the base is two units and the height is three units for both triangles. So, the length of the hypotenuse for those triangles is math.sqrt(2^2 + 3^2). Now that we know the base, we can find the magnitude of the vector. The base of the triangle is math.sqrt(2^2 + 3^2) and the height is five. Putting those together, we get P.magnitude = math.sqrt(math.sqrt(2^2 + 3^2)^2 + 5^2) or math.sqrt(2^2 + 3^2 + 5^2). This logic can be applied to all 3d vectors, so the magnitude of any 3d vector is math.sqrt(x^2 + y^2 + z^2).
The last element to this tutorial is on the most common usage of .magnitude. Usually (in Roblox), this is used to find the distance between two positions, such as a player and a player or a player and a treasure/interaction object of some sort. I have already covered adding and subtracting vectors, but I will add that the same methods are applied when adding or subtracting three dimensional vectors. For instance, if I want to subtract (3, 6, 2) from (5, 2, 7) you will get (5-3, 2-6, 7-2) or (2, -4, 5). So, getting the magnitude (or distance) between to objects is simple (using the concepts we have already covered). We subtract the first position from the second to get the vector that has a magnitude that is the distance between those two positions (or vectors), we solve for that magnitude, and we return it. Here is what a function might look like:
local function getDistance(pointOne, pointTwo) local newVector = pointOne - PointTwo local magnitude = math.sqrt(newVector.x^2 + newVector.y^2 + newVector.z^2) return magnitude end
Similarly, the Roblox implementation can be used like this:
local distance = (posOne - posTwo).magnitude
I hope this tutorial helps your understanding of .magnitude. If you have any additional questions, feel free to respond and I will do my best to answer them. This is my first tutorial, so please comment on how I could do better in the future. Have a great day scripting, Phlegethon5778