6f2168d33814355f6f79332c4170bc2af187161d
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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 {.$DEFINE aabbtree_many_asserts}
18 {$DEFINE aabbtree_query_count}
19 {.$DEFINE aabbtree_use_floats}
22 interface
24 uses
28 // ////////////////////////////////////////////////////////////////////////// //
29 type
33 // ////////////////////////////////////////////////////////////////////////// //
34 type
36 public
40 public
53 // ////////////////////////////////////////////////////////////////////////// //
54 type
56 public
59 private
66 public
82 // return true if the current AABB contains the AABB given in parameter
86 // return true if the current AABB is overlapping with the AABB in parameter
87 // two AABBs overlap if they overlap in the two axes at the same time
90 // ray direction must be normalized
91 function intersects (constref ray: Ray2D; tmino: PSingle=nil; tmaxo: PSingle=nil): Boolean; overload;
102 // ////////////////////////////////////////////////////////////////////////// //
103 (* Dynamic AABB tree (bounding volume hierarchy)
104 * based on the code from ReactPhysics3D physics library, http://www.reactphysics3d.com
105 * Copyright (c) 2010-2016 Daniel Chappuis
106 *
107 * This software is provided 'as-is', without any express or implied warranty.
108 * In no event will the authors be held liable for any damages arising from the
109 * use of this software.
110 *
111 * Permission is granted to anyone to use this software for any purpose,
112 * including commercial applications, and to alter it and redistribute it
113 * freely, subject to the following restrictions:
114 *
115 * 1. The origin of this software must not be misrepresented; you must not claim
116 * that you wrote the original software. If you use this software in a
117 * product, an acknowledgment in the product documentation would be
118 * appreciated but is not required.
119 *
120 * 2. Altered source versions must be plainly marked as such, and must not be
121 * misrepresented as being the original software.
122 *
123 * 3. This notice may not be removed or altered from any source distribution.
124 *)
125 // ////////////////////////////////////////////////////////////////////////// //
126 (*
127 * This class implements a dynamic AABB tree that is used for broad-phase
128 * collision detection. This data structure is inspired by Nathanael Presson's
129 * dynamic tree implementation in BulletPhysics. The following implementation is
130 * based on the one from Erin Catto in Box2D as described in the book
131 * "Introduction to Game Physics with Box2D" by Ian Parberry.
132 *)
133 // ////////////////////////////////////////////////////////////////////////// //
134 // Dynamic AABB Tree: can be used to speed up broad phase in various engines
135 type
137 public
140 private
141 type
144 public
148 public
149 // a node is either in the tree (has a parent) or in the free nodes list (has a next node)
151 //nextNodeId: Integer;
152 // a node is either a leaf (has data) or is an internal node (has children)
153 children: array [0..1] of Integer; // left and right child of the node (children[0] = left child)
154 // height of the node in the tree (-1 for free nodes)
156 // fat axis aligned bounding box (AABB) corresponding to the node
158 //TODO: `flesh` can be united with `children`
162 public
163 // return true if the node is a leaf of the tree
168 //property flesh: Integer read children[0] write children[0];
174 //TVisitVisitorCB = function (abody: TTreeFlesh; atag: Integer): Boolean is nested;
180 public
181 // return `true` to stop
182 type TForEachLeafCB = function (abody: TTreeFlesh; constref aabb: AABB2D): Boolean is nested; // WARNING! don't modify AABB here!
184 public
185 // in the broad-phase collision detection (dynamic AABB tree), the AABBs are
186 // also inflated in direction of the linear motion of the body by mutliplying the
187 // followin constant with the linear velocity and the elapsed time between two frames
188 {$IFDEF aabbtree_use_floats}
190 {$ELSE}
192 {$ENDIF}
194 public
195 // called when a overlapping node has been found during the call to forEachAABBOverlap()
196 // return `true` to stop
198 type TSegQueryCallback = function (abody: TTreeFlesh; ax, ay, bx, by: Single): Single is nested; // return dist from (ax,ay) to abody
210 private
213 mFreeNodeId: Integer; // id of the first node of the list of free (allocated) nodes in the tree that we can use
217 // extra AABB Gap used to allow the collision shape to move a little bit
218 // without triggering a large modification of the tree which can be costly
223 // for segment query
239 private
248 function visit (constref caabb: AABB2D; mode: Integer; checker: TVisitCheckerCB; visitor: TQueryOverlapCB; visdg: TQueryOverlapDg; tagmask: Integer): Integer;
252 public
253 {$IFDEF aabbtree_query_count}
255 {$ENDIF}
257 public
261 // clear all the nodes and reset the tree
271 // returns `false` if nodeid is not leaf
274 // return `false` for invalid flesh
275 function getFleshAABB (out aabb: AABB2D; flesh: TTreeFlesh; tag: Integer): Boolean; virtual; abstract;
277 // insert an object into the tree
278 // this method creates a new leaf node in the tree and returns the id of the corresponding node or -1 on error
279 // AABB for static object will not be "fat" (simple optimization)
280 // WARNING! inserting the same object several times *WILL* break everything!
281 function insertObject (flesh: TTreeFlesh; tag: Integer=-1; staticObject: Boolean=false): Integer;
283 // remove an object from the tree
284 // WARNING: ids of removed objects can be reused on later insertions!
287 (** update the dynamic tree after an object has moved.
288 *
289 * if the new AABB of the object that has moved is still inside its fat AABB, then nothing is done.
290 * otherwise, the corresponding node is removed and reinserted into the tree.
291 * the method returns true if the object has been reinserted into the tree.
292 * the `dispX` and `dispY` parameters are the linear velocity of the AABB multiplied by the elapsed time between two frames.
293 * if the `forceReinsert` parameter is `true`, we force a removal and reinsertion of the node
294 * (this can be useful if the shape AABB has become much smaller than the previous one for instance).
295 *
296 * note that you should call this method if body's AABB was modified, even if the body wasn't moved.
297 *
298 * if `forceReinsert` = `true` and both `dispX` and `dispY` are zeroes, convert object to "static" (don't extrude AABB).
299 *
300 * return `true` if the tree was modified.
301 *)
302 function updateObject (nodeId: Integer; dispX, dispY: TreeNumber; forceReinsert: Boolean=false): Boolean; overload;
305 function aabbQuery (ax, ay, aw, ah: TreeNumber; cb: TQueryOverlapCB; tagmask: Integer=-1): TTreeFlesh;
307 function segmentQuery (out qr: TSegmentQueryResult; ax, ay, bx, by: TreeNumber; cb: TSegQueryCallback; tagmask: Integer=-1): Boolean;
314 {$IFDEF aabbtree_query_count}
317 {$ELSE}
320 {$ENDIF}
331 implementation
333 uses
334 SysUtils;
337 // ////////////////////////////////////////////////////////////////////////// //
338 function dtMinI (a, b: Integer): Integer; inline; begin if (a < b) then result := a else result := b; end;
339 function dtMaxI (a, b: Integer): Integer; inline; begin if (a > b) then result := a else result := b; end;
341 function dtMinF (a, b: TreeNumber): TreeNumber; inline; begin if (a < b) then result := a else result := b; end;
342 function dtMaxF (a, b: TreeNumber): TreeNumber; inline; begin if (a > b) then result := a else result := b; end;
345 // ////////////////////////////////////////////////////////////////////////// //
346 constructor Ray2D.Create (ax, ay: Single; aangle: Single); begin setXYAngle(ax, ay, aangle); end;
347 constructor Ray2D.Create (ax0, ay0, ax1, ay1: Single); begin setX0Y0X1Y1(ax0, ay0, ax1, ay1); end;
352 begin
360 var
362 begin
369 begin
377 begin
386 // ////////////////////////////////////////////////////////////////////////// //
388 begin
393 begin
398 begin
403 begin
410 function AABB2D.getvalid (): Boolean; inline; begin result := (minX <= maxX) and (minY <= maxY); end;
412 {$IFDEF aabbtree_use_floats}
415 {$ELSE}
418 {$ENDIF}
423 begin
428 {$IF DEFINED(D2F_DEBUG)}
430 {$ENDIF}
435 begin
440 {$IF DEFINED(D2F_DEBUG)}
442 {$ENDIF}
447 begin
448 {$IF DEFINED(D2F_DEBUG)}
451 {$ENDIF}
456 {$IF DEFINED(D2F_DEBUG)}
458 {$ENDIF}
463 begin
469 begin
470 {$IF DEFINED(D2F_DEBUG)}
472 {$ENDIF}
477 {$IF DEFINED(D2F_DEBUG)}
479 {$ENDIF}
484 begin
485 result :=
492 begin
498 begin
500 // exit with no intersection if found separated along any axis
507 // something to consider here is that 0 * inf =nan which occurs when the ray starts exactly on the edge of a box
508 // https://tavianator.com/fast-branchless-raybounding-box-intersections-part-2-nans/
509 function AABB2D.intersects (constref ray: Ray2D; tmino: PSingle=nil; tmaxo: PSingle=nil): Boolean; overload;
510 var
513 begin
514 // ok with coplanars
517 // do X
519 begin
526 // do Y
528 begin
532 // tmin
536 // tmax
543 begin
547 end
548 else
549 begin
555 var
558 begin
560 // it may be faster to first check if start or end point is inside AABB (this is sometimes enough for dyntree)
563 // nope, do it hard way
573 // ////////////////////////////////////////////////////////////////////////// //
574 constructor TDynAABBTreeBase.TSegmentQueryResult.Create (fuckyoufpc: Boolean); begin dist := -1; flesh := Default(ITP); end;
575 procedure TDynAABBTreeBase.TSegmentQueryResult.reset (); inline; begin dist := -1; flesh := Default(ITP); end;
576 function TDynAABBTreeBase.TSegmentQueryResult.valid (): Boolean; inline; begin result := (dist >= 0) and (flesh <> Default(ITP)); end;
579 // ////////////////////////////////////////////////////////////////////////// //
580 function TDynAABBTreeBase.TTreeNode.leaf (): Boolean; inline; begin result := (height = 0); end;
581 function TDynAABBTreeBase.TTreeNode.isfree (): Boolean; inline; begin result := (height = -1); end;
584 begin
598 begin
599 e_WriteLog(Format('NODE: parentId=%d; children=[%d,%d]; height=%d; tag=%d; fleshX=%d; fleshY=%d; aabb=(%d,%d)-(%d,%d)',
600 [parentId, children[0], children[1], Integer(height), tag, fleshX, fleshY, aabb.minX, aabb.minY, aabb.maxX, aabb.maxY]),
601 MSG_NOTIFY);
605 // ////////////////////////////////////////////////////////////////////////// //
606 // allocate and return a node to use in the tree
608 var
611 begin
612 // if there is no more allocated node to use
614 begin
616 // allocate more nodes in the tree
620 // initialize the allocated nodes
622 begin
629 // get the next free node
640 //e_WriteLog(Format('tree: allocated node #%d', [result]), MSG_NOTIFY);
644 // release a node
646 begin
656 //e_WriteLog(Format('tree: released node #%d', [nodeId]), MSG_NOTIFY);
660 // insert a leaf node in the tree
661 // the process of inserting a new leaf node in the dynamic tree is described in the book "Introduction to Game Physics with Box2D" by Ian Parberry
663 var
670 begin
671 // if the tree is empty
673 begin
676 exit;
681 // find the best sibling node for the new node
685 begin
689 // compute the merged AABB
694 // compute the cost of making the current node the sibling of the new node
697 // compute the minimum cost of pushing the new node further down the tree (inheritance cost)
700 // compute the cost of descending into the left child
705 // compute the cost of descending into the right child
710 // if the cost of making the current node a sibling of the new node is smaller than the cost of going down into the left or right child
713 // it is cheaper to go down into a child of the current node, choose the best child
714 //currentNodeId = (costLeft < costRight ? leftChild : rightChild);
720 // create a new parent for the new node and the sibling node
728 // if the sibling node was not the root node
730 begin
733 begin
735 end
736 else
737 begin
744 end
745 else
746 begin
747 // if the sibling node was the root node
755 // move up in the tree to change the AABBs that have changed
759 begin
760 // balance the sub-tree of the current node if it is not balanced
770 // recompute the height of the node in the tree
774 // recompute the AABB of the node
784 // remove a leaf node from the tree
786 var
789 begin
793 // if we are removing the root node (root node is a leaf in this case)
800 begin
802 end
803 else
804 begin
808 // if the parent of the node to remove is not the root node
810 begin
811 // destroy the parent node
813 begin
815 end
816 else
817 begin
818 {$IFDEF aabbtree_many_asserts}assert(mNodes[grandParentNodeId].children[TTreeNode.Right] = parentNodeId);{$ENDIF}
824 // now, we need to recompute the AABBs of the node on the path back to the root and make sure that the tree is still balanced
827 begin
828 // balance the current sub-tree if necessary
833 // get the two children of the current node
837 // recompute the AABB and the height of the current node
839 mNodes[currentNodeId].height := dtMaxI(mNodes[leftChildId].height, mNodes[rightChildId].height)+1;
844 end
845 else
846 begin
847 // if the parent of the node to remove is the root node, the sibling node becomes the new root node
855 // balance the sub-tree of a given node using left or right rotations
856 // the rotation schemes are described in the book "Introduction to Game Physics with Box2D" by Ian Parberry
857 // this method returns the new root node id
859 var
863 begin
868 // if the node is a leaf or the height of A's sub-tree is less than 2
869 if (nodeA.leaf) or (nodeA.height < 2) then begin result := nodeId; exit; end; // do not perform any rotation
871 // get the two children nodes
879 // compute the factor of the left and right sub-trees
882 // if the right node C is 2 higher than left node B
884 begin
899 begin
901 begin
903 end
904 else
905 begin
906 {$IFDEF aabbtree_many_asserts}assert(mNodes[nodeC.parentId].children[TTreeNode.Right] = nodeId);{$ENDIF}
909 end
910 else
911 begin
918 // if the right node C was higher than left node B because of the F node
920 begin
925 // recompute the AABB of node A and C
929 // recompute the height of node A and C
934 end
935 else
936 begin
937 // if the right node C was higher than left node B because of node G
942 // recompute the AABB of node A and C
946 // recompute the height of node A and C
953 // return the new root of the sub-tree
955 exit;
958 // if the left node B is 2 higher than right node C
960 begin
975 begin
977 begin
979 end
980 else
981 begin
982 {$IFDEF aabbtree_many_asserts}assert(mNodes[nodeB.parentId].children[TTreeNode.Right] = nodeId);{$ENDIF}
985 end
986 else
987 begin
994 // if the left node B was higher than right node C because of the F node
996 begin
1001 // recompute the AABB of node A and B
1005 // recompute the height of node A and B
1010 end
1011 else
1012 begin
1013 // if the left node B was higher than right node C because of node G
1018 // recompute the AABB of node A and B
1022 // recompute the height of node A and B
1029 // return the new root of the sub-tree
1031 exit;
1034 // if the sub-tree is balanced, return the current root node
1039 // compute the height of a given node in the tree
1041 var
1044 begin
1048 // if the node is a leaf, its height is zero
1051 // compute the height of the left and right sub-tree
1055 // return the height of the node
1060 // internally add an object into the tree
1061 function TDynAABBTreeBase.insertObjectInternal (constref aabb: AABB2D; staticObject: Boolean): Integer;
1062 var
1065 begin
1066 // get the next available node (or allocate new ones if necessary)
1071 // create the fat aabb to use in the tree
1074 begin
1081 // set the height of the node in the tree
1084 // insert the new leaf node in the tree
1090 // return the id of the node
1095 // initialize the tree
1097 var
1099 begin
1106 //memset(mNodes, 0, mAllocCount*TTreeNode.sizeof);
1109 // initialize the allocated nodes
1111 begin
1120 // also, checks if the tree structure is valid (for debugging purpose)
1122 var
1126 begin
1129 // if it is the root
1131 // get the children nodes
1135 begin
1136 {$IFDEF aabbtree_use_floats}
1137 e_WriteLog(Format('AABB:(%f,%f)-(%f,%f); volume=%f; valid=%d; height=%d; leaf=%d', [pNode.aabb.minX, pNode.aabb.minY, pNode.aabb.maxX, pNode.aabb.maxY, pNode.aabb.volume, Integer(pNode.aabb.valid), pNode.height, Integer(pNode.leaf)]), MSG_NOTIFY);
1138 {$ELSE}
1139 e_WriteLog(Format('AABB:(%d,%d)-(%d,%d); volume=%d; valid=%d; height=%d; leaf=%d', [pNode.aabb.minX, pNode.aabb.minY, pNode.aabb.maxX, pNode.aabb.maxY, pNode.aabb.volume, Integer(pNode.aabb.valid), pNode.height, Integer(pNode.leaf)]), MSG_NOTIFY);
1140 {$ENDIF}
1142 begin
1144 {$IFDEF aabbtree_use_floats}
1145 e_WriteLog(Format(' LEAF AABB:(%f,%f)-(%f,%f); valid=%d; volume=%f', [aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, Integer(aabb.valid), aabb.volume]), MSG_NOTIFY);
1146 {$ELSE}
1147 e_WriteLog(Format(' LEAF AABB:(%d,%d)-(%d,%d); valid=%d; volume=%d', [aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, Integer(aabb.valid), aabb.volume]), MSG_NOTIFY);
1148 {$ENDIF}
1153 // if the current node is a leaf
1155 begin
1158 end
1159 else
1160 begin
1163 // check that the children node Ids are valid
1166 // check that the children nodes have the correct parent node
1169 // check the height of node
1172 // check the AABB of the node
1178 // recursively check the children nodes
1185 // also, checks if the tree structure is valid (for debugging purpose)
1187 begin
1188 // recursively check each node
1193 // return `true` from visitor to stop immediately
1194 // checker should check if this node should be considered to further checking
1195 // returns tree node if visitor says stop or -1
1196 function TDynAABBTreeBase.visit (constref caabb: AABB2D; mode: Integer; checker: TVisitCheckerCB; visitor: TQueryOverlapCB; visdg: TQueryOverlapDg; tagmask: Integer): Integer;
1197 const
1199 var
1206 begin
1208 //if not assigned(visitor) and not assigned(visdg) then raise Exception.Create('dyntree: empty visitors aren''t supported');
1214 {$IFDEF aabbtree_query_count}
1217 {$ENDIF}
1219 // start from root node
1220 // we can't have nested functions in generics, sorry
1221 {$IF FALSE}
1223 {$ELSE}
1227 {$ENDIF}
1229 // while there are still nodes to visit
1231 begin
1232 // get the next node id to visit
1233 // we can't have nested functions in generics, sorry
1234 {$IF FALSE}
1236 {$ELSE}
1239 {$ENDIF}
1240 // skip it if it is a nil node
1243 // get the corresponding node
1245 // should we investigate this node?
1248 ModeAABB:
1249 begin
1250 //doNode := caabb.overlaps(node.aabb);
1251 // this gives small speedup (or not...)
1252 // exit with no intersection if found separated along any axis
1257 ModePoint:
1258 begin
1259 //doNode := node.aabb.contains(caabb.minX, caabb.minY);
1260 // this gives small speedup
1261 doNode := (caabb.minX >= node.aabb.minX) and (caabb.minY >= node.aabb.minY) and (caabb.minX <= node.aabb.maxX) and (caabb.minY <= node.aabb.maxY);
1265 begin
1266 // if the node is a leaf
1268 begin
1269 // call visitor on it
1272 begin
1274 // update object vars from cache, so recursive calls to `visit()` will work
1277 // call callbacks
1280 // do some sanity checks
1282 // should we exit?
1284 begin
1288 exit;
1291 end
1292 else
1293 begin
1294 // if the node is not a leaf, we need to visit its children
1295 // we can't have nested functions in generics, sorry
1296 {$IF FALSE}
1299 {$ELSE}
1305 {$ENDIF}
1316 // add `extraAABBGap` to bounding boxes so slight object movement won't cause tree rebuilds
1317 // extra AABB Gap used to allow the collision shape to move a little bit without triggering a large modification of the tree which can be costly
1319 begin
1329 begin
1336 // clear all the nodes and reset the tree
1338 begin
1344 function TDynAABBTreeBase.computeTreeHeight (): Integer; begin result := computeHeight(mRootNodeId); end;
1347 // return the root AABB of the tree
1349 begin
1350 {$IFDEF aabbtree_many_asserts}assert((mRootNodeId >= 0) and (mRootNodeId < mAllocCount));{$ENDIF}
1355 // does the given id represents a valid object?
1356 // WARNING: ids of removed objects can be reused on later insertions!
1358 begin
1363 // get object by nodeid; can return nil for invalid ids
1365 begin
1366 if (nodeid >= 0) and (nodeid < mAllocCount) and (mNodes[nodeid].leaf) then result := mNodes[nodeid].flesh else result := Default(ITP);
1369 // get fat object AABB by nodeid; returns random shit for invalid ids
1371 begin
1372 if (nodeid >= 0) and (nodeid < mAllocCount) and (not mNodes[nodeid].isfree) then aabb := AABB2D.Create(mNodes[nodeid].aabb) else aabb := AABB2D.Create(0, 0, 0, 0);
1376 begin
1378 begin
1380 {$IFDEF aabbtree_use_floats}
1383 {$ELSE}
1386 {$ENDIF}
1387 end
1388 else
1389 begin
1393 //if (nodeid >= 0) and (nodeid < mAllocCount) then mNodes[nodeid].dumpToLog();
1398 // insert an object into the tree
1399 // this method creates a new leaf node in the tree and returns the id of the corresponding node or -1 on error
1400 // AABB for static object will not be "fat" (simple optimization)
1401 // WARNING! inserting the same object several times *WILL* break everything!
1402 function TDynAABBTreeBase.insertObject (flesh: TTreeFlesh; tag: Integer; staticObject: Boolean=false): Integer;
1403 var
1406 begin
1408 begin
1409 {$IFDEF aabbtree_use_floats}
1410 e_WriteLog(Format('trying to insert FUCKED FLESH:(%f,%f)-(%f,%f); volume=%f; valid=%d', [aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, aabb.volume, Integer(aabb.valid)]), MSG_WARNING);
1411 {$ELSE}
1412 e_WriteLog(Format('trying to insert FUCKED FLESH:(%d,%d)-(%d,%d); volume=%d; valid=%d', [aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, aabb.volume, Integer(aabb.valid)]), MSG_WARNING);
1413 {$ENDIF}
1414 //raise Exception.Create('trying to insert invalid flesh in dyntree');
1416 exit;
1419 begin
1420 {$IFDEF aabbtree_use_floats}
1421 e_WriteLog(Format('trying to insert FUCKED AABB:(%f,%f)-(%f,%f); volume=%f; valid=%d', [aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, aabb.volume, Integer(aabb.valid)]), MSG_WARNING);
1422 {$ELSE}
1423 e_WriteLog(Format('trying to insert FUCKED AABB:(%d,%d)-(%d,%d); volume=%d; valid=%d', [aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, aabb.volume, Integer(aabb.valid)]), MSG_WARNING);
1424 {$ENDIF}
1427 exit;
1429 //e_WriteLog(Format('inserting AABB:(%f,%f)-(%f,%f); volume=%f; valid=%d', [aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, aabb.volume, Integer(aabb.valid)]), MSG_NOTIFY);
1442 // remove an object from the tree
1443 // WARNING: ids of removed objects can be reused on later insertions!
1445 begin
1446 if (nodeId < 0) or (nodeId >= mAllocCount) or (not mNodes[nodeId].leaf) then raise Exception.Create('invalid node id in TDynAABBTreeBase');
1447 // remove the node from the tree
1453 function TDynAABBTreeBase.updateObject (nodeId: Integer; forceReinsert: Boolean=false): Boolean; overload;
1454 var
1457 begin
1458 if (nodeId < 0) or (nodeId >= mAllocCount) or (not mNodes[nodeId].leaf) then raise Exception.Create('invalid node id in TDynAABBTreeBase.updateObject');
1460 if not getFleshAABB(newAABB, mNodes[nodeId].flesh, mNodes[nodeId].tag) then raise Exception.Create('invalid flesh dimensions in TDynAABBTreeBase.updateObject');
1461 if not newAABB.valid then raise Exception.Create('invalid flesh aabb in TDynAABBTreeBase.updateObject');
1472 function TDynAABBTreeBase.updateObject (nodeId: Integer; dispX, dispY: TreeNumber; forceReinsert: Boolean=false): Boolean; overload;
1473 var
1477 begin
1478 if (nodeId < 0) or (nodeId >= mAllocCount) or (not mNodes[nodeId].leaf) then raise Exception.Create('invalid node id in TDynAABBTreeBase.updateObject');
1480 if not getFleshAABB(newAABB, mNodes[nodeId].flesh, mNodes[nodeId].tag) then raise Exception.Create('invalid flesh dimensions in TDynAABBTreeBase.updateObject');
1481 if not newAABB.valid then raise Exception.Create('invalid flesh aabb in TDynAABBTreeBase.updateObject');
1486 // if the new AABB is still inside the fat AABB of the node
1488 begin
1493 exit;
1496 // if the new AABB is outside the fat AABB, we remove the corresponding node
1501 // compute the fat AABB by inflating the AABB with a constant gap
1507 begin
1514 // inflate the fat AABB in direction of the linear motion of the AABB
1516 begin
1517 node.aabb.minX += LinearMotionGapMultiplier*dispX {$IFDEF aabbtree_use_floats}{$ELSE}div 10{$ENDIF};
1518 end
1519 else
1520 begin
1521 node.aabb.maxX += LinearMotionGapMultiplier*dispX {$IFDEF aabbtree_use_floats}{$ELSE}div 10{$ENDIF};
1525 begin
1526 node.aabb.minY += LinearMotionGapMultiplier*dispY {$IFDEF aabbtree_use_floats}{$ELSE}div 10{$ENDIF};
1527 end
1528 else
1529 begin
1530 node.aabb.maxY += LinearMotionGapMultiplier*dispY {$IFDEF aabbtree_use_floats}{$ELSE}div 10{$ENDIF};
1535 // reinsert the node into the tree
1543 begin
1548 // report all shapes overlapping with the AABB given in parameter
1549 function TDynAABBTreeBase.aabbQuery (ax, ay, aw, ah: TreeNumber; cb: TQueryOverlapCB; tagmask: Integer=-1): TTreeFlesh;
1550 var
1553 begin
1557 //chkAABB := AABB2D.Create(ax, ay, ax+aw, ay+ah);
1570 begin
1575 // report body that contains the given point, or nil
1576 function TDynAABBTreeBase.pointQuery (ax, ay: TreeNumber; cb: TQueryOverlapCB; tagmask: Integer=-1): TTreeFlesh;
1577 var
1580 begin
1584 {$IFDEF aabbtree_many_asserts}assert((nid < 0) or ((nid >= 0) and (nid < mAllocCount) and (mNodes[nid].leaf)));{$ENDIF}
1591 begin
1596 var
1598 begin
1600 // if the user returned a hitFraction of zero, it means that the raycasting should stop here
1602 begin
1606 exit;
1608 // if the user returned a positive fraction
1610 begin
1611 // we update the maxFraction value and the ray AABB using the new maximum fraction
1613 begin
1617 // fix curb here
1618 //curb := cura+dir*hitFraction;
1627 // segment querying method
1628 function TDynAABBTreeBase.segmentQuery (out qr: TSegmentQueryResult; ax, ay, bx, by: TreeNumber; cb: TSegQueryCallback; tagmask: Integer=-1): Boolean;
1629 var
1637 begin
1658 // normalize
1663 //chkAABB := AABB2D.Create(0, 0, 1, 1);