Getting Negative Values of Magnitude

My program does a bunch of things based on the magnitude of the difference of 2 Vector3s that I have written something like this:

local magOfDiff = (vectorVar1 - vectorVar2).Magnitude

The problem now is that I need to include negative values (ie. when vectorVar2 goes in 1 direction, magOfDiff becomes negative, but becomes positive in the other way), but the Magnitude property of Vector3s don’t do that. Kinda like a scalar value in physics, if that makes sense. Does anyone know a way to achieve a similar effect?

You could handwrite a vector that’s looking in the direction in which your scalar value would be positive, and then the scalar product of your vector and your handwritten one would either get you a positive or negative value based on the direction of the vector you’re testing.
When you know the sign of the scalar product, simply multiply by 1 or -1 the vector.Magnitude.

I wasn’t aware that Vector3s have a direction. The only things I could find about it in the reference, though, are not related to controlling that direction. How do I set a Vector3’s direction?

Basically, the vectors (0,1,0) and (0,-1,0) would be two vertical vectors that have the same magnitude (1), but they don’t face in the same direction (I believe (0,1,0) faces upwards while (0,-1,0) faces downwards).

Actually, I got ahead of myself. The direction itself would be changing on a case-by-case basis. I can use FromNormalID to grab the direction, but that only grabs it in the local space. Is there a way to achieve this in the global space? Or maybe there’s a way to get it based on local space?

local world_Space = CFrame.new(the FromNormalID vector):ToWorldSpace()

Okay, I actually found a solution, though it’s so specific that I doubt anyone will be able to actually use it in the future.

The way I got vectorVar2 involves a bit of linear algebra. Specifically, to find a point on a ray, I used the sum of the origin and the product of the direction and a variable. That variable is negative whenever I drag behind the origin. All I had to do was extract that out of the module script and multiply the magOfDiff by the sign of that variable.

So, if someone in the future happens to be using linear algebra, here you go.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.