Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Quick question about magnitude, what exactly is the number?

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!

2 answers

✓ Accepted answer

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.

3 comments

0

Oh, alright. But is the length in studs? Is that the number I'm seeing when I print the magnitude?

0

Yeah

0

Thank you. I just needed to know that.

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

1 comment

0

Wow, thanks for all of the information, Blue. Masta answered my question, though, but I'll give you rep because I'll definitely use this. Thanks ^_^

This discussion is preserved as it was. New answers, comments, and votes are closed.