Why do I find different lenght of a Vector?

Hey everyone!

I want find the lenght of 2 Vector. But it seems like that magnitude and pythagorean theorem gives different solutions.

local Part1 = game.Workspace.Part1

local Part2 = game.Workspace.Part2

local function Pythagora(Va)

local lenght = math.sqrt(Va.X^2 + Va.Y^2 + Va.Z^2)

return lenght

end

local Part1 = Pythagora(Part1.Position)

local Part2 = Pythagora(Part2.Position)

local lenght = Part2 + Part1

local magnitude = (workspace.Part2.Position - workspace.Part1.Position).Magnitude

print(magnitude)

print(lenght)

Vectors are positions not lengths, I am pretty sure.

What are you trying to achieve?

Vectors can be defined with the pythagorean theorem

Try Cframes instead of vectors

1 Like

image

No look at the picture again

instead of using 2d we can use 3d and that with the z axis

math.sqrt returns the square root of a number

hm and why doesnt the theorem not working in 3d space. a bit confused

Because they are points and not the actual length.

and how can I do that with Cframes?

Ok try using a magnitude

local magnitude = (workspace.Part1.Position - workspace.Part2.Position).Magnitude
print(magnitude)

All you are trying to do is finding the distance between 2 vectors right? so here is the api reference
Vector3 | Roblox Creator Documentation.

I did that. but the question is: Doesnt exist another way to do it?

(workspace.Part1.Position + workspace.Part2.Position)/2

more than that

Hm, I don’t know any other ways about going on this

1 Like

The reason you’re function isn’t working is you’re trying to find the length of the vector from 0,0,0 on two different parts and then adding those lengths together. You forgot to subtract one vector from the other vector before doing Pythagoras Theorem.

function distance(vector1,vector2)
    local difference = vector2 - vector1

    local distance = math.sqrt(difference.X^2 + difference.Y^2 + difference.Z^2)
    
    return distance
end

so the pythagoras theorem can work in 3d?

Yes if you use it correctly. It works in 3 axises.

1 Like

Yes if you use it correctly. It works in 3 axises.

so it didnt work

math.abs(distance) is unnecessary because squareroot is always non-negative.

1 Like

It does not work because here you’re not calculating the same thing as here:

With magnitude you’re calculating the vector from part1 to part2 and with pythagorean you’re calculating the sum of part1.position vector length + part2.position vector length.

In order to get the same results you can do for example:

Pythagora(Part2.Position - Part1.Position)

or

local Part1 = Part1.Position.Magnitude

local Part2 = Part2.Position.Magnitude

local lenght = Part2 + Part1