The following warnings occurred:
Warning [2] Undefined array key 0 - Line: 1677 - File: showthread.php PHP 8.2.31 (Linux)
File Line Function
/inc/class_error.php 157 errorHandler->error
/showthread.php 1677 errorHandler->error_callback
/showthread.php 916 buildtree




Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Line of sight
#4
There's a much faster distance formula, which doesn't require square roots, albeit it's not as precise, but it works. It's what I use in my games - I can't afford a square root per frame so I just...

Code:
Function distance (x1 as uByte, y1 as uByte, x2 as uByte, y2 as uByte) dim dx as uByte dim dy as uByte dim mn as uByte dx = Abs (x2 - x1) dy = Abs (y2 - y1) If dx < dy Then mn = dx Else mn = dy End If Return (dx + dy - (mn >> 1) - (mn >> 2) + (mn >> 4)) End Function

Roughly translated from C code, but should work:

Code:
unsigned char distance (unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2) { unsigned char dx = abs (x2 - x1); unsigned char dy = abs (y2 - y1); unsigned char mn = dx < dy ? dx : dy; return (dx + dy - (mn >> 1) - (mn >> 2) + (mn >> 4)); }
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)