How does subtracting the position of one object from another give you the distance

I can’t visualise how subtracting the position of one object from the other like (workspace.Part1.Position - workspace.Part2.Position).Magnitude gives you the distance between them. It would be great if you could explain with an infographic of sorts.

2 Likes

Hi there, Deathlios. So with magnitude, you’ll probably learn it in physics. To calculate magnitude, it’s :

sqrt(a^2+b^2+c^2), so… If we subtract the 2, magnitude = (Delta a^2 + Delta b^2 + Delta c^2), so therefore. Taking the difference between 2 objects, and calculating the magnitude gets us the distance.

I hope this helps!

2 Likes

That’s basically what the .Magnitude part of the script is doing. It takes that distance and gives you the difference in studs.

What exactly does the delta here do?

Hi again, Delta is difference, meaning the subtraction of one value to another…

So let’s say I have 2 Xs, x=4 and x=2. Delta X would be 4-2 = 2. That’s all!

1 Like

Think of it like a number line where if you want to get the distance between two numbers, you subtract them from each other. This same concept applies in three dimensional space when we need to find the distance between two objects, we subtract them from each other.

This is more of a math question, but I’ll try to illustrate it to you:

Vocabulary to know:

  1. Origin, (0,0,0) the coordinate at the center of the world, or wherever you put that center “relativity”. But in Roblox there definitely is a center (0,0,0) best not to go too far from it.

  2. Vectors, uhh TL;DR it’s an arrow pointing somewhere (direction) with a length (magnitude)

  3. Position vectors, a vector (arrow) starting from origin then going to the position of an object like a part.

So here is our problem we got two parts A and B and we know their position vectors.

But now we want to know the path from part A to part B. By knowing this path we will know the direction to go and more importantly the distance from part A to part B which is the question here.

So to know the path from A to B, we well start from part A, then using the information we know which is position vectors A and B our only option is to take the path of path A back towards origin.

This will make the position vector A negative as before position vector A is from origin to A, but now we are going from A to origin so it’s negative.

Once we arrive to origin, we can now just go along path B to towards part B.

Thus our path from A to B is = - A + B

Now you might be thinking, hold on if I walked from part A to origin then part B, isn’t that significantly longer than if I walked directly from part A to B?

The answer is, well vectors don’t care about path we took from

  • A to origin to part B,

the only thing that matters is the start position and end position, which vectors get rid off during the mathematical subtraction process so it just becomes.

  • A to B.

Now that we know the path in the from of a vector AB, we take the length of this path using another math operation known as .Magnitude.

That’s for a different topic but tl;dr it’s from Pythagoras theorem.

1 Like

It’s basically a math question. Subtracting vectors will result in a subtraction of their components
e.g.

local r = Vector3.new(5, 8, 1) - Vector3.new(1, 7, 2)  -- (4, 1, -1)

The magnitude is its distance from the origin, which is (0, 0, 0).
It’s calculated with sqrt(x^2 + y^2 + z^2), from the pythagorean theorem