1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE a_modes.inc}
16 // some geometry utilities
19 interface
22 // do line clipping; returns `false` if line is outside of the box
25 // returns `true` if there is an intersection (if starting point is inside the box, it counts as intersection)
28 // you are not supposed to understand this
29 // returns `true` if there is an intersection, and enter coords
30 // enter coords will be equal to (x0, y0) if starting point is inside the box
31 // if result is `false`, `inx` and `iny` are undefined
32 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
34 type
38 // sweep two AABB's to see if and when they are overlapping
39 // returns `true` if collision was detected (but boxes aren't overlap)
40 // `u1` and `u1` has no sense if no collision was detected (`hitx` and `hity` either)
41 // u0 = normalized time of first collision (i.e. collision starts at myMove*u0)
42 // u1 = normalized time of second collision (i.e. collision stops after myMove*u1)
43 // hitedge for `it`: it will probably be `None` if no collision was detected, but it is not guaranteed
44 // enter/exit coords will form non-intersecting configuration (i.e. will be before/after the actual collision)
45 // but beware of floating point inexactness; `sweepAABB()` will try to (crudely) compensate for it
46 // while calculating `hitx` and `hity`.
47 function sweepAABB (mex0, mey0, mew, meh: Integer; medx, medy: Integer; itx0, ity0, itw, ith: Integer;
54 implementation
57 // ////////////////////////////////////////////////////////////////////////// //
58 function distanceSq (x0, y0, x1, y1: Integer): Integer; inline; begin result := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0); end;
61 // ////////////////////////////////////////////////////////////////////////// //
63 const
71 begin
77 var
81 begin
86 begin
92 begin
95 end
97 begin
100 end
102 begin
105 end
107 begin
112 begin
116 end
117 else
118 begin
127 // returns `true` if there is an intersection (if starting point is inside the box, it counts as intersection)
129 var
131 begin
134 if (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh) then begin result := true; exit; end;
141 // returns `true` if there is an intersection, and enter coords
142 // enter coords will be equal to (x0, y0) if starting point is inside the box
143 // if result is `false`, `inx` and `iny` are undefined
144 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
145 var
147 begin
152 if (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh) then begin result := true; exit; end;
157 begin
160 // hack!
163 end
164 else
165 begin
172 // ////////////////////////////////////////////////////////////////////////// //
173 function sweepAABB (mex0, mey0, mew, meh: Integer; medx, medy: Integer; itx0, ity0, itw, ith: Integer;
176 var
180 var
182 begin
186 begin
190 end
192 begin
199 begin
202 end
204 begin
212 var
215 begin
230 // check if they are overlapping right now (SAT)
231 //if (mex1 >= itx0) and (mex0 <= itx1) and (mey1 >= ity0) and (mey0 <= ity1) then begin result := true; exit; end;
235 // treat b as stationary, so invert v to get relative velocity
249 begin
252 begin
255 // just in case, compensate for floating point inexactness
257 begin