JS method for getting distance between coords (like #(coords - coords) in lua)

Hello there,
I recently found a way to get a better function for calculating distances between two coords. I think it is no secret to any developer, that the default function GetDistanceBetweenCoords is a very underperforming native, so here is what I use now :

function calcDist(start_x, start_y, start_z, target_x, target_y, target_z) {
  var a = start_x - target_x;
  var b = start_y - target_y;
  var c = start_z - target_z;

  return Math.sqrt(a * a + b * b + c * c)
}

Hope you enjoy it and just watch if and how your script performance gets better !
Juli3n

3 Likes

Unneeded overhead with js runtime on client side, have you tested this ?

like what?
just use this lol
dist = #(Vector3 - Vector3)

1 Like

This doesn`t work in JavaScript…

Yes I tested it and on my scripts the performance was better with this instead of the native

1 Like

ahh bro nevermind i didnt see the javascript xD

I thought this argument was long since over. You’re talking about micro seconds choose what runtime makes you the most productive, stop with this stupid overhead shit.

You can also use Math.hypot.

export const getDistance = (c1: number[], c2: number[]) => {
	return Math.hypot(c1[0] - c2[0], c1[1] - c2[1], c1[2] - c2[2]);
};

Both statements, yours and OP, are simply feelings and not facts at this moment. I didnt come here to start an argument, nor be disrespectful. I just asked if this was tested as stated “Hope you enjoy it and just watch if and how your script performance gets better !”.

:person_shrugging:

This will be better then a native invoke, yes, working with native math libraries will be faster then calling the native, this would be exactly the same as using #(vec1 - vec2) in lua, which is calling its native math library.

Native invokes for constant calls will always be out performed by something that the runtime can optimize.

Sorry that I came off a bit hostile, the overhead argument gets quite old after a very long time, we’re measuring in microseconds here, a 0.03 microsecond overhead for ticks is not noticeable by the lay person, and if the JS tick gets put behind the actual runtime (like how Lua and mono_v2 runtime have) there will be no noticeable overhead.

1 Like