So I've learned about magnitude and how it works, and I've inputted the following script:
mag = (game.Workspace.Part1.Position - game.Workspace.Part2.Position).magnitude print(mag)
Now, when I print the magnitude, my questions, what is the number that is being displayed in the output after I play? Is it the distance between their X value, or all of them combined?
Like I said, just a quick question. Much thanks. Cheers!
It is the distance from one point (Vector2 or Vector3) to another.
The best way to visualize it is to imagine a straight line being drawn from the centre of one block to the centre of the other. The length of this line is the magnitude.
The difference of two vectors is called the displacement between them (the vector getting from one point to another).
Displacement from B
to A
is just A - B
.
Vector subtraction, like vector addition, is just coordinate-wise, that is
-- for Vectors A and B... AminusB = Vector3.new( a.x - b.x, a.y - b.y, a.z - b.z ) print(AminusB) print(A - B) -- the same thing
In your example, you are asking for the magnitude (length) of the displacement. Since the units of the positions are Studs, the units of the displacement, and consequently the magnitude, are also studs.
print(Aminus.magnitude) -- distance between A and B
In particular, magnitude (in real, three dimensions, in Euclidean space) is computed like this:
magA = math.sqrt(a.x ^2 + a.y ^ 2 + a.z ^ 2) print(magA) print(A.magnitude) -- same
Hence the full
print( (A - B).magnitude ) print( math.sqrt( (A.x - B.x)^2 + (A.y - B.y)^2 + (A.z - B.z)^2 ) ) -- Same