Not really sure if this was mentioned anywhere, but I thought it might be useful for anyone who wants to make use of vector math, specifically calculating the length of a 2D vector, while minimizing the risk of overflow.
Because the sqrt(32767) ~= 181, it is easy to overflow when using the textbook equation for calculating vector length:
len = sqrt(x*x + y*y) |
To derisk overflowing when squaring the terms, you can first scale down the vector's x and y components:
m = max(x,y) x = x / m y = y / m |
then scale the square root of their sums:
len = sqrt(x*x + y*y) * m |
Lua code (simplified since one term will always == 1):
function length(v) local d = max(abs(v[1]),abs(v[2])) local n = min(abs(v[1]),abs(v[2])) / d return sqrt(n*n + 1) * d end |
Started work on a game based loosely on "jumpman" called "HighSteal".
I should be done in a few days... ok, maybe a week or so.
So far it is coming along really well. My original though was to create a 2-player game, and if there was time and space, add a single player mode.
I was so glad today when I upgraded to the latest pico-8 and found the code char limit had been doubled!!! Thank you!!!
So I am now working on the single player mode, and I think it will work out well.