Log In  


Hi everyone,

I am using Pythagoras to work out the distance between two points. Unfortunately I am looking to work out the distance between two points that are quite a long way away from each other so I am finding that dxdx + dydy is pushing me over the 32k number limit.

Is there any way I can get around this?

Thanks



You could choose a different unit for your coordinates. For instance, it’s pretty common to count in map units, where everything is divided by 8 (and multiplied by 8 before displaying).

Or you could pre-divide things to avoid the overflow:

dist = sqrt((x >> 8) * x + (y >> 8) * y) << 4

How accurate do you need to be? You might get away with an approximation:

https://en.wikibooks.org/wiki/Algorithms/Distance_approximations

I had this issue recently, but as I only needed to check if a distance was less than some value I just returned false if dx or dy were greater than the value, and only then bothered with Pythagoras.


@samhocevar unfortunately the game I'm working on isn't map based, however I will give the pre-divide thing a go, I take it that basically just divides the numbers before squaring and then multiplies them again?

@Two_Owls not super accurate however I do need a distance not just a case of checking if a distance meets a condition. I'll give your link a look thank you!



[Please log in to post a comment]