Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Quick question about magnitude, what exactly is the number?

Asked by
bloxxyz 274 Moderation Voter
9 years ago

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

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

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.

0
Oh, alright. But is the length in studs? Is that the number I'm seeing when I print the magnitude? bloxxyz 274 — 9y
0
Yeah BlackJPI 2658 — 9y
0
Thank you. I just needed to know that. bloxxyz 274 — 9y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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
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 ^_^ bloxxyz 274 — 9y

Answer this question