Debian Codegraph @

src/scene/castlescenecore_collisions.inc

raw
1 {
2 Copyright 2003-2022 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 all XxxCollision methods in CastleSceneCore unit. }
17
18 { TCastleSceneCore ----------------------------------------------------------- }
19
20 function TCastleSceneCore.LocalHeightCollision(const APosition, GravityUp: TVector3;
21 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc;
22 out AboveHeight: Single; out AboveGround: PTriangle): boolean;
23 var
24 NewResult: boolean;
25 NewAboveHeight: Single;
26 NewAboveGround: PTriangle;
27 BoxIntersection: TVector3;
28 begin
29 if CheckCollides then
30 begin
31 Result := inherited LocalHeightCollision(APosition, GravityUp,
32 TrianglesToIgnoreFunc, AboveHeight, AboveGround);
33
34 if UseInternalOctreeCollisions then
35 begin
36 NewResult := InternalOctreeCollisions.HeightCollision(APosition, GravityUp,
37 NewAboveHeight, NewAboveGround, nil, TrianglesToIgnoreFunc);
38 end else
39 begin
40 { collide with LocalBoundingBoxNoChildren }
41 { Using TryRayEntrance here would also be sensible, but sometimes too eager:
42 In case creature walks over an item, it would cause the item to go upward
43 (because the creature is collidable (item is not), so item's gravity
44 would cause it to grow). Sometimes also the creatures would too easily
45 climb on top of each other.
46 It may be changed in the future back into TryRayEntrance? Item problems
47 could be solved by using GrowSpeed = 0 for items. }
48 NewResult := LocalBoundingBoxNoChildren.TryRayClosestIntersection(BoxIntersection, NewAboveHeight, APosition, -GravityUp);
49 NewAboveGround := nil;
50 end;
51
52 if NewResult and (NewAboveHeight < AboveHeight) then
53 begin
54 Result := NewResult;
55 AboveHeight := NewAboveHeight;
56 AboveGround := NewAboveGround;
57 end;
58 end else
59 begin
60 Result := false;
61 AboveHeight := MaxSingle;
62 AboveGround := nil;
63 end;
64 end;
65
66 function TCastleSceneCore.LocalBoundingVolumeMoveCollision(
67 const OldPos, NewPos: TVector3;
68 const IsRadius: boolean; const Radius: Single;
69 const OldBox, NewBox: TBox3D): boolean;
70 var
71 MyBox: TBox3D;
72
73 { P1 is closer to our middle than P2. }
74 function CloserToMiddle(const P1, P2: TVector3): boolean;
75 var
76 M: TVector3;
77 begin
78 M := Middle;
79 Result := PointsDistanceSqr(M, P1) < PointsDistanceSqr(M, P2);
80 end;
81
82 var
83 OldCollision, NewCollision: boolean;
84 begin
85 { check collision with our bounding box.
86
87 We do not look here at our own sphere. When other objects move,
88 it's better to treat ourself as larger (not smaller), to prevent
89 collisions rather then allow them in case of uncertainty.
90 So we ignore Self.Sphere method.
91
92 But we do take into account that other (moving) object may prefer to
93 be treated as a sphere, so we take into account IsRadius, Radius parameters.
94 This allows a player to climb on top of dead corpses (with flat
95 bbox), since player's sphere is slightly above the ground.
96 And it allows the missiles (like arrow) to use their spheres
97 for determining what is hit, which is good because e.g. arrow
98 has a very large bbox, sphere is much better (otherwise it may be too easy
99 to hit with arrow). }
100
101 Result := true;
102
103 MyBox := LocalBoundingBoxNoChildren;
104
105 if IsRadius then
106 begin
107 OldCollision := MyBox.SphereCollision(OldPos, Radius);
108 NewCollision := MyBox.SphereCollision(NewPos, Radius);
109 end else
110 begin
111 OldCollision := MyBox.Collision(OldBox);
112 NewCollision := MyBox.Collision(NewBox);
113 end;
114
115 if NewCollision then
116 begin
117 { We now know that we have a collision with new position.
118 Strictly thinking, move should be disallowed
119 (we should exit with false). But it's not that simple.
120
121 There is a weakness in collision checking with dynamic objects,
122 like creatures, because when LifeTime changes then effectively
123 LocalBoundingBoxNoChildren changes, and there is no way how I can prevent collisions
124 from occuring (we cannot stop/reverse an arbitrary animation,
125 this would look bad and require AI preparations, see @link(Sphere) comments).
126
127 So we must allow some moves, to allow player/creature that is already
128 stuck (already collidable with Self) to get out of the collision.
129 To do this, we are going to enable a move, only if *old position
130 was already collidable (so the other object is stuck with us already)
131 and new position is further from us (so the other object tries
132 to get unstuck)". }
133 if (not OldCollision) or CloserToMiddle(NewPos, OldPos) then
134 Exit(false);
135 end else
136 if (not OldCollision) and
137 { new and old positions are Ok (not collidable), so check also
138 line segment. Otherwise fast moving player could run through slim
139 creature. }
140 MyBox.SegmentCollision(OldPos, NewPos) then
141 Exit(false);
142
143 { Simpler implementation that doesn't allow others to become "unstuck".
144 It's also slightly less optimal, as internally LocalBoundingBoxNoChildren and CheckCollides
145 will be calculated many times (although they should be lighting-fast,
146 still their time matters, as this is the basis of our AI and may be called
147 many times per frame).
148 OTOH, this simpler version is a little cleaner: it delegates work
149 to other methods, they may use LocalBoundingBoxNoChildren or something else.
150
151 if IsRadius then
152 Result := not ( CheckCollides and
153 ( LocalSegmentCollision(OldPos, ProposedNewPos, TrianglesToIgnoreFunc, false) or
154 LocalSphereCollision(ProposedNewPos, Radius, TrianglesToIgnoreFunc) ) )
155 else
156 Result := not ( CheckCollides and
157 ( LocalSegmentCollision(OldPos, ProposedNewPos, TrianglesToIgnoreFunc, false) or
158 LocalBoxCollision(NewBox, TrianglesToIgnoreFunc) ) );
159 }
160 end;
161
162 function TCastleSceneCore.LocalMoveCollision(
163 const OldPos, ProposedNewPos: TVector3; out NewPos: TVector3;
164 const IsRadius: boolean; const Radius: Single;
165 const OldBox, NewBox: TBox3D;
166 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
167 begin
168 Result := true;
169 NewPos := ProposedNewPos;
170
171 if CheckCollides then
172 begin
173 if UseInternalOctreeCollisions then
174 begin
175 Result := InternalOctreeCollisions.MoveCollision(OldPos, ProposedNewPos, NewPos,
176 IsRadius, Radius, OldBox, NewBox, nil, TrianglesToIgnoreFunc);
177 end else
178 begin
179 { no wall-sliding with bounding volume }
180 Result := LocalBoundingVolumeMoveCollision(OldPos, ProposedNewPos,
181 IsRadius, Radius, OldBox, NewBox);
182 end;
183 if not Result then Exit;
184
185 { no wall-sliding in inherited }
186 Result := inherited LocalMoveCollision(OldPos, ProposedNewPos,
187 IsRadius, Radius, OldBox, NewBox, TrianglesToIgnoreFunc);
188 end;
189 end;
190
191 function TCastleSceneCore.LocalMoveCollision(
192 const OldPos, NewPos: TVector3;
193 const IsRadius: boolean; const Radius: Single;
194 const OldBox, NewBox: TBox3D;
195 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
196 begin
197 Result := true;
198
199 if CheckCollides then
200 begin
201 if UseInternalOctreeCollisions then
202 begin
203 Result := InternalOctreeCollisions.MoveCollision(OldPos, NewPos,
204 IsRadius, Radius, OldBox, NewBox, nil, TrianglesToIgnoreFunc);
205 end else
206 begin
207 Result := LocalBoundingVolumeMoveCollision(OldPos, NewPos,
208 IsRadius, Radius, OldBox, NewBox);
209 end;
210 if not Result then Exit;
211
212 Result := inherited LocalMoveCollision(OldPos, NewPos,
213 IsRadius, Radius, OldBox, NewBox, TrianglesToIgnoreFunc);
214 end;
215 end;
216
217 function TCastleSceneCore.LocalSegmentCollision(const Pos1, Pos2: TVector3;
218 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc;
219 const ALineOfSight: boolean): boolean;
220 begin
221 Result := false;
222
223 if CheckCollides or (ALineOfSight and Exists) then
224 begin
225 if UseInternalOctreeCollisions then
226 begin
227 Result := InternalOctreeCollisions.IsSegmentCollision(
228 Pos1, Pos2, nil, false, TrianglesToIgnoreFunc)
229 end else
230 begin
231 Result := LocalBoundingBoxNoChildren.SegmentCollision(Pos1, Pos2);
232 end;
233 if Result then Exit;
234
235 Result := inherited LocalSegmentCollision(Pos1, Pos2, TrianglesToIgnoreFunc, ALineOfSight);
236 end;
237 end;
238
239 function TCastleSceneCore.LocalSphereCollision(
240 const Pos: TVector3; const Radius: Single;
241 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
242 begin
243 Result := false;
244
245 if CheckCollides then
246 begin
247 if UseInternalOctreeCollisions then
248 begin
249 Result := InternalOctreeCollisions.IsSphereCollision(
250 Pos, Radius, nil, TrianglesToIgnoreFunc)
251 end else
252 begin
253 Result := LocalBoundingBoxNoChildren.SphereCollision(Pos, Radius);
254 end;
255 if Result then Exit;
256
257 Result := inherited LocalSphereCollision(Pos, Radius, TrianglesToIgnoreFunc);
258 end;
259 end;
260
261 function TCastleSceneCore.LocalSphereCollision2D(
262 const Pos: TVector2; const Radius: Single;
263 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc;
264 const Details: TCollisionDetails): boolean;
265 begin
266 Result := false;
267
268 if CheckCollides then
269 begin
270 if UseInternalOctreeCollisions then
271 begin
272 Result := InternalOctreeCollisions.IsSphereCollision2D(
273 Pos, Radius, nil, TrianglesToIgnoreFunc);
274 end else
275 begin
276 Result := LocalBoundingBoxNoChildren.SphereCollision2D(Pos, Radius);
277 end;
278
279 if Result then
280 begin
281 if Details <> nil then
282 begin
283 Details.Clear;
284 Details.Add(Self);
285 end;
286 Exit;
287 end;
288
289 Result := inherited LocalSphereCollision2D(Pos, Radius, TrianglesToIgnoreFunc, Details);
290 end;
291 end;
292
293 function TCastleSceneCore.LocalPointCollision2D(const Point: TVector2;
294 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
295 begin
296 Result := false;
297
298 if CheckCollides then
299 begin
300 if UseInternalOctreeCollisions then
301 begin
302 Result := InternalOctreeCollisions.IsPointCollision2D(
303 Point, nil, TrianglesToIgnoreFunc);
304 end else
305 begin
306 Result := LocalBoundingBoxNoChildren.Contains2D(Point);
307 end;
308 if Result then Exit;
309
310 Result := inherited LocalPointCollision2D(Point, TrianglesToIgnoreFunc);
311 end;
312 end;
313
314 function TCastleSceneCore.LocalBoxCollision(const Box: TBox3D;
315 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
316 begin
317 Result := false;
318
319 if CheckCollides then
320 begin
321 if UseInternalOctreeCollisions then
322 begin
323 Result := InternalOctreeCollisions.IsBoxCollision(
324 Box, nil, TrianglesToIgnoreFunc);
325 end else
326 begin
327 Result := LocalBoundingBoxNoChildren.Collision(Box);
328 end;
329 if Result then Exit;
330
331 Result := inherited LocalBoxCollision(Box, TrianglesToIgnoreFunc);
332 end;
333 end;
334
335 function TCastleSceneCore.LocalRayCollision(const RayOrigin, RayDirection: TVector3;
336 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): TRayCollision;
337 var
338 Triangle: PTriangle;
339 Intersection: TVector3;
340 IntersectionDistance: Single;
341 NewNode: PRayCollisionNode;
342 NewResult: TRayCollision;
343 begin
344 Result := nil;
345
346 if CheckPickable then
347 begin
348 if UseInternalOctreeCollisions then
349 begin
350 Triangle := InternalOctreeCollisions.RayCollision(
351 Intersection, IntersectionDistance, RayOrigin, RayDirection,
352 { ReturnClosestIntersection } true,
353 { TriangleToIgnore } nil,
354 { IgnoreMarginAtStart } false, TrianglesToIgnoreFunc);
355 if Triangle <> nil then
356 begin
357 Result := TRayCollision.Create;
358 Result.Distance := IntersectionDistance;
359 NewNode := PRayCollisionNode(Result.Add);
360 NewNode^.Item := Self;
361 NewNode^.Point := Intersection;
362 NewNode^.Triangle := Triangle;
363 NewNode^.RayOrigin := RayOrigin;
364 NewNode^.RayDirection := RayDirection;
365 end;
366 end else
367 begin
368 if LocalBoundingBoxNoChildren.TryRayEntrance(Intersection, IntersectionDistance, RayOrigin, RayDirection) then
369 begin
370 Result := TRayCollision.Create;
371 Result.Distance := IntersectionDistance;
372
373 NewNode := PRayCollisionNode(Result.Add);
374 NewNode^.Item := Self;
375 NewNode^.Point := Intersection;
376 NewNode^.Triangle := nil;
377 NewNode^.RayOrigin := RayOrigin;
378 NewNode^.RayDirection := RayDirection;
379 end;
380 end;
381
382 NewResult := inherited LocalRayCollision(RayOrigin, RayDirection, TrianglesToIgnoreFunc);
383 if NewResult <> nil then
384 begin
385 if (Result = nil) or (NewResult.Distance < Result.Distance) then
386 begin
387 SysUtils.FreeAndNil(Result);
388 Result := NewResult;
389 end else
390 FreeAndNil(NewResult);
391 end;
392 end;
393 end;