Originally Posted by merlight
Won't fix bad flop performance, but here's a common optimization for distance checks that might help you:
Lua Code:
-- instead of if sqrt((x1-x2)^2 + (y1-y2)^2) < MAX_DIST then ... end -- do local dx = x1 - x2 local dy = y1 - y2 local dist2 = dx*dx + dy*dy if dist2 < MAX_DIST2 then ... end -- MAX_DIST2 = MAX_DIST^2 -- and needs only be computed once if you have fixed MAX_DIST
You don't have to compute sqrt for a distance check, square the threshold instead, it's cheaper. And of course, once you compute the squared distance, memoize it, don't compute it again for another check a few lines below
|
I found a way around without a need to scrap the root, but if I run into another performance hindrance I'll have to resort to that. My problem with distance checks is that when player moves all distances change
and I have to recalculate all of them to make sure I selected the ones I need.