Debian Codegraph @

src/scene/castleinternaltriangleoctree_raysegmentcollisions.inc

raw
1 {
2 Copyright 2003-2018 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Implementation of TTriangleOctreeNode.CommonSegmentLeaf and TTriangleOctreeNode.CommonRayLeaf.
17 Uses SEGMENT_COLLISION to differentiate between segment and ray case. }
18
19 {$ifdef SEGMENT_COLLISION}
20 var
21 RayOrigin: TVector3 absolute Pos1;
22 RayDirection: TVector3;
23 {$endif}
24
25 function TryTriangleCollision(
26 out Intersection: TVector3;
27 out IntersectionDistance: Single;
28 var Item: TTriangle): boolean;
29 begin
30 Result:=
31 {$ifdef SEGMENT_COLLISION}
32 Item.SegmentDirCollision(Intersection, IntersectionDistance,
33 Pos1, RayDirection, Tag)
34 {$else}
35 Item.RayCollision(Intersection, IntersectionDistance,
36 RayOrigin, RayDirection, Tag)
37 {$endif}
38 end;
39
40 function FoundIntersection(
41 out Intersection: TVector3;
42 out IntersectionDistance: Single;
43 const ItemPtr: PTriangle): boolean;
44 begin
45 Result := TryTriangleCollision(Intersection, IntersectionDistance, ItemPtr^) and
46 ( TriangleToIgnore <> ItemPtr ) and
47 ( (not IgnoreMarginAtStart) or
48 (not TVector3.Equals(Intersection, RayOrigin)) ) and
49 ( (not Assigned(TrianglesToIgnoreFunc)) or
50 (not TrianglesToIgnoreFunc(ParentTree, ItemPtr)) );
51 end;
52
53 var
54 Iterate_ItemNumber: integer;
55 Iterate_Item: PTriangle;
56 Iterate_Intersection: TVector3;
57 Iterate_IntersectionDistance: Single;
58 begin
59 {$ifdef SEGMENT_COLLISION}
60 RayDirection := Pos2 - Pos1;
61 {$endif}
62
63 // Always define to something, to avoid warnings and to be deterministic
64 // (although user code should not use it if Result = nil).
65 IntersectionDistance := 0;
66
67 if ReturnClosestIntersection then
68 begin
69 Result := nil;
70
71 for Iterate_ItemNumber := 0 to ItemsCount - 1 do
72 begin
73 Iterate_Item := Items[Iterate_ItemNumber];
74 if FoundIntersection(Iterate_Intersection, Iterate_IntersectionDistance, Iterate_Item) then
75 begin
76 if (Result = nil) or
77 (Iterate_IntersectionDistance < IntersectionDistance) then
78 begin
79 Result := Iterate_Item;
80 Intersection := Iterate_Intersection;
81 IntersectionDistance := Iterate_IntersectionDistance;
82 end;
83 end;
84 end;
85 end else
86 begin
87 for Iterate_ItemNumber := 0 to ItemsCount - 1 do
88 begin
89 Iterate_Item := Items[Iterate_ItemNumber];
90 if FoundIntersection(Iterate_Intersection, Iterate_IntersectionDistance, Iterate_Item) then
91 begin
92 Result := Iterate_Item;
93 Intersection := Iterate_Intersection;
94 IntersectionDistance := Iterate_IntersectionDistance;
95 Exit;
96 end;
97 end;
98 Exit(nil);
99 end;
100 end;