diff --git a/src/game/g_grid.pas b/src/game/g_grid.pas
index b7b9e3fbb74f036f96850056900c69c9f45860ed..4c3b82643e593b51df955446367e982c7b2a2e87 100644 (file)
--- a/src/game/g_grid.pas
+++ b/src/game/g_grid.pas
-(* Copyright (C) DooM 2D:Forever Developers
+(* Copyright (C) Doom 2D: Forever Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
{.$DEFINE D2F_DEBUG_MOVER}
{$ENDIF}
{.$DEFINE GRID_USE_ORTHO_ACCEL}
{.$DEFINE D2F_DEBUG_MOVER}
{$ENDIF}
{.$DEFINE GRID_USE_ORTHO_ACCEL}
+{$DEFINE LINEAABB2}
unit g_grid;
interface
unit g_grid;
interface
+uses
+ mempool;
+
+(*
+ * In order to make this usable for kind-of-recursive calls,
+ * we'll use "frame memory pool" to return results. That is,
+ * we will allocate a memory pool that will be cleared on
+ * frame start, and then used as a simple "no-free" allocator.
+ * Grid will put results into this pool, and will never bother
+ * to free it. Caller should call "release" on result, and
+ * the pool will throw away everything.
+ * No more callbacks, of course.
+ *)
+
const
GridTileSize = 32; // must be power of two!
type
TBodyProxyId = Integer;
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
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
// no callback: return object of the nearest hit or nil
// if `inverted` is true, trace will register bodies *exluding* tagmask
//WARNING: don't change tags in callbacks here!
// no callback: return object of the nearest hit or nil
// if `inverted` is true, trace will register bodies *exluding* tagmask
//WARNING: don't change tags in callbacks here!
+ {
function traceRayOld (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; overload;
function traceRayOld (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
function traceRayOld (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; overload;
function traceRayOld (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
+ }
//WARNING: don't modify grid while any query is in progress (no checks are made!)
// you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
//WARNING: don't modify grid while any query is in progress (no checks are made!)
// you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
private
wx0, wy0, wx1, wy1: Integer; // window coordinates
stx, sty: Integer; // "steps" for x and y axes
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
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
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;
// 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;
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;
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;
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
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;
+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 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);
// ////////////////////////////////////////////////////////////////////////// //
constructor TLineWalker.Create (minx, miny, maxx, maxy: Integer);
wy1 := maxy;
end;
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
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
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
begin
- ex := (lxd and (not (TileSize-1)))-1;
- xwalk := lxd-ex;
+ result := true;
end
else
begin
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;
end;
- // calculate ywalk
- if (lsty < 0) then
+ // check for ortho lines
+ if (y0 = y1) then
+ begin
+ // 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 if (x0 = x1) then
begin
begin
- ey := (lyd and (not (TileSize-1)))-1;
- ywalk := lyd-ey;
+ // 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
end
else
begin
- ey := (lyd or (TileSize-1))+1;
- ywalk := ey-lyd;
- end;
-
- while true do
- 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
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
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;
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;
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;
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
begin
- //writeln('e=', e, '; dx2=', dx2, '; dy2=', dy2);
- if xyswapped then
+ if horiz then
begin
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
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;
end;
+ Dec(stleft);
+ result := (stleft <= 0);
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; begin a := a xor b; b := b xor a; a := a xor b; end;
+// true: done
+function TLineWalker.stepToNextTile (): Boolean; inline;
var
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;
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
- 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);
- end;
+ if (stleft < 2) then begin result := true; exit; end; // max one pixel left, nothing to do
- // vertical setup
- if (y0 < y1) then
+ // strictly horizontal?
+ if (sty = 0) then
begin
begin
- // from top to bottom
- if (y0 > wy1) or (y1 < wy0) then exit; // out of screen
- sty := 1; // going down
- 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
- 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);
- 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
+ // only xd
+ if (stx < 0) then
begin
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;
+ // 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;
end;
+ result := (stleft <= 0);
+ xd := ex;
+ exit;
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;
- end;
-
- if (y1 > wy1) then
+ // strictly vertical?
+ if (stx = 0) then
begin
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 (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;
-
-
-// ////////////////////////////////////////////////////////////////////////// //
-// 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
- 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;
-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
- begin
- // check this point
- result := (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh);
+ // 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;
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;
+ // diagonal
- // 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
- 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);
- end;
-
- // vertical setup
- if (y0 < y1) then
+ // calculate xwalk
+ if (stx < 0) then
begin
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
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;
end;
- dsx := x1-x0;
- dsy := y1-y0;
-
- if (dsx < dsy) then
+ // calculate ywalk
+ if (sty < 0) then
begin
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
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; //BUGGY
- if (xd < wx0) then begin xd += 1; 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;
- 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);
+ ey := (yd or (TileSize-1))+1;
+ ywalk := ey-yd;
end;
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
begin
- result := false;
-
- if (me1 < it0) then
+ if horiz then
begin
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
end
- else if (it1 < me0) then
+ else
begin
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;
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
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
end
- else if (d > 0) and (me1 > it0) then
+ else
begin
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;
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;
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);
// ////////////////////////////////////////////////////////////////////////// //
procedure TBodyGridBase.TBodyProxyRec.setup (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer);
mProxyFree := 0;
mProxyCount := 0;
mProxyMaxCount := 0;
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;
end;
if (mcb < cnt) then mcb := cnt;
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;
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
// 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;
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
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;
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;
if mInQuery then raise Exception.Create('recursive queries aren''t supported');
mInQuery := true;
repeat
lw.getXY(cx, cy);
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
// 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;
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
// inside?
if firstCell and (x0 >= px0) and (y0 >= py0) and (x0 <= px1) and (y0 <= py1) then
begin
ey := ay0;
result := px.mObj;
mInQuery := false;
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
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
begin
// hit detected
- sweepw.getPrevXY(hx, hy);
distSq := distanceSq(x0, y0, 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 (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;
wasHit := true;
end;
end;
// ////////////////////////////////////////////////////////////////////////// //
// no callback: return `true` on the nearest hit
// ////////////////////////////////////////////////////////////////////////// //
// no callback: return `true` on the nearest hit
+(*
function TBodyGridBase.traceRayOld (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
var
ex, ey: Integer;
function TBodyGridBase.traceRayOld (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
var
ex, ey: Integer;
prevx := xptr^+minx;
prevy := yptr^+miny;
prevx := xptr^+minx;
prevy := yptr^+miny;
- (*
+ ( *
// move coords
if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
xd += stx;
// done?
if (xd = term) then exit;
// move coords
if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
xd += stx;
// done?
if (xd = term) then exit;
- *)
+ * )
{$IF DEFINED(D2F_DEBUG)}
if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*mTileSize) and (yptr^ >= gh*mTileSize) then raise Exception.Create('raycaster internal error (0)');
{$IF DEFINED(D2F_DEBUG)}
if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*mTileSize) and (yptr^ >= gh*mTileSize) then raise Exception.Create('raycaster internal error (0)');
mInQuery := false;
end;
mInQuery := false;
end;
+*)
end.
end.