Debian Codegraph @

src/transform/castletriangles_istrianglespherecollision.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 var
17 Intersection: TVector3;
18 SphereRadiusSqr: Single;
19 I, NextI: integer;
20 {$ifndef HAS_PRECALC_PLANE} TriPlane: TVector4; {$endif}
21 { In case HAS_PRECALC_PLANE is not defined, TriDir is valid only once
22 TriPlane is calculated. }
23 TriDir: TVector3 absolute TriPlane;
24 begin
25 SphereRadiusSqr := Sqr(SphereRadius);
26
27 (* Is SphereCenter within the radius of one of triangle corners.
28 Note that this case is necessary, it is not fully catched by
29 "SphereCenter close to one of the triangles' edges" lower. *)
30 if (PointsDistanceSqr(Tri.Data[0], SphereCenter) <= SphereRadiusSqr) or
31 (PointsDistanceSqr(Tri.Data[1], SphereCenter) <= SphereRadiusSqr) or
32 (PointsDistanceSqr(Tri.Data[2], SphereCenter) <= SphereRadiusSqr) then
33 Exit(true);
34
35 {$ifndef HAS_PRECALC_PLANE} TriPlane := Tri.Plane; {$endif}
36 (* Is SphereCenter within the triangle. *)
37 Intersection := PointOnPlaneClosestToPoint(TriPlane, SphereCenter);
38 if (PointsDistanceSqr(SphereCenter, Intersection) <= SphereRadiusSqr) and
39 IsPointOnTrianglePlaneWithinTriangle(Intersection, Tri, TriDir) then
40 Exit(true);
41
42 (* Is SphereCenter close to one of the triangles' edges. *)
43 for I := 0 to 2 do
44 begin
45 NextI := (I + 1) mod 3;
46 Intersection := PointOnLineClosestToPoint(Tri.Data[I],
47 Tri.Data[NextI] - Tri.Data[I], SphereCenter);
48 if IsPointOnSegmentLineWithinSegment(Intersection, Tri.Data[I], Tri.Data[NextI]) and
49 (PointsDistanceSqr(SphereCenter, Intersection) <= SphereRadiusSqr) then
50 Exit(true);
51 end;
52
53 Result := false;
54 end;