DEADSOFTWARE

mempool is optional now
[d2df-sdl.git] / src / game / g_grid.pas
index 38ee53b79abbe55c6f70e70852689050e10b73a0..27fa615fba297bda231768783ed094bbdc579155 100644 (file)
   {.$DEFINE D2F_DEBUG_MOVER}
 {$ENDIF}
 {.$DEFINE GRID_USE_ORTHO_ACCEL}
+{$DEFINE LINEAABB2}
 unit g_grid;
 
 interface
 
+{$IFDEF USE_MEMPOOL}
+uses
+  mempool;
+{$ENDIF}
+
 const
   GridTileSize = 32; // must be power of two!
 
 type
   TBodyProxyId = Integer;
 
-  generic TBodyGridBase<ITP> = class(TObject)
+  generic TBodyGridBase<ITP> = class{$IFDEF USE_MEMPOOL}(TPoolObject){$ENDIF}
   public
     type TGridQueryCB = function (obj: ITP; tag: Integer): Boolean is nested; // return `true` to stop
     type TGridRayQueryCB = function (obj: ITP; tag: Integer; x, y, prevx, prevy: Integer): Boolean is nested; // return `true` to stop
@@ -249,12 +255,10 @@ type
   private
     wx0, wy0, wx1, wy1: Integer; // window coordinates
     stx, sty: Integer; // "steps" for x and y axes
-    dx2, dy2: Integer; // "double lengthes" for x and y axes
+    stleft: Integer; // "steps left"
+    err, errinc, errmax: Integer;
     xd, yd: Integer; // current coord
-    e: Integer; // "error" (as in bresenham algo)
-    term: Integer; // end for xd (xd = term: done)
-    //xptr, yptr: PInteger;
-    xyswapped: Boolean; // true: xd is y
+    horiz: Boolean;
 
   public
     // call `setyp` after this
@@ -278,39 +282,15 @@ type
     // move to next tile; return `true` if the line is complete (and walker state is undefined then)
     function stepToNextTile (): Boolean; inline;
 
-    // hack for line-vs-aabb; NOT PROPERLY TESTED!
-    procedure getPrevXY (out ox, oy: Integer); inline;
-
-    // current coords
-    function x (): Integer; inline;
-    function y (): Integer; inline;
-
     procedure getXY (out ox, oy: Integer); inline;
 
-    // move directions; always [-1..1] (can be zero!)
-    function dx (): Integer; inline;
-    function dy (): Integer; inline;
+  public
+    // current coords
+    property x: Integer read xd;
+    property y: Integer read yd;
   end;
 
 
-// you are not supposed to understand this
-// returns `true` if there is an intersection, and enter coords
-// enter coords will be equal to (x0, y0) if starting point is inside the box
-// if result is `false`, `inx` and `iny` are undefined
-function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
-
-// sweep two AABB's to see if and when they are overlapping
-// returns `true` if collision was detected (but boxes doesn't overlap)
-// u1 and u1 has no sense if no collision was detected
-// u0 = normalized time of first collision (i.e. collision starts at myMove*u0)
-// u1 = normalized time of second collision (i.e. collision stops after myMove*u1)
-// hitedge for `it`: 0: top; 1: right; 2: bottom; 3: left
-// enter/exit coords will form non-intersecting configuration (i.e. will be before/after the actual collision)
-function sweepAABB (mex0, mey0, mew, meh: Integer; medx, medy: Integer; itx0, ity0, itw, ith: Integer;
-                    u0: PSingle=nil; hitedge: PInteger=nil; u1: PSingle=nil): Boolean;
-
-function distanceSq (x0, y0, x1, y1: Integer): Integer; inline;
-
 procedure swapInt (var a: Integer; var b: Integer); inline;
 //function minInt (a, b: Integer): Integer; inline;
 //function maxInt (a, b: Integer): Integer; inline;
@@ -319,7 +299,7 @@ procedure swapInt (var a: Integer; var b: Integer); inline;
 implementation
 
 uses
-  SysUtils, e_log, g_console, utils;
+  SysUtils, e_log, g_console, geom, utils;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
@@ -328,8 +308,6 @@ procedure swapInt (var a: Integer; var b: Integer); inline; var t: Integer; begi
 //function minInt (a, b: Integer): Integer; inline; begin if (a < b) then result := a else result := b; end;
 //function maxInt (a, b: Integer): Integer; inline; begin if (a > b) then result := a else result := b; end;
 
-function distanceSq (x0, y0, x1, y1: Integer): Integer; inline; begin result := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0); end;
-
 
 // ////////////////////////////////////////////////////////////////////////// //
 constructor TLineWalker.Create (minx, miny, maxx, maxy: Integer);
@@ -346,415 +324,226 @@ begin
   wy1 := maxy;
 end;
 
-function TLineWalker.done (): Boolean; inline; begin result := (xd = term); end;
-
-function TLineWalker.step (): Boolean; inline;
-begin
-  if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
-  xd += stx;
-  result := (xd = term);
-end;
-
-function TLineWalker.stepToNextTile (): Boolean; inline;
+function TLineWalker.setup (x0, y0, x1, y1: Integer): Boolean;
 var
-  ex, ey: Integer;
-  xwalk, ywalk, wklen: Integer; // to the respective edges
-  lstx, lsty, lterm: Integer;
-  le, ldx2, ldy2: Integer;
-  lxd, lyd: Integer;
-  f: Integer;
+  sx0, sy0, sx1, sy1: Single;
 begin
-  result := false;
-
-  lstx := stx;
-  lsty := sty;
-  lterm := term;
-  lxd := xd;
-
-  // ortho?
-  if (lsty = 0) then
-  begin
-    // only xd
-    //assert(lsty <> 0);
-    if (lstx < 0) then
-    begin
-      // xd: to left edge
-      xd := (lxd and (not (TileSize-1)))-1;
-      result := (lxd <= lterm);
-      exit;
-    end
-    else
-    begin
-      // xd: to right edge
-      xd := (lxd or (TileSize-1))+1;
-      result := (lxd >= lterm);
-      exit;
-    end;
-  end;
-
-  // not ortho
-  //assert(lstx <> 0); // invariant
+  if (wx1 < wx0) or (wy1 < wy0) then begin stleft := 0; xd := x0; yd := y0; result := false; exit; end;
 
-  lyd := yd;
-  le := e;
-  ldx2 := dx2;
-  ldy2 := dy2;
-
-  // calculate xwalk
-  if (lstx < 0) then
+  if (x0 >= wx0) and (y0 >= wy0) and (x0 <= wx1) and (y0 <= wy1) and
+     (x1 >= wx0) and (y1 >= wy0) and (x1 <= wx1) and (y1 <= wy1) then
   begin
-    ex := (lxd and (not (TileSize-1)))-1;
-    xwalk := lxd-ex;
+    result := true;
   end
   else
   begin
-    ex := (lxd or (TileSize-1))+1;
-    xwalk := ex-lxd;
+    sx0 := x0; sy0 := y0;
+    sx1 := x1; sy1 := y1;
+    result := clipLine(sx0, sy0, sx1, sy1, wx0, wy0, wx1, wy1);
+    if not result then begin stleft := 0; xd := x0; yd := y0; exit; end;
+    x0 := trunc(sx0); y0 := trunc(sy0);
+    x1 := trunc(sx1); y1 := trunc(sy1);
   end;
 
-  // calculate ywalk
-  if (lsty < 0) then
+  // check for ortho lines
+  if (y0 = y1) then
   begin
-    ey := (lyd and (not (TileSize-1)))-1;
-    ywalk := lyd-ey;
+    // horizontal
+    horiz := true;
+    stleft := abs(x1-x0)+1;
+    if (x0 < x1) then stx := 1 else stx := -1;
+    sty := 0;
+    errinc := 0;
+    errmax := 10; // anything that is greater than zero
   end
-  else
+  else if (x0 = x1) then
   begin
-    ey := (lyd or (TileSize-1))+1;
-    ywalk := ey-lyd;
-  end;
-
-  while true do
+    // vertical
+    horiz := false;
+    stleft := abs(y1-y0)+1;
+    stx := 0;
+    if (y0 < y1) then sty := 1 else sty := -1;
+    errinc := 0;
+    errmax := 10; // anything that is greater than zero
+  end
+  else
   begin
-    // in which dir we want to walk?
-    if (xwalk <= ywalk) then wklen := xwalk else wklen := ywalk;
-    // walk x
-    if (lstx < 0) then
+    // diagonal
+    if (abs(x1-x0) >= abs(y1-y0)) then
     begin
-      lxd -= wklen;
-      if (lxd <= lterm) then begin xd := lxd; result := true; exit; end;
+      // horizontal
+      horiz := true;
+      stleft := abs(x1-x0)+1;
+      errinc := abs(y1-y0)+1;
     end
     else
     begin
-      lxd += wklen;
-      if (lxd >= lterm) then begin xd := lxd; result := true; exit; end;
+      // vertical
+      horiz := false;
+      stleft := abs(y1-y0)+1;
+      errinc := abs(x1-x0)+1;
     end;
-    // walk y
-    for f := 1 to wklen do if (le >= 0) then begin lyd += lsty; le -= ldx2; end else le += ldy2;
-    if (lxd = ex) or (lyd = ey) then break;
-    xwalk -= wklen; if (xwalk = 0) then xwalk := TileSize;
-    ywalk -= wklen; if (ywalk = 0) then ywalk := TileSize;
+    if (x0 < x1) then stx := 1 else stx := -1;
+    if (y0 < y1) then sty := 1 else sty := -1;
+    errmax := stleft;
   end;
-  //assert((xd div TileSize <> lxd div TileSize) or (yd div TileSize <> lyd div TileSize));
-  xd := lxd;
-  yd := lyd;
-  e := le;
+  xd := x0;
+  yd := y0;
+  err := -errmax;
 end;
 
-// NOT TESTED!
-procedure TLineWalker.getPrevXY (out ox, oy: Integer); inline;
+function TLineWalker.done (): Boolean; inline; begin result := (stleft <= 0); end;
+
+// true: done
+function TLineWalker.step (): Boolean; inline;
 begin
-  //writeln('e=', e, '; dx2=', dx2, '; dy2=', dy2);
-  if xyswapped then
+  if horiz then
   begin
-    if (e >= 0) then ox := yd-sty else ox := yd;
-    oy := xd-stx;
+    xd += stx;
+    err += errinc;
+    if (err >= 0) then begin err -= errmax; yd += sty; end;
   end
   else
   begin
-    if (e >= 0) then oy := yd-sty else oy := yd;
-    ox := xd-stx;
+    yd += sty;
+    err += errinc;
+    if (err >= 0) then begin err -= errmax; xd += stx; end;
   end;
+  Dec(stleft);
+  result := (stleft <= 0);
 end;
 
-function TLineWalker.x (): Integer; inline; begin if xyswapped then result := yd else result := xd; end;
-function TLineWalker.y (): Integer; inline; begin if xyswapped then result := xd else result := yd; end;
-procedure TLineWalker.getXY (out ox, oy: Integer); inline; begin if xyswapped then begin ox := yd; oy := xd; end else begin ox := xd; oy := yd; end; end;
-
-function TLineWalker.dx (): Integer; inline; begin if xyswapped then result := stx else result := sty; end;
-function TLineWalker.dy (): Integer; inline; begin if xyswapped then result := sty else result := stx; end;
-
-function TLineWalker.setup (x0, y0, x1, y1: Integer): Boolean;
-  procedure swapInt (var a: Integer; var b: Integer); inline; begin a := a xor b; b := b xor a; a := a xor b; end;
+// true: done
+function TLineWalker.stepToNextTile (): Boolean; inline;
 var
-  dsx, dsy: Integer; // "lengthes" for x and y axes
-  rem: Integer;
-  xfixed: Boolean;
-  temp: Integer;
+  ex, ey: Integer;
+  xwalk, ywalk, wklen: Integer; // to the respective edges
+  f: Integer;
 begin
   result := false;
-  xyswapped := false;
 
-  // horizontal setup
-  if (x0 < x1) then
-  begin
-    // from left to right
-    if (x0 > wx1) or (x1 < wx0) then exit; // out of screen
-    stx := 1; // going right
-  end
-  else
+  if (stleft < 2) then begin result := true; exit; end; // max one pixel left, nothing to do
+
+  // strictly horizontal?
+  if (sty = 0) then
   begin
-    // from right to left
-    if (x1 > wx1) or (x0 < wx0) then exit; // out of screen
-    stx := -1; // going left
-    x0 := -x0;
-    x1 := -x1;
-    wx0 := -wx0;
-    wx1 := -wx1;
-    swapInt(wx0, wx1);
+    // only xd
+    if (stx < 0) then
+    begin
+      // xd: to left edge
+      ex := (xd and (not (TileSize-1)))-1;
+      stleft -= xd-ex;
+    end
+    else
+    begin
+      // xd: to right edge
+      ex := (xd or (TileSize-1))+1;
+      stleft -= ex-xd;
+    end;
+    result := (stleft <= 0);
+    xd := ex;
+    exit;
   end;
 
-  // vertical setup
-  if (y0 < y1) then
-  begin
-    // from top to bottom
-    if (y0 > wy1) or (y1 < wy0) then exit; // out of screen
-    sty := 1; // going down
-  end
-  else
+  // strictly vertical?
+  if (stx = 0) then
   begin
-    // from bottom to top
-    if (y1 > wy1) or (y0 < wy0) then exit; // out of screen
-    sty := -1; // going up
-    y0 := -y0;
-    y1 := -y1;
-    wy0 := -wy0;
-    wy1 := -wy1;
-    swapInt(wy0, wy1);
+    // only xd
+    if (sty < 0) then
+    begin
+      // yd: to top edge
+      ey := (yd and (not (TileSize-1)))-1;
+      stleft -= yd-ey;
+    end
+    else
+    begin
+      // yd: to bottom edge
+      ey := (yd or (TileSize-1))+1;
+      stleft -= ey-yd;
+    end;
+    result := (stleft <= 0);
+    yd := ey;
+    exit;
   end;
 
-  dsx := x1-x0;
-  dsy := y1-y0;
+  // diagonal
 
-  if (dsx < dsy) then
+  // calculate xwalk
+  if (stx < 0) then
   begin
-    xyswapped := true;
-    //xptr := @yd;
-    //yptr := @xd;
-    swapInt(x0, y0);
-    swapInt(x1, y1);
-    swapInt(dsx, dsy);
-    swapInt(wx0, wy0);
-    swapInt(wx1, wy1);
-    swapInt(stx, sty);
+    ex := (xd and (not (TileSize-1)))-1;
+    xwalk := xd-ex;
   end
   else
   begin
-    //xptr := @xd;
-    //yptr := @yd;
-  end;
-
-  dx2 := 2*dsx;
-  dy2 := 2*dsy;
-  xd := x0;
-  yd := y0;
-  e := 2*dsy-dsx;
-  term := x1;
-
-  xfixed := false;
-  if (y0 < wy0) then
-  begin
-    // clip at top
-    temp := dx2*(wy0-y0)-dsx;
-    xd += temp div dy2;
-    rem := temp mod dy2;
-    if (xd > wx1) then exit; // x is moved out of clipping rect, nothing to do
-    if (xd+1 >= wx0) then
-    begin
-      yd := wy0;
-      e -= rem+dsx;
-      //if (rem > 0) then begin Inc(xd); e += dy2; end; //BUGGY
-      if (xd < wx0) then begin xd += 1; e += dy2; end; //???
-      xfixed := true;
-    end;
+    ex := (xd or (TileSize-1))+1;
+    xwalk := ex-xd;
   end;
 
-  if (not xfixed) and (x0 < wx0) then
+  // calculate ywalk
+  if (sty < 0) then
   begin
-    // clip at left
-    temp := dy2*(wx0-x0);
-    yd += temp div dx2;
-    rem := temp mod dx2;
-    if (yd > wy1) or (yd = wy1) and (rem >= dsx) then exit;
-    xd := wx0;
-    e += rem;
-    if (rem >= dsx) then begin Inc(yd); e -= dx2; end;
-  end;
-
-  if (y1 > wy1) then
+    ey := (yd and (not (TileSize-1)))-1;
+    ywalk := yd-ey;
+  end
+  else
   begin
-    // clip at bottom
-    temp := dx2*(wy1-y0)+dsx;
-    term := x0+temp div dy2;
-    rem := temp mod dy2;
-    if (rem = 0) then Dec(term);
+    ey := (yd or (TileSize-1))+1;
+    ywalk := ey-yd;
   end;
 
-  if (term > wx1) then term := wx1; // clip at right
-
-  Inc(term); // draw last point (it is ok to inc here, as `term` sign will be changed later
-  //if (term = xd) then exit; // this is the only point, get out of here
-
-  if (sty = -1) then yd := -yd;
-  if (stx = -1) then begin xd := -xd; term := -term; end;
-  dx2 -= dy2;
-
-  result := true;
-end;
-
-
-// ////////////////////////////////////////////////////////////////////////// //
-// true: has something to draw
-// based on paper by S.R.Kodituwakku, K.R.Wijeweera, M.A.P.Chamikara
-function clipMY (var x0, y0, x1, y1: Single; minx, miny, maxx, maxy: Single): Boolean;
-var
-  m, c: Single;
-begin
-  // non vertical lines
-  if (x0 <> x1) then
-  begin
-    // non vertical and non horizontal lines
-    if (y0 <> y1) then
-    begin
-      m := (y0-y1)/(x0-x1); // gradient
-      c := (x0*y1-x1*y0)/(x0-x1); // y-intercept
-           if (x0 < minx) then begin x0 := minx; y0 := m*minx+c; end
-      else if (x0 > maxx) then begin x0 := maxx; y0 := m*maxx+c; end;
-           if (y0 < miny) then begin x0 := (miny-c)/m; y0 := miny; end
-      else if (y0 > maxy) then begin x0 := (maxy-c)/m; y0 := maxy; end;
-           if (x1 < minx) then begin x1 := minx; y1 := m*minx+c; end
-      else if (x1 > maxx) then begin x1 := maxx; y1 := m*maxx+c; end;
-           if (y1 < miny) then begin x1 := (miny-c)/m; y1 := miny; end
-      else if (y1 > maxy) then begin x1 := (maxy-c)/m; y1 := maxy; end;
-      result := not ((x0-x1 < 1) and (x1-x0 < 1)); // completely outside?
-    end
-    else
-    begin
-      // horizontal lines
-      if (y0 <= miny) or (y0 >= maxy) then begin result := false; exit; end; // completely outside
-      if (x0 < minx) then x0 := minx else if (x0 > maxx) then x0 := maxx;
-      if (x1 < minx) then x1 := minx else if (x1 > maxx) then x1 := maxx;
-      result := not ((x0-x1 < 1) and (x1-x0 < 1)); // completely outside?
-    end;
-  end
-  else
+  {
+  while (xd <> ex) and (yd <> ey) do
   begin
-    // vertical lines
-    // initial line is just a point
-    if (y0 = y1) then
-    begin
-      result := not ((y0 <= miny) or (y0 >= maxy) or (x0 <= minx) or (x0 >= maxx));
-    end
-    else if (x0 <= minx) or (x0 >= maxx) then
+    if horiz then
     begin
-      // completely outside
-      result := false;
+      xd += stx;
+      err += errinc;
+      if (err >= 0) then begin err -= errmax; yd += sty; end;
     end
     else
     begin
-      if (y0 < miny) then y0 := miny else if (y0 > maxy) then y0 := maxy;
-      if (y1 < miny) then y1 := miny else if (y1 > maxy) then y1 := maxy;
-      result := not ((y0-y1 < 1) and (y1-y0 < 1)); // completely outside?
+      yd += sty;
+      err += errinc;
+      if (err >= 0) then begin err -= errmax; xd += stx; end;
     end;
+    Dec(stleft);
+    if (stleft < 1) then begin result := true; exit; end;
   end;
-end;
-
-// you are not supposed to understand this
-// returns `true` if there is an intersection, and enter coords
-// enter coords will be equal to (x0, y0) if starting point is inside the box
-// if result is `false`, `inx` and `iny` are undefined
-function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
-var
-  sx0, sy0, sx1, sy1: Single;
-begin
-  if (bw < 1) or (bh < 1) then begin inx := x0; iny := y0; result := false; exit; end;
-  if (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh) then begin inx := x0; iny := y0; result := true; exit; end;
-  sx0 := x0; sy0 := y0;
-  sx1 := x1; sy1 := y1;
-  result := clipMY(sx0, sy0, sx1, sy1, bx, by, bx+bw-1, by+bh-1);
-  if result then begin inx := trunc(sx0); iny := trunc(sy0); end else begin inx := x1; iny := y1; end;
-end;
-
-
-// ////////////////////////////////////////////////////////////////////////// //
-function sweepAABB (mex0, mey0, mew, meh: Integer; medx, medy: Integer; itx0, ity0, itw, ith: Integer;
-                    u0: PSingle=nil; hitedge: PInteger=nil; u1: PSingle=nil): Boolean;
-var
-  tin, tout: Single;
+  }
 
-  function axisOverlap (me0, me1, it0, it1, d, he0, he1: Integer): Boolean; inline;
-  var
-    t: Single;
+  if (xwalk <= ywalk) then wklen := xwalk else wklen := ywalk;
+  while true do
   begin
-    result := false;
-
-    if (me1 < it0) then
-    begin
-      if (d >= 0) then exit; // oops, no hit
-      t := (me1-it0+1)/d;
-      if (t > tin) then begin tin := t; hitedge^ := he1; end;
-    end
-    else if (it1 < me0) then
-    begin
-      if (d <= 0) then exit; // oops, no hit
-      t := (me0-it1-1)/d;
-      if (t > tin) then begin tin := t; hitedge^ := he0; end;
-    end;
-
-    if (d < 0) and (it1 > me0) then
+    // in which dir we want to walk?
+    stleft -= wklen;
+    if (stleft <= 0) then begin result := true; exit; end;
+    if horiz then
     begin
-      t := (me0-it1-1)/d;
-      if (t < tout) then tout := t;
+      xd += wklen*stx;
+      for f := 1 to wklen do
+      begin
+        err += errinc;
+        if (err >= 0) then begin err -= errmax; yd += sty; end;
+      end;
     end
-    else if (d > 0) and (me1 > it0) then
+    else
     begin
-      t := (me1-it0+1)/d;
-      if (t < tout) then tout := t;
+      yd += wklen*sty;
+      for f := 1 to wklen do
+      begin
+        err += errinc;
+        if (err >= 0) then begin err -= errmax; xd += stx; end;
+      end;
     end;
-
-    result := true;
-  end;
-
-var
-  mex1, mey1, itx1, ity1, vx, vy: Integer;
-  htt: Integer = -1;
-begin
-  result := false;
-  if (u0 <> nil) then u0^ := -1.0;
-  if (u1 <> nil) then u1^ := -1.0;
-  if (hitedge = nil) then hitedge := @htt else hitedge^ := -1;
-
-  if (mew < 1) or (meh < 1) or (itw < 1) or (ith < 1) then exit;
-
-  mex1 := mex0+mew-1;
-  mey1 := mey0+meh-1;
-  itx1 := itx0+itw-1;
-  ity1 := ity0+ith-1;
-
-  // check if they are overlapping right now (SAT)
-  //if (mex1 >= itx0) and (mex0 <= itx1) and (mey1 >= ity0) and (mey0 <= ity1) then begin result := true; exit; end;
-
-  if (medx = 0) and (medy = 0) then exit; // both boxes are sationary
-
-  // treat b as stationary, so invert v to get relative velocity
-  vx := -medx;
-  vy := -medy;
-
-  tin := -100000000.0;
-  tout := 100000000.0;
-
-  if not axisOverlap(mex0, mex1, itx0, itx1, vx, 1, 3) then exit;
-  if not axisOverlap(mey0, mey1, ity0, ity1, vy, 2, 0) then exit;
-
-  if (u0 <> nil) then u0^ := tin;
-  if (u1 <> nil) then u1^ := tout;
-
-  if (tin <= tout) and (tin >= 0.0) and (tin <= 1.0) then
-  begin
-    result := true;
+    // check for walk completion
+    if (xd = ex) or (yd = ey) then exit;
+    wklen := 1;
   end;
 end;
 
+procedure TLineWalker.getXY (out ox, oy: Integer); inline; begin ox := xd; oy := yd; end;
+
 
 // ////////////////////////////////////////////////////////////////////////// //
 procedure TBodyGridBase.TBodyProxyRec.setup (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer);
@@ -877,7 +666,7 @@ begin
   mProxyFree := 0;
   mProxyCount := 0;
   mProxyMaxCount := 0;
-  e_WriteLog(Format('created grid with size: %dx%d (tile size: %d); pix: %dx%d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize]), MSG_NOTIFY);
+  e_WriteLog(Format('created grid with size: %dx%d (tile size: %d); pix: %dx%d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize]), TMsgType.Notify);
 end;
 
 
@@ -907,7 +696,7 @@ begin
     end;
     if (mcb < cnt) then mcb := cnt;
   end;
-  e_WriteLog(Format('grid size: %dx%d (tile size: %d); pix: %dx%d; used cells: %d; max bodies in cell: %d; max proxies allocated: %d; proxies used: %d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize, mUsedCells, mcb, mProxyMaxCount, mProxyCount]), MSG_NOTIFY);
+  e_WriteLog(Format('grid size: %dx%d (tile size: %d); pix: %dx%d; used cells: %d; max bodies in cell: %d; max proxies allocated: %d; proxies used: %d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize, mUsedCells, mcb, mProxyMaxCount, mProxyCount]), TMsgType.Notify);
 end;
 
 
@@ -2065,7 +1854,7 @@ end;
 // you are not supposed to understand this
 function TBodyGridBase.traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridQueryCB; tagmask: Integer=-1): ITP;
 var
-  lw, sweepw: TLineWalker;
+  lw: TLineWalker;
   ccidx: Integer;
   cc: PGridCell;
   px: PBodyProxyRec;
@@ -2098,10 +1887,12 @@ begin
   lw := TLineWalker.Create(0, 0, gw*mTileSize-1, gh*mTileSize-1);
   if not lw.setup(x0, y0, x1, y1) then exit; // out of screen
 
-  sweepw := TLineWalker.Create(0, 0, 1, 1); // doesn't matter, just shut ups the compiler
-
   lastDistSq := distanceSq(ax0, ay0, ax1, ay1)+1;
 
+  {$IF DEFINED(D2F_DEBUG)}
+  //if assigned(dbgRayTraceTileHitCB) then e_LogWritefln('*** traceRay: (%s,%s)-(%s,%s)', [x0, y0, x1, y1]);
+  {$ENDIF}
+
   if mInQuery then raise Exception.Create('recursive queries aren''t supported');
   mInQuery := true;
 
@@ -2117,6 +1908,9 @@ begin
 
   repeat
     lw.getXY(cx, cy);
+    {$IF DEFINED(D2F_DEBUG)}
+    if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB(cx+mMinX, cy+mMinY);
+    {$ENDIF}
     // check tile
     ccidx := mGrid[(cy div mTileSize)*gw+(cx div mTileSize)];
     // process cells
@@ -2141,6 +1935,9 @@ begin
           py0 := px.mY-miny;
           px1 := px0+px.mWidth-1;
           py1 := py0+px.mHeight-1;
+          {$IF DEFINED(D2F_DEBUG)}
+          //if assigned(dbgRayTraceTileHitCB) then e_LogWritefln(' cxy=(%s,%s); pan=(%s,%s)-(%s,%s)', [cx, cy, px0, py0, px1, py1]);
+          {$ENDIF}
           // inside?
           if firstCell and (x0 >= px0) and (y0 >= py0) and (x0 <= px1) and (y0 <= py1) then
           begin
@@ -2149,23 +1946,25 @@ begin
             ey := ay0;
             result := px.mObj;
             mInQuery := false;
+            {$IF DEFINED(D2F_DEBUG)}
+            if assigned(dbgRayTraceTileHitCB) then e_LogWriteln('  INSIDE!');
+            {$ENDIF}
             exit;
           end;
           // do line-vs-aabb test
-          sweepw.setClip(px0, py0, px1, py1);
-          if sweepw.setup(x0, y0, x1, y1) then
+          if lineAABBIntersects(x0, y0, x1, y1, px0, py0, px1-px0+1, py1-py0+1, hx, hy) then
           begin
             // hit detected
-            sweepw.getPrevXY(hx, hy);
             distSq := distanceSq(x0, y0, hx, hy);
+            {$IF DEFINED(D2F_DEBUG)}
+            //if assigned(dbgRayTraceTileHitCB) then e_LogWritefln('  hit=(%s,%s); distSq=%s; lastDistSq=%s', [hx, hy, distSq, lastDistSq]);
+            {$ENDIF}
             if (distSq < lastDistSq) then
             begin
               lastDistSq := distSq;
               ex := hx+minx;
               ey := hy+miny;
               result := px.mObj;
-              // if this is not a first cell, get outta here
-              if not firstCell then begin mInQuery := false; exit; end;
               wasHit := true;
             end;
           end;