Cube-like magnitude distance

how would i create a distance script that doesnt get the distance in a sphere, but in a cube?

example:
the pink dot and the yellow dot would be the exact same distance from the center

image

i dont want do use a sphere distance script like this

(vectorB-vectorA).Magnitude

using this on the pink and yellow dots positions wont return the same distance
how would this “cubed distance” thing work?

My first thought was trying to inscribe a cube around a sphere but then I just realized that there is a much simpler solution.
So instead of just using (CurrentPos-InitalPos).Magnitude, which would give you a sphere, you can just check each axis, and use whichever one is bigger.

Something like:

local DeltaPosition = CurrentPos - InitalPos -- Get the delta
local DeltaX = math.abs(DeltaPosition.X)
local DeltaZ = math.abs(DeltaPosition.Z)

local DistanceFromCenter
	if DeltaX > DeltaZ then -- check for which is bigger and use that for the distance.
		DistanceFromCenter = DeltaX
	else
		DistanceFromCenter = DeltaZ
	end
	
print(DistanceFromCenter)

This doesn’t check for your Y position, but you can add that into the conditional if you really need to.
Also, correct me if I’m wrong but I think the original DOOM had this exact method for explosion checks too.

1 Like