Vmath.length_sqr returning strange result

I created a minimum repro example to demo the issue:

I am attempting to calculate the distance squared between to vmath.vec3 points and it’s returning an extremely large value. EDIT: Removed WolframAlpha link since I put in the wrong points after creating the repro =X

vmath.length_sqr takes only one argument and returns the square of the length of that argument. The argument may be a vector or a quaternion. You are passing a pair of vectors instead…

If I understand correctly what you are trying to do, you should pass the difference of the two vectors to length_sqr.

I may guess that in your code you are just computing the squared length of the first argument.

1 Like

Ah thank you, I’m surprised no error is generated in this case. However changing it to subtraction instead of two parameters is yielding 537.03527832031. This is still pretty far away from 55^2 which is 3025… I’m probably just sleep deprived :stuck_out_tongue:

We do not check for extra arguments being passed to any Defold API function. We only check the arguments we actually use.

Ah I see. Is there some reason for this? It seems like it would prevent some mistakes while writing code.

Could you please paste your code here, please? I would be EXTREMELY surprised if there was an error in a so low level vmath method…

local dist = vmath.length_sqr(vmath.vector3(511.37414550781, 851.29266357422, 0) - vmath.vector3(488.44427490234, 847.93762207031, 0))
pprint(dist)

Doing everything by hand I get about 545…

(x_1 - x_0)^2 + (y_1 - y_0)^2 + (z_1 - z_0)^2 = 23^2 + 4^2 + 0^2 = 545

1 Like

Hmm, yeah I guess it’s pretty close to that. The engine is giving me 537.03527832031 for the two points shown in the code above.

We generally don’t do defensive programming.
Each little extra check that gets added will cost everyone some precious bytes/kilobytes, and it all adds up in the end.

2 Likes

I see, thanks I was thinking that might be the case. However, is this compile times we’re speaking of? If not, couldn’t the compiler check and then disregard the arguments after some validation?

If you mean a linter, that would make a lot of sense, yes. We’ve talked about it before, but haven’t added something built in for that.
You can probably use the save hook in the editor scripting to perform linting on your code. Not sure if anyone already has implemented that.

There isn’t any mention of a save hook in the editor script manual.

I guess I was mistaken (probably due to the fact that we’ve talked about it so many times), sry.

You should be able to loop over your resources at build time at least.

1 Like

I think @aglitchman posted something like this a while back.