Magnitude Not Working

I would consider myself an experienced developer, a developer who has worked with magnitude before in the past. I started a new project where I found myself in need of magnitude to find the distance between 2 objects only for it not work, I would have two parts 4 studs away from each other and magnitude would print some crazy number like 45.234532453.

Code:

local distance = (workspace.PartA.Position - workspace.PartB.Position).Magnitude
print(distance)

PartA is at position {0, 0.5, 0} and PartB is at {0, 0.5, 4}, magnitude prints 4.0000000. What the heck. Help please. Thanks.

2 Likes

Magnitude is the distance between 2 objects. As you can see,

Part A is at position {0, 0.5, 0} and Part B is at {0, 0.5, 4}

It prints 4 because that is the amount of studs (distance) between the two objects.

2 Likes

Is this not 2 studs anymore?

1 Like

Can you share your code? I don’t see the error with anything really.

1 Like

I already shared my code, but I found the issue:

I forgot that Roblox added the whole ‘origin’ thing. I just don’t understand how I am supposed to accurately get the distance of objects that are larger or not the same size…

1 Like

That’s how it always worked? Parts are centered around their position, but for models you can use pivots to offset that to your liking

1 Like

Also to answer your question you can try using raycasts to measure the actual gap between two parts, fire one from part A to B and then fire another one from that raycast’s hit position back at part A

2 Likes

I don’t believe so, before origins, I think magnitude would return the distance between the two objects, not their origins; however, I used ray casting to find the objects’ edge positions, rather than their origin positions.

lol I saw this a second after I posted that.

1 Like

That’s not how it works. You are first getting the origin of each part by indexing for the .Position property, and then you are just getting the scalar displacement between the two vectors (origins) by subtracting them and getting the magnitude of the resultant vector
The math is correct, but you’re using it wrong basically

1 Like

Might just be me, but this doesn’t grant enough clarity on the matter.
Im still getting these abnormally large numbers and this message at most gives me places to look.
I have checked forum posts and documentation on function Scalar(distance) and other methods like it and pretty much everyone I’ve seen has no clue how this works.

I had to literally look up the meaning of " scalar " to even remotely understand why this doesnt work.

Scalar means Magnitude, NOT direction; I cant get the difference between two points if all Im indexing is both of their positions, and subtracting them.
thats like saying Im at 1x, 1y , 1z and your position is just an offset of that by 1 stud.

The result would just return 1 in each coordinate ( 3 ) instead of the desired 1 stud away.

So lets assume I understood that part properly, how do you get the difference between two points properly?

My wording was definitely confusing, that’s my fault.

When I said scalar displacement, it was referring to the result after both operations described; subtracting AND getting the magnitude. If you only subtract the vectors, that’s vector displacement because you still end up with a vector. A magnitude operation gives you a scalar value.

This depends on the kind of difference you want; vector or scalar. Displacement is when you just subtract two vectors, the order matters. Distance is the magnitude of the displacement.

If you have 2 vectors, v1, v2, and you want to get the displacement, there’s two ways:

  1. v1 - v2
  2. v2 - v1

You will notice the difference will just be the sign (positive or negative) of the resultant displacement. It can be explained as which vector you’re treating as the “origin” or the “root” vector. The first one assumes v2 to be the origin, because if v2 was actually the origin it would be (0, 0) and the displacement would end up just being v1 itself (this is called the identity property of subtraction)

v1 - Vector3.zero = v1

Another perspective, if you think of v2 as the origin, then adding the displacement to it will give you back v1. This is simple algebra.

v1 - v2 = disp
v2 + disp = v1

Finding most of this out in studio was both enlightening and abit frustrating.
Either I get a negative ( massive number ) when

Part 1 is only a half a stud away from Part 2,

Or a massive positive number, or just flat out 0,0,0; To prevent me going in circles again,

Why does (part1.position, part2.position).Magnitude return a massive number when what you described should return the distance between those two points?
I realize I technically asked this question and you answered, and I still simply don’t get it.

Im gonna try one last thing in workspace, I wonder if my problem isn’t this.

Im using an explosion to impact multiple parts ( no joint destruction ), using Explosion.Hit and taking its BlastRadius.

Expos is the position of the explosion’s origin.
Parpos is the position of a part.

now I thought, if I only spawn the explosion on impact of a part ( meaning its distance shouldnt be all that much )

My math is the following:

local result = (Parpos - Expos).Magnitude
if result <= BlastRadius then
…

My logic is, if the result’s distance is within that of the blast radius, then it should work, but in practice it does not, I either get a massive negative number, positive ( that id need to absorbently divide to get a reasonable number ), or literally 0/nil.
I get different results as I change the order of everything, it doesn’t really play out in my head that any of this would be incorrect.

I looked on one forum where they used the DOT of two parts and someone started bringing up Trigonometry to get an offset and although I know trig, I did not want to touch that, especially when the example given on roblox’s Forum on how to use explosions never mentions it.

( by the way I tried to use roblox’s sample, it doesn’t work, it’s also lengthy and I knew it could be compacted with magnitude, if I could ever get it to work. )
It’s a shame I don’t understand how to use it, because that leaves only one method of doing this and that’s manually get distances from an explosion to deal drop-off damage.

100 on the center ( <= 1 )
70 next to ( <= 8 )
20… so on and so forth; its bad practice and amateurish because Id have to use an if and elseif statement for each distance when I could use a math operator to do this calculation for me.

Feel free to send a repro place file. This is either a bug, or something we’re both completely misunderstanding. You should also create a new thread to cover your specific issue instead of using someone else’s.

I will say that it is impossible for .Magnitude to return a negative number. The way magnitude is calculated uses a squareroot, which can never return a negative number. Complex numbers are not a thing in Lua. It is also impossible for it to return nil because it has to return a number, even a malformed one such as NaN. You probably did something else that caused the code to proc a negative/nil value and the issue did not stem from Magnitude itself.

The order indeed does not matter, shown here:

And this is the result if you do a magnitude operation on a malformed vector:
image

Result if you do a magnitude operation on an empty vector:
image

Result if you get the magnitude of the unit vector:
image

Result if you get the magnitude of the negative unit vector:
image