Magnitude

From Legacy Roblox Wiki
Jump to navigationJump to search

Magnitude is a term used across mathematics that means several things. In vector theory, the magnitude of a vector is the length of a vector.

The length of a vector v is calculated using Pythagoras as follows:

|(x, y)| = sqrt(x2 + y2)

Or in 3 dimensions,

|(x, y, z)| = sqrt(x2 + y2 + z2)

Distance between points

Diagram of two line segments using Cartesian Coordinates

Often, it is useful to find the distance between two points.

Let's use the blue line for example. We have a vector that starts at point (1, -5) and ends at point (3, -3). Firstly, we need to subtract one from the other to get the line vector. This gives us (2, 2).

Now, if we imagine a vertical line coming from (2,2) to the x-axis, a right triangle is formed, with a from the origin, and Pythagorus' theorem can be used to find the length of the hypotenuse:

 a2 + b2 = c2

Commonly, in ROBLOX and other situations in need of finding the distance between two points, it is also referred to as the distance formula. ROBLOX has a special property for Vector3 and Vector2 values that does the math for finding the magnitude of the value for you. Here is how it can be indexed:

SomeVector3Value.magnitude
or
Something.Position.magnitude
or
Something.Size.magnitude

As you can see, any Vector3 has the magnitude property (this also includes Vector2 values). Now the simplest way to find the distance between two points (parts in this case) in ROBLOX is to run the following code (edit to fit your needs):

local magnitude = (part1.Position - part2.Position).magnitude
print(magnitude)