diff --git a/src/game/g_grid.pas b/src/game/g_grid.pas
index 55feca9a094f1ac89568f79b79e78f5ab2e72d93..27fa615fba297bda231768783ed094bbdc579155 100644 (file)
--- a/src/game/g_grid.pas
+++ b/src/game/g_grid.pas
{.$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
function allocProxy (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer): TBodyProxyId;
procedure freeProxy (body: TBodyProxyId);
- procedure insertInternal (body: TBodyProxyId);
- procedure removeInternal (body: TBodyProxyId);
-
function forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean;
function inserter (grida: Integer; bodyId: TBodyProxyId): Boolean;
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
// 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;
implementation
uses
- SysUtils, e_log, g_console, utils;
+ SysUtils, e_log, g_console, geom, utils;
// ////////////////////////////////////////////////////////////////////////// //
procedure swapInt (var a: Integer; var b: Integer); inline; var t: Integer; begin t := a; a := b; b := t; end;
+//procedure swapInt (var a: Integer; var b: Integer); inline; begin a := a xor b; b := b xor a; a := a xor b; end;
//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);
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;
-
-// move to next tile; return `true` if the line is complete (and walker state is undefined then)
-function TLineWalker.stepToNextTile (): Boolean; inline;
-var
- ex, ey, wklen, f: Integer;
-begin
- result := false;
-
- //writeln('stx=', stx, '; sty=', sty);
-
- // ortho?
- if (sty = 0) then
- begin
- // only xd
- assert(sty <> 0);
- if (stx < 0) then
- begin
- // xd: to left edge
- xd := (xd and (not (TileSize-1)))-1;
- result := (xd <= term);
- exit;
- end
- else
- begin
- // xd: to right edge
- xd := (xd or (TileSize-1))+1;
- result := (xd >= term);
- exit;
- end;
- end;
-
- assert(stx <> 0); // invariant
-
- // not ortho
- if (sty < 0) then ey := (yd and (not (TileSize-1)))-1 else ey := (yd or (TileSize-1))+1;
-
- //FIXME: do some math to avoid single-stepping
- if (stx < 0) then
- begin
- // xd: to left edge
- ex := (xd and (not (TileSize-1)))-1;
- if (ex <= term) then begin result := true; exit; end;
- wklen := xd-ex;
- end
- else
- begin
- // xd: to right edge
- ex := (xd or (TileSize-1))+1;
- if (ex >= term) then begin result := true; exit; end;
- wklen := ex-xd;
- end;
-
- //writeln('xd=', xd, '; yd=', yd, '; ex=', ex, '; ey=', ey, '; term=', term, '; wklen=', wklen);
-
- for f := wklen downto 1 do
- begin
- // do step
- if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
- xd += stx;
- if (xd = term) then begin result := true; exit; end;
- if (xd = ex) or (yd = ey) then exit; // done
- end;
-
- raise Exception.Create('TLineWalker.stepToNextTile: the thing that should not be!');
-end;
-
-// NOT TESTED!
-procedure TLineWalker.getPrevXY (out ox, oy: Integer); inline;
-begin
- //writeln('e=', e, '; dx2=', dx2, '; dy2=', dy2);
- if xyswapped then
- begin
- if (e >= 0) then ox := yd-sty else ox := yd;
- oy := xd-stx;
- end
- else
- begin
- if (e >= 0) then oy := yd-sty else oy := yd;
- ox := xd-stx;
- end;
-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; var t: Integer; begin t := a; a := b; b := t; end;
var
- dsx, dsy: Integer; // "lengthes" for x and y axes
- rem: Integer;
- xfixed: Boolean;
- temp: Integer;
+ sx0, sy0, sx1, sy1: Single;
begin
- result := false;
- xyswapped := false;
+ if (wx1 < wx0) or (wy1 < wy0) then begin stleft := 0; xd := x0; yd := y0; result := false; exit; end;
- // horizontal setup
- if (x0 < x1) 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
- // from left to right
- if (x0 > wx1) or (x1 < wx0) then exit; // out of screen
- stx := 1; // going right
+ result := true;
end
else
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);
+ 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;
- // vertical setup
- if (y0 < y1) then
+ // check for ortho lines
+ if (y0 = y1) then
begin
- // from top to bottom
- if (y0 > wy1) or (y1 < wy0) then exit; // out of screen
- sty := 1; // going down
+ // 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
- 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);
- end;
-
- dsx := x1-x0;
- dsy := y1-y0;
-
- if (dsx < dsy) then
+ else if (x0 = x1) 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);
+ // 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
- //xptr := @xd;
- //yptr := @yd;
+ // diagonal
+ if (abs(x1-x0) >= abs(y1-y0)) then
+ begin
+ // horizontal
+ horiz := true;
+ stleft := abs(x1-x0)+1;
+ errinc := abs(y1-y0)+1;
+ end
+ else
+ begin
+ // vertical
+ horiz := false;
+ stleft := abs(y1-y0)+1;
+ errinc := abs(x1-x0)+1;
+ end;
+ if (x0 < x1) then stx := 1 else stx := -1;
+ if (y0 < y1) then sty := 1 else sty := -1;
+ errmax := stleft;
end;
-
- dx2 := 2*dsx;
- dy2 := 2*dsy;
xd := x0;
yd := y0;
- e := 2*dsy-dsx;
- term := x1;
+ err := -errmax;
+end;
- 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;
- xfixed := true;
- end;
- end;
+function TLineWalker.done (): Boolean; inline; begin result := (stleft <= 0); end;
- if (not xfixed) and (x0 < wx0) then
+// true: done
+function TLineWalker.step (): Boolean; inline;
+begin
+ if horiz 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
+ xd += stx;
+ err += errinc;
+ if (err >= 0) then begin err -= errmax; yd += sty; end;
+ 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);
+ yd += sty;
+ err += errinc;
+ if (err >= 0) then begin err -= errmax; xd += stx; end;
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;
+ Dec(stleft);
+ result := (stleft <= 0);
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;
+// true: done
+function TLineWalker.stepToNextTile (): Boolean; inline;
var
- wx0, wy0, wx1, wy1: Integer; // window coordinates
- stx, sty: Integer; // "steps" for x and y axes
- dsx, dsy: Integer; // "lengthes" for x and y axes
- dx2, dy2: Integer; // "double lengthes" for x and y axes
- xd, yd: Integer; // current coord
- e: Integer; // "error" (as in bresenham algo)
- rem: Integer;
- //!term: Integer;
- d0, d1: PInteger;
- xfixed: Boolean;
- temp: Integer;
+ ex, ey: Integer;
+ xwalk, ywalk, wklen: Integer; // to the respective edges
+ f: Integer;
begin
result := false;
- // why not
- inx := x0;
- iny := y0;
- if (bw < 1) or (bh < 1) then exit; // impossible box
- if (x0 = x1) and (y0 = y1) then
+ if (stleft < 2) then begin result := true; exit; end; // max one pixel left, nothing to do
+
+ // strictly horizontal?
+ if (sty = 0) then
begin
- // check this point
- result := (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh);
+ // 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;
- // check if staring point is inside the box
- if (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh) then begin result := true; exit; end;
-
- // clip rectange
- wx0 := bx;
- wy0 := by;
- wx1 := bx+bw-1;
- wy1 := by+bh-1;
-
- // 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
+ // strictly vertical?
+ if (stx = 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 (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;
- // vertical setup
- if (y0 < y1) then
+ // diagonal
+
+ // calculate xwalk
+ if (stx < 0) then
begin
- // from top to bottom
- if (y0 > wy1) or (y1 < wy0) then exit; // out of screen
- sty := 1; // going down
+ ex := (xd and (not (TileSize-1)))-1;
+ xwalk := xd-ex;
end
else
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);
+ ex := (xd or (TileSize-1))+1;
+ xwalk := ex-xd;
end;
- dsx := x1-x0;
- dsy := y1-y0;
-
- if (dsx < dsy) then
+ // calculate ywalk
+ if (sty < 0) then
begin
- d0 := @yd;
- d1 := @xd;
- swapInt(x0, y0);
- swapInt(x1, y1);
- swapInt(dsx, dsy);
- swapInt(wx0, wy0);
- swapInt(wx1, wy1);
- swapInt(stx, sty);
+ ey := (yd and (not (TileSize-1)))-1;
+ ywalk := yd-ey;
end
else
begin
- d0 := @xd;
- d1 := @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;
- xfixed := true;
- end;
- end;
-
- if (not xfixed) and (x0 < wx0) 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;
+ ey := (yd or (TileSize-1))+1;
+ ywalk := ey-yd;
end;
- (*
- if (y1 > wy1) then
- begin
- // clip at bottom
- temp := dx2*(wy1-y0)+dsx;
- term := x0+temp div dy2;
- rem := temp mod dy2;
- if (rem = 0) then Dec(term);
- end;
-
- if (term > wx1) then term := wx1; // clip at right
-
- Inc(term); // draw last point
- //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;
-
- inx := d0^;
- iny := d1^;
- result := true;
-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;
+ {
+ while (xd <> ex) and (yd <> ey) do
begin
- result := false;
-
- if (me1 < it0) then
+ if horiz 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;
+ xd += stx;
+ err += errinc;
+ if (err >= 0) then begin err -= errmax; yd += sty; end;
end
- else if (it1 < me0) then
+ else
begin
- if (d <= 0) then exit; // oops, no hit
- t := (me0-it1-1)/d;
- if (t > tin) then begin tin := t; hitedge^ := he0; end;
+ 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;
+ }
- if (d < 0) and (it1 > me0) then
+ if (xwalk <= ywalk) then wklen := xwalk else wklen := ywalk;
+ while true do
+ begin
+ // 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);
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;
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;
// ////////////////////////////////////////////////////////////////////////// //
function TBodyGridBase.forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean;
var
- gx, gy: Integer;
gw, gh: Integer;
+ ex, ey: Integer;
+ gx, gy: Integer;
begin
result := false;
if (w < 1) or (h < 1) or not assigned(cb) then exit;
if (x+w <= 0) or (y+h <= 0) then exit;
gw := mWidth;
gh := mHeight;
- //tsize := mTileSize;
if (x >= gw*mTileSize) or (y >= gh*mTileSize) then exit;
- for gy := y div mTileSize to (y+h-1) div mTileSize do
- begin
- if (gy < 0) then continue;
- if (gy >= gh) then break;
- for gx := x div mTileSize to (x+w-1) div mTileSize do
+ ex := (x+w-1) div mTileSize;
+ ey := (y+h-1) div mTileSize;
+ x := x div mTileSize;
+ y := y div mTileSize;
+ // clip rect
+ if (x < 0) then x := 0 else if (x >= gw) then x := gw-1;
+ if (y < 0) then y := 0 else if (y >= gh) then y := gh-1;
+ if (ex < 0) then ex := 0 else if (ex >= gw) then ex := gw-1;
+ if (ey < 0) then ey := 0 else if (ey >= gh) then ey := gh-1;
+ if (x > ex) or (y > ey) then exit; // just in case
+ // do the work
+ for gy := y to ey do
+ begin
+ for gx := x to ex do
begin
- if (gx < 0) then continue;
- if (gx >= gw) then break;
result := cb(gy*gw+gx, bodyId);
if result then exit;
end;
mGrid[grida] := ccidx;
end;
-procedure TBodyGridBase.insertInternal (body: TBodyProxyId);
-var
- px: PBodyProxyRec;
-begin
- if (body < 0) or (body > High(mProxies)) then exit; // just in case
- px := @mProxies[body];
- forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, inserter, body);
-end;
-
// assume that we cannot have one object added to bucket twice
function TBodyGridBase.remover (grida: Integer; bodyId: TBodyProxyId): Boolean;
end;
end;
-procedure TBodyGridBase.removeInternal (body: TBodyProxyId);
-var
- px: PBodyProxyRec;
-begin
- if (body < 0) or (body > High(mProxies)) then exit; // just in case
- px := @mProxies[body];
- forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, remover, body);
-end;
-
// ////////////////////////////////////////////////////////////////////////// //
function TBodyGridBase.insertBody (aObj: ITP; aX, aY, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
begin
aTag := aTag and TagFullMask;
result := allocProxy(aX, aY, aWidth, aHeight, aObj, aTag);
- insertInternal(result);
+ //insertInternal(result);
+ forGridRect(aX, aY, aWidth, aHeight, inserter, result);
end;
procedure TBodyGridBase.removeBody (body: TBodyProxyId);
+var
+ px: PBodyProxyRec;
begin
if (body < 0) or (body > High(mProxies)) then exit; // just in case
- removeInternal(body);
+ px := @mProxies[body];
+ //removeInternal(body);
+ forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, remover, body);
freeProxy(body);
end;
// did any corner crossed tile boundary?
if (x0 div mTileSize <> nx div mTileSize) or
(y0 div mTileSize <> ny div mTileSize) or
- ((x0+w) div mTileSize <> (nx+nw) div mTileSize) or
- ((y0+h) div mTileSize <> (ny+nh) div mTileSize) then
+ ((x0+w-1) div mTileSize <> (nx+nw-1) div mTileSize) or
+ ((y0+h-1) div mTileSize <> (ny+nh-1) div mTileSize) then
begin
- removeInternal(body);
+ //writeln('moveResizeBody: cell occupation changed! old=(', x0, ',', y0, ')-(', x0+w-1, ',', y0+h-1, '); new=(', nx, ',', ny, ')-(', nx+nw-1, ',', ny+nh-1, ')');
+ //removeInternal(body);
+ forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, remover, body);
px.mX := nx+mMinX;
px.mY := ny+mMinY;
px.mWidth := nw;
px.mHeight := nh;
- insertInternal(body);
+ //insertInternal(body);
+ forGridRect(px.mX, px.mY, nw, nh, inserter, body);
end
else
begin
end;
end;
+
//TODO: optimize for horizontal/vertical moves
procedure TBodyGridBase.moveBody (body: TBodyProxyId; nx, ny: Integer);
var
px.mY := ny+mMinY;
end;
+
procedure TBodyGridBase.resizeBody (body: TBodyProxyId; nw, nh: Integer);
var
px: PBodyProxyRec;
{$IF DEFINED(D2F_DEBUG_MOVER)}
e_WriteLog(Format('proxy #%d: RESIZE: xg=%d;yg=%d;w=%d;h=%d;nw=%d;nh=%d', [body, x0, y0, w, h, nw, nh]), MSG_NOTIFY);
{$ENDIF}
- if ((x0+w) div mTileSize <> (x0+nw) div mTileSize) or
- ((y0+h) div mTileSize <> (y0+nh) div mTileSize) then
+ if ((x0+w-1) div mTileSize <> (x0+nw-1) div mTileSize) or
+ ((y0+h-1) div mTileSize <> (y0+nh-1) div mTileSize) then
begin
// crossed tile boundary, do heavy work
- removeInternal(body);
+ //removeInternal(body);
+ forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, remover, body);
px.mWidth := nw;
px.mHeight := nh;
- insertInternal(body);
+ //insertInternal(body);
+ forGridRect(px.mX, px.mY, nw, nh, inserter, body);
end
else
begin
@@ -1690,12 +1422,13 @@ function TBodyGridBase.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tag
var
idx: Integer;
gx, gy: Integer;
+ sx, sy, ex, ey: Integer;
curci: Integer;
f: Integer;
cc: PGridCell = nil;
px: PBodyProxyRec;
lq: LongWord;
- gw: Integer;
+ gw, gh: Integer;
x0, y0: Integer;
ptag: Integer;
begin
Dec(y, mMinY);
gw := mWidth;
- //tsize := mTileSize;
+ gh := mHeight;
if (x+w <= 0) or (y+h <= 0) then exit;
- if (x >= gw*mTileSize) or (y >= mHeight*mTileSize) then exit;
+ if (x >= gw*mTileSize) or (y >= gh*mTileSize) then exit;
+
+ sx := x div mTileSize;
+ sy := y div mTileSize;
+ ex := (x+w-1) div mTileSize;
+ ey := (y+h-1) div mTileSize;
+
+ // clip rect
+ if (sx < 0) then sx := 0 else if (sx >= gw) then sx := gw-1;
+ if (sy < 0) then sy := 0 else if (sy >= gh) then sy := gh-1;
+ if (ex < 0) then ex := 0 else if (ex >= gw) then ex := gw-1;
+ if (ey < 0) then ey := 0 else if (ey >= gh) then ey := gh-1;
+ if (sx > ex) or (sy > ey) then exit; // just in case
+ // has something to do
if mInQuery then raise Exception.Create('recursive queries aren''t supported');
mInQuery := true;
lq := mLastQuery;
// go on
- for gy := y div mTileSize to (y+h-1) div mTileSize do
+ for gy := sy to ey do
begin
- if (gy < 0) then continue;
- if (gy >= mHeight) then break;
- for gx := x div mTileSize to (x+w-1) div mTileSize do
+ for gx := sx to ex do
begin
- if (gx < 0) then continue;
- if (gx >= gw) then break;
// process cells
curci := mGrid[gy*gw+gx];
while (curci <> -1) do
begin
if (cc.bodies[f] = -1) then break;
px := @mProxies[cc.bodies[f]];
- // shit. has to do it this way, so i can change tag in callback
+ // shit! has to do it this way, so i can change tag in callback
if (px.mQueryMark = lq) then continue;
px.mQueryMark := lq;
ptag := px.mTag;
// 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;
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;
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
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
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;
begin
yd := wy0;
e -= rem+dsx;
- if (rem > 0) then begin Inc(xd); e += dy2; end;
+ //if (rem > 0) then begin Inc(xd); e += dy2; end; //BUGGY
+ if (xd < wx0) then begin xd += 1; e += dy2; end; //???
xfixed := true;
end;
end;