| 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 |
{ Octrees that resolve collisions with TTriangle triangles (TBaseTrianglesOctree). } |
| 17 |
unit CastleInternalBaseTriangleOctree; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
{$I octreeconf.inc} |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
uses SysUtils, Generics.Collections, |
| 25 |
CastleVectors, CastleUtils, CastleBoxes, |
| 26 |
CastleInternalOctree, CastleTriangles; |
| 27 |
|
| 28 |
{ TBaseTrianglesOctree ----------------------------------------------------------- } |
| 29 |
|
| 30 |
type |
| 31 |
{ } |
| 32 |
TCollisionCount = Int64; |
| 33 |
|
| 34 |
{ } |
| 35 |
TBaseTrianglesOctree = class; |
| 36 |
|
| 37 |
{ } |
| 38 |
TBaseTrianglesOctreeNode = class(TOctreeNode) |
| 39 |
protected |
| 40 |
{ These realize the common implementation of SphereCollision: |
| 41 |
traversing down the octree nodes. They take care of traversing |
| 42 |
down the non-leaf nodes, you only have to override |
| 43 |
the CommonXxxLeaf versions where you handle the leaves |
| 44 |
(and you have to call CommonXxx from normal Xxx routines). } |
| 45 |
function CommonSphere(const pos: TVector3; |
| 46 |
const Radius: Single; |
| 47 |
const TriangleToIgnore: PTriangle; |
| 48 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 49 |
|
| 50 |
function CommonSphereLeaf(const pos: TVector3; |
| 51 |
const Radius: Single; |
| 52 |
const TriangleToIgnore: PTriangle; |
| 53 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 54 |
|
| 55 |
function CommonSphere2D(const pos: TVector2; |
| 56 |
const Radius: Single; |
| 57 |
const TriangleToIgnore: PTriangle; |
| 58 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 59 |
|
| 60 |
function CommonSphere2DLeaf(const pos: TVector2; |
| 61 |
const Radius: Single; |
| 62 |
const TriangleToIgnore: PTriangle; |
| 63 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 64 |
|
| 65 |
function CommonPoint2D(const Point: TVector2; |
| 66 |
const TriangleToIgnore: PTriangle; |
| 67 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 68 |
|
| 69 |
function CommonPoint2DLeaf(const Point: TVector2; |
| 70 |
const TriangleToIgnore: PTriangle; |
| 71 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 72 |
|
| 73 |
function CommonBox(const ABox: TBox3D; |
| 74 |
const TriangleToIgnore: PTriangle; |
| 75 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 76 |
|
| 77 |
function CommonBoxLeaf(const ABox: TBox3D; |
| 78 |
const TriangleToIgnore: PTriangle; |
| 79 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 80 |
|
| 81 |
function CommonSegment( |
| 82 |
out Intersection: TVector3; |
| 83 |
out IntersectionDistance: Single; |
| 84 |
const pos1, pos2: TVector3; |
| 85 |
const Tag: TMailboxTag; |
| 86 |
const ReturnClosestIntersection: boolean; |
| 87 |
const TriangleToIgnore: PTriangle; |
| 88 |
const IgnoreMarginAtStart: boolean; |
| 89 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 90 |
|
| 91 |
function CommonSegmentLeaf( |
| 92 |
out Intersection: TVector3; |
| 93 |
out IntersectionDistance: Single; |
| 94 |
const pos1, pos2: TVector3; |
| 95 |
const Tag: TMailboxTag; |
| 96 |
const ReturnClosestIntersection: boolean; |
| 97 |
const TriangleToIgnore: PTriangle; |
| 98 |
const IgnoreMarginAtStart: boolean; |
| 99 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 100 |
|
| 101 |
function CommonRay( |
| 102 |
out Intersection: TVector3; |
| 103 |
out IntersectionDistance: Single; |
| 104 |
const RayOrigin, RayDirection: TVector3; |
| 105 |
const Tag: TMailboxTag; |
| 106 |
const ReturnClosestIntersection: boolean; |
| 107 |
const TriangleToIgnore: PTriangle; |
| 108 |
const IgnoreMarginAtStart: boolean; |
| 109 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 110 |
|
| 111 |
function CommonRayLeaf( |
| 112 |
out Intersection: TVector3; |
| 113 |
out IntersectionDistance: Single; |
| 114 |
const RayOrigin, RayDirection: TVector3; |
| 115 |
const Tag: TMailboxTag; |
| 116 |
const ReturnClosestIntersection: boolean; |
| 117 |
const TriangleToIgnore: PTriangle; |
| 118 |
const IgnoreMarginAtStart: boolean; |
| 119 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 120 |
public |
| 121 |
{ See TBaseTrianglesOctree for documentation of these routines. |
| 122 |
|
| 123 |
Note that methods here do not try to limit detected intersections |
| 124 |
to their boxes. If you will insert a large triangle into a node, |
| 125 |
that is partially inside and partially outside of this node, |
| 126 |
the collision methods may find an intersection outside of this node. |
| 127 |
|
| 128 |
This is not be a problem for a root node, since the root node has |
| 129 |
a box such that every triangle is completely inside. |
| 130 |
But it is important to remember when you implement recursive |
| 131 |
*Collision calls in nodes: if you want to query your subnodes |
| 132 |
in some particular order (for example to honour ReturnClosestIntersection |
| 133 |
= @true), then remember that one subnode may detect a collision |
| 134 |
that in fact happened in other subnode. |
| 135 |
|
| 136 |
@groupBegin } |
| 137 |
function SphereCollision(const pos: TVector3; |
| 138 |
const Radius: Single; |
| 139 |
const TriangleToIgnore: PTriangle; |
| 140 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 141 |
|
| 142 |
function IsSphereCollision(const pos: TVector3; |
| 143 |
const Radius: Single; |
| 144 |
const TriangleToIgnore: PTriangle; |
| 145 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; virtual; abstract; |
| 146 |
|
| 147 |
function SphereCollision2D(const pos: TVector2; |
| 148 |
const Radius: Single; |
| 149 |
const TriangleToIgnore: PTriangle; |
| 150 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 151 |
|
| 152 |
function IsSphereCollision2D(const pos: TVector2; |
| 153 |
const Radius: Single; |
| 154 |
const TriangleToIgnore: PTriangle; |
| 155 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; virtual; abstract; |
| 156 |
|
| 157 |
function PointCollision2D(const Point: TVector2; |
| 158 |
const TriangleToIgnore: PTriangle; |
| 159 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 160 |
|
| 161 |
function IsPointCollision2D(const Point: TVector2; |
| 162 |
const TriangleToIgnore: PTriangle; |
| 163 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; virtual; abstract; |
| 164 |
|
| 165 |
function BoxCollision(const ABox: TBox3D; |
| 166 |
const TriangleToIgnore: PTriangle; |
| 167 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 168 |
|
| 169 |
function IsBoxCollision(const ABox: TBox3D; |
| 170 |
const TriangleToIgnore: PTriangle; |
| 171 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; virtual; abstract; |
| 172 |
|
| 173 |
function SegmentCollision( |
| 174 |
out Intersection: TVector3; |
| 175 |
out IntersectionDistance: Single; |
| 176 |
const pos1, pos2: TVector3; |
| 177 |
const Tag: TMailboxTag; |
| 178 |
const ReturnClosestIntersection: boolean; |
| 179 |
const TriangleToIgnore: PTriangle; |
| 180 |
const IgnoreMarginAtStart: boolean; |
| 181 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 182 |
|
| 183 |
function IsSegmentCollision( |
| 184 |
const pos1, pos2: TVector3; |
| 185 |
const Tag: TMailboxTag; |
| 186 |
const TriangleToIgnore: PTriangle; |
| 187 |
const IgnoreMarginAtStart: boolean; |
| 188 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; virtual; abstract; |
| 189 |
|
| 190 |
function RayCollision( |
| 191 |
out Intersection: TVector3; |
| 192 |
out IntersectionDistance: Single; |
| 193 |
const RayOrigin, RayDirection: TVector3; |
| 194 |
const Tag: TMailboxTag; |
| 195 |
const ReturnClosestIntersection: boolean; |
| 196 |
const TriangleToIgnore: PTriangle; |
| 197 |
const IgnoreMarginAtStart: boolean; |
| 198 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; virtual; abstract; |
| 199 |
|
| 200 |
function IsRayCollision( |
| 201 |
const RayOrigin, RayDirection: TVector3; |
| 202 |
const Tag: TMailboxTag; |
| 203 |
const TriangleToIgnore: PTriangle; |
| 204 |
const IgnoreMarginAtStart: boolean; |
| 205 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; virtual; abstract; |
| 206 |
{ @groupEnd } |
| 207 |
end; |
| 208 |
|
| 209 |
{ Callback for @link(TBaseTrianglesOctree.EnumerateTriangles). } |
| 210 |
TEnumerateTriangleFunc = procedure (const Triangle: PTriangle) of object; |
| 211 |
|
| 212 |
{ Abstract class for octrees that can check and return collisions |
| 213 |
with TTriangle. |
| 214 |
|
| 215 |
Octree node class used by this must be a TBaseTrianglesOctreeNode descendant. |
| 216 |
|
| 217 |
In a simple case, this is an ancestor of TTriangleOctree, |
| 218 |
that is just an octree storing TTriangle. But it's also an |
| 219 |
ancestor of TShapeOctree, since each shape has also a |
| 220 |
triangle octree. This way, TShapeOctree can calculate collisions |
| 221 |
with TTriangle, even though it doesn't directly store TTriangle items. } |
| 222 |
TBaseTrianglesOctree = class(TOctree) |
| 223 |
private |
| 224 |
{ Return NextFreeTag and increment it (for the future AssignNewTag). |
| 225 |
|
| 226 |
This guarantees that NextFreeTag is incremented immediately, |
| 227 |
so it will not be reused by some other routine. For example |
| 228 |
if your collision query will cause another collision query |
| 229 |
inside, that calls inside another AssignNewTag, everything will work OK. } |
| 230 |
function AssignNewTag: TMailboxTag; |
| 231 |
public |
| 232 |
{ Collision checking using the octree. |
| 233 |
|
| 234 |
SegmentCollision checks for collision between a line segment and tree items. |
| 235 |
|
| 236 |
SphereCollision checks for collision with a sphere. |
| 237 |
|
| 238 |
BoxCollision checks for collision with a box (axis-aligned, TBox3D type). |
| 239 |
|
| 240 |
RayCollision checks for collision with a ray. |
| 241 |
|
| 242 |
All there methods return nil if there is no collision, or a pointer |
| 243 |
to colliding item. |
| 244 |
|
| 245 |
@param(ReturnClosestIntersection |
| 246 |
|
| 247 |
If @false, then any collision detected is returned. |
| 248 |
For routines that don't have ReturnClosestIntersection parameter |
| 249 |
(SphereCollision, BoxCollision) always any collision is returned. |
| 250 |
|
| 251 |
If this is @true, then the collision closest to RayOrigin (for RayCollision) |
| 252 |
or Pos1 (for SegmentCollision) is returned. This makes the collision |
| 253 |
somewhat slower (as we have to check all collisions, while |
| 254 |
for ReturnClosestIntersection = @false we can terminate at first |
| 255 |
collision found.) |
| 256 |
|
| 257 |
The versions that return boolean value (IsXxxCollision) don't |
| 258 |
take this parameter, as they are naturally interested in existence |
| 259 |
of @italic(any) intersection.) |
| 260 |
|
| 261 |
@param(TriangleToIgnore |
| 262 |
|
| 263 |
If this is non-nil, then Segment/RayCollision assume that there |
| 264 |
is @italic(never) a collision with this octree item. |
| 265 |
It's never returned as collidable item. |
| 266 |
|
| 267 |
This is useful for recursive ray-tracer, when you start tracing |
| 268 |
from some existing face (octree item). In this case, you don't |
| 269 |
want to "hit" the starting face. So you can pass this face |
| 270 |
as TriangleToIgnore. |
| 271 |
|
| 272 |
Note that IgnoreMarginAtStart helps with the same problem, |
| 273 |
although a little differently.) |
| 274 |
|
| 275 |
@param(TrianglesToIgnoreFunc |
| 276 |
|
| 277 |
If assigned, then items for which TrianglesToIgnoreFunc returns @true |
| 278 |
will be ignored. This is a more general mechanism than |
| 279 |
TriangleToIgnore, as you can ignore many items, you can also |
| 280 |
make some condition to ignore --- for example, you can ignore |
| 281 |
partially transparent items.) |
| 282 |
|
| 283 |
@param(IgnoreMarginAtStart |
| 284 |
|
| 285 |
If @true, then collisions that happen very very close to RayOrigin (or Pos1 |
| 286 |
for SegmentCollision) will be ignored. |
| 287 |
|
| 288 |
This is another thing helpful for recursive ray-tracers: |
| 289 |
you don't want to hit the starting face, or any coplanar faces, |
| 290 |
when tracing reflected/refracted/shadow ray. |
| 291 |
|
| 292 |
Note that if you know actual pointer of your face, it's better to use |
| 293 |
TriangleToIgnore --- TriangleToIgnore is a 100% guaranteed |
| 294 |
stable solution, while IgnoreMarginAtStart necessarily has some |
| 295 |
"epsilon" constant that determines which items are ignored. |
| 296 |
This epsilon may be too large, or too small, in some cases. |
| 297 |
|
| 298 |
In practice, recursive ray-tracers should use both |
| 299 |
TriangleToIgnore (to avoid collisions with starting face) |
| 300 |
and IgnoreMarginAtStart = @true (to avoid collisions with faces |
| 301 |
coplanar with starting face).) |
| 302 |
|
| 303 |
@param(IntersectionDistance |
| 304 |
For RayCollision: |
| 305 |
Returned IntersectionDistance is the distance along the RayDirection: |
| 306 |
smaller IntersectionDistance, closer to RayOrigin. |
| 307 |
IntersectionDistance is always >= 0. |
| 308 |
Intersection is always equal to RayOrigin + RayDirection * IntersectionDistance. |
| 309 |
|
| 310 |
For SegmentCollision: analogously, |
| 311 |
IntersectionDistance is along Pos2 - Pos1. |
| 312 |
IntersectionDistance is always in 0...1. |
| 313 |
Intersectio is always equal to Pos1 + (Pos2 - Pos1) * IntersectionDistance. |
| 314 |
) |
| 315 |
|
| 316 |
@groupBegin |
| 317 |
} |
| 318 |
function SegmentCollision( |
| 319 |
out Intersection: TVector3; |
| 320 |
out IntersectionDistance: Single; |
| 321 |
const pos1, pos2: TVector3; |
| 322 |
const ReturnClosestIntersection: boolean; |
| 323 |
const TriangleToIgnore: PTriangle; |
| 324 |
const IgnoreMarginAtStart: boolean; |
| 325 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; overload; |
| 326 |
|
| 327 |
function SegmentCollision( |
| 328 |
out Intersection: TVector3; |
| 329 |
const pos1, pos2: TVector3; |
| 330 |
const ReturnClosestIntersection: boolean; |
| 331 |
const TriangleToIgnore: PTriangle; |
| 332 |
const IgnoreMarginAtStart: boolean; |
| 333 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; overload; |
| 334 |
|
| 335 |
function SegmentCollision( |
| 336 |
out IntersectionDistance: Single; |
| 337 |
const pos1, pos2: TVector3; |
| 338 |
const ReturnClosestIntersection: boolean; |
| 339 |
const TriangleToIgnore: PTriangle; |
| 340 |
const IgnoreMarginAtStart: boolean; |
| 341 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; overload; |
| 342 |
|
| 343 |
function SegmentCollision( |
| 344 |
const pos1, pos2: TVector3; |
| 345 |
const ReturnClosestIntersection: boolean; |
| 346 |
const TriangleToIgnore: PTriangle; |
| 347 |
const IgnoreMarginAtStart: boolean; |
| 348 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; overload; |
| 349 |
|
| 350 |
function IsSegmentCollision( |
| 351 |
const pos1, pos2: TVector3; |
| 352 |
const TriangleToIgnore: PTriangle; |
| 353 |
const IgnoreMarginAtStart: boolean; |
| 354 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 355 |
|
| 356 |
function SphereCollision(const pos: TVector3; |
| 357 |
const Radius: Single; |
| 358 |
const TriangleToIgnore: PTriangle; |
| 359 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 360 |
|
| 361 |
function IsSphereCollision(const pos: TVector3; |
| 362 |
const Radius: Single; |
| 363 |
const TriangleToIgnore: PTriangle; |
| 364 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 365 |
|
| 366 |
function SphereCollision2D(const pos: TVector2; |
| 367 |
const Radius: Single; |
| 368 |
const TriangleToIgnore: PTriangle; |
| 369 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 370 |
|
| 371 |
function IsSphereCollision2D(const pos: TVector2; |
| 372 |
const Radius: Single; |
| 373 |
const TriangleToIgnore: PTriangle; |
| 374 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 375 |
|
| 376 |
function PointCollision2D(const Point: TVector2; |
| 377 |
const TriangleToIgnore: PTriangle; |
| 378 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 379 |
|
| 380 |
function IsPointCollision2D(const Point: TVector2; |
| 381 |
const TriangleToIgnore: PTriangle; |
| 382 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 383 |
|
| 384 |
function BoxCollision(const ABox: TBox3D; |
| 385 |
const TriangleToIgnore: PTriangle; |
| 386 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 387 |
|
| 388 |
function IsBoxCollision(const ABox: TBox3D; |
| 389 |
const TriangleToIgnore: PTriangle; |
| 390 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 391 |
|
| 392 |
function RayCollision( |
| 393 |
out Intersection: TVector3; |
| 394 |
out IntersectionDistance: Single; |
| 395 |
const RayOrigin, RayDirection: TVector3; |
| 396 |
const ReturnClosestIntersection: boolean; |
| 397 |
const TriangleToIgnore: PTriangle; |
| 398 |
const IgnoreMarginAtStart: boolean; |
| 399 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; overload; |
| 400 |
|
| 401 |
function RayCollision( |
| 402 |
out Intersection: TVector3; |
| 403 |
const RayOrigin, RayDirection: TVector3; |
| 404 |
const ReturnClosestIntersection: boolean; |
| 405 |
const TriangleToIgnore: PTriangle; |
| 406 |
const IgnoreMarginAtStart: boolean; |
| 407 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; overload; |
| 408 |
|
| 409 |
function RayCollision( |
| 410 |
out IntersectionDistance: Single; |
| 411 |
const RayOrigin, RayDirection: TVector3; |
| 412 |
const ReturnClosestIntersection: boolean; |
| 413 |
const TriangleToIgnore: PTriangle; |
| 414 |
const IgnoreMarginAtStart: boolean; |
| 415 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; overload; |
| 416 |
|
| 417 |
function RayCollision(const RayOrigin, RayDirection: TVector3; |
| 418 |
const ReturnClosestIntersection: boolean; |
| 419 |
const TriangleToIgnore: PTriangle; |
| 420 |
const IgnoreMarginAtStart: boolean; |
| 421 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; overload; |
| 422 |
|
| 423 |
function IsRayCollision( |
| 424 |
const RayOrigin, RayDirection: TVector3; |
| 425 |
const TriangleToIgnore: PTriangle; |
| 426 |
const IgnoreMarginAtStart: boolean; |
| 427 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 428 |
{ @groupEnd } |
| 429 |
|
| 430 |
{ Check is move allowed. This is the perfect (precise, using triangle mesh, |
| 431 |
and fast) implementation of TCastleTransform.MoveCollision interface. |
| 432 |
|
| 433 |
TriangleToIgnore and TrianglesToIgnoreFunc meaning |
| 434 |
is just like for RayCollision. This can be used to allow |
| 435 |
camera to walk thorugh some surfaces (e.g. through water |
| 436 |
surface, or to allow player to walk through some "fake wall" |
| 437 |
and discover secret room in game etc.). } |
| 438 |
function MoveCollision( |
| 439 |
const OldPos, NewPos: TVector3; |
| 440 |
const IsRadius: boolean; const Radius: Single; |
| 441 |
const OldBox, NewBox: TBox3D; |
| 442 |
const TriangleToIgnore: PTriangle = nil; |
| 443 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc = nil): boolean; overload; |
| 444 |
function MoveCollision( |
| 445 |
const OldPos, ProposedNewPos: TVector3; out NewPos: TVector3; |
| 446 |
const IsRadius: boolean; const Radius: Single; |
| 447 |
const OldBox, NewBox: TBox3D; |
| 448 |
const TriangleToIgnore: PTriangle = nil; |
| 449 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc = nil): boolean; overload; |
| 450 |
{ @groupEnd } |
| 451 |
|
| 452 |
{ For given camera position and up vector, calculate camera height |
| 453 |
above the ground. This is comfortable for cooperation with |
| 454 |
TWalkCamera.OnHeight. |
| 455 |
|
| 456 |
See TCastleTransform.Height for specification. |
| 457 |
|
| 458 |
TriangleToIgnore and TrianglesToIgnoreFunc meaning |
| 459 |
is just like for RayCollision. } |
| 460 |
function HeightCollision( |
| 461 |
const Position, GravityUp: TVector3; |
| 462 |
out AboveHeight: Single; out AboveGround: PTriangle; |
| 463 |
const TriangleToIgnore: PTriangle; |
| 464 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 465 |
|
| 466 |
{ Enumerate every triangle of this octree. |
| 467 |
|
| 468 |
It passes to EnumerateTriangleFunc callback a Triangle. |
| 469 |
Triangle is passed as a pointer (never @nil) --- these are guaranteed |
| 470 |
to be "stable" pointers stored inside octrees' lists (so they will be valid |
| 471 |
as long as octree (and eventual children octrees for TShapeOctree)). |
| 472 |
|
| 473 |
Every triangle is guaranteed to have it's World coordinates updated |
| 474 |
(to put it simply, when this is used on TShapeOctree, then we |
| 475 |
call UpdateWorld on each triangle). } |
| 476 |
procedure EnumerateTriangles(EnumerateTriangleFunc: TEnumerateTriangleFunc); |
| 477 |
virtual; abstract; |
| 478 |
|
| 479 |
{ Number of triangles within the octree. This counts all triangles |
| 480 |
returned by EnumerateTriangles. } |
| 481 |
function TrianglesCount: Cardinal; virtual; abstract; |
| 482 |
end; |
| 483 |
|
| 484 |
implementation |
| 485 |
|
| 486 |
uses Math, |
| 487 |
CastleStringUtils; |
| 488 |
|
| 489 |
{ TBaseTrianglesOctreeNode ----------------------------------------------- |
| 490 |
|
| 491 |
Common* (non-leaf nodes) implementations } |
| 492 |
|
| 493 |
function TBaseTrianglesOctreeNode.CommonSphere(const pos: TVector3; |
| 494 |
const Radius: Single; |
| 495 |
const TriangleToIgnore: PTriangle; |
| 496 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 497 |
var |
| 498 |
BoxLo, BoxHi: TOctreeSubnodeIndex; |
| 499 |
SubnodesBox: TBox3D; |
| 500 |
B0, B1, B2: boolean; |
| 501 |
begin |
| 502 |
if not IsLeaf then |
| 503 |
begin |
| 504 |
Result := nil; |
| 505 |
|
| 506 |
{ Visit every subnode containing this sphere, and look for collision there. |
| 507 |
TODO: we take box below, as simply bounding box of the sphere, |
| 508 |
so potentially we visit more nodes than necessary. } |
| 509 |
SubnodesBox.Data[0] := pos - Vector3(Radius, Radius, Radius); |
| 510 |
SubnodesBox.Data[1] := pos + Vector3(Radius, Radius, Radius); |
| 511 |
|
| 512 |
SubnodesWithBox(SubnodesBox, BoxLo, BoxHi); |
| 513 |
|
| 514 |
for B0 := BoxLo[0] to BoxHi[0] do |
| 515 |
for B1 := BoxLo[1] to BoxHi[1] do |
| 516 |
for B2 := BoxLo[2] to BoxHi[2] do |
| 517 |
begin |
| 518 |
Result := TBaseTrianglesOctreeNode(TreeSubNodes[B0, B1, B2]). |
| 519 |
CommonSphere(Pos, Radius, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 520 |
if Result <> nil then Exit; |
| 521 |
end; |
| 522 |
end else |
| 523 |
begin |
| 524 |
Result := CommonSphereLeaf(Pos, Radius, TriangleToIgnore, |
| 525 |
TrianglesToIgnoreFunc); |
| 526 |
end; |
| 527 |
end; |
| 528 |
|
| 529 |
function TBaseTrianglesOctreeNode.CommonSphere2D(const pos: TVector2; |
| 530 |
const Radius: Single; |
| 531 |
const TriangleToIgnore: PTriangle; |
| 532 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 533 |
var |
| 534 |
BoxLo, BoxHi: TOctreeSubnodeIndex; |
| 535 |
SubnodesBoxMin, SubnodesBoxMax: TVector2; |
| 536 |
B0, B1, B2: boolean; |
| 537 |
begin |
| 538 |
if not IsLeaf then |
| 539 |
begin |
| 540 |
Result := nil; |
| 541 |
|
| 542 |
{ Visit every subnode containing this sphere, and look for collision there. |
| 543 |
TODO: we take box below, as simply bounding box of the sphere, |
| 544 |
so potentially we visit more nodes than necessary. } |
| 545 |
SubnodesBoxMin := pos - Vector2(Radius, Radius); |
| 546 |
SubnodesBoxMax := pos + Vector2(Radius, Radius); |
| 547 |
|
| 548 |
SubnodesWithBox2D(SubnodesBoxMin, SubnodesBoxMax, BoxLo, BoxHi); |
| 549 |
|
| 550 |
for B0 := BoxLo[0] to BoxHi[0] do |
| 551 |
for B1 := BoxLo[1] to BoxHi[1] do |
| 552 |
for B2 := BoxLo[2] to BoxHi[2] do |
| 553 |
begin |
| 554 |
Result := TBaseTrianglesOctreeNode(TreeSubNodes[B0, B1, B2]). |
| 555 |
CommonSphere2D(Pos, Radius, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 556 |
if Result <> nil then Exit; |
| 557 |
end; |
| 558 |
end else |
| 559 |
begin |
| 560 |
Result := CommonSphere2DLeaf(Pos, Radius, TriangleToIgnore, |
| 561 |
TrianglesToIgnoreFunc); |
| 562 |
end; |
| 563 |
end; |
| 564 |
|
| 565 |
function TBaseTrianglesOctreeNode.CommonPoint2D(const Point: TVector2; |
| 566 |
const TriangleToIgnore: PTriangle; |
| 567 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 568 |
var |
| 569 |
BoxLo, BoxHi: TOctreeSubnodeIndex; |
| 570 |
B0, B1, B2: boolean; |
| 571 |
begin |
| 572 |
if not IsLeaf then |
| 573 |
begin |
| 574 |
Result := nil; |
| 575 |
|
| 576 |
SubnodesWithBox2D(Point, Point, BoxLo, BoxHi); |
| 577 |
|
| 578 |
for B0 := BoxLo[0] to BoxHi[0] do |
| 579 |
for B1 := BoxLo[1] to BoxHi[1] do |
| 580 |
for B2 := BoxLo[2] to BoxHi[2] do |
| 581 |
begin |
| 582 |
Result := TBaseTrianglesOctreeNode(TreeSubNodes[B0, B1, B2]). |
| 583 |
CommonPoint2D(Point, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 584 |
if Result <> nil then Exit; |
| 585 |
end; |
| 586 |
end else |
| 587 |
begin |
| 588 |
Result := CommonPoint2DLeaf(Point, TriangleToIgnore, |
| 589 |
TrianglesToIgnoreFunc); |
| 590 |
end; |
| 591 |
end; |
| 592 |
|
| 593 |
function TBaseTrianglesOctreeNode.CommonBox(const ABox: TBox3D; |
| 594 |
const TriangleToIgnore: PTriangle; |
| 595 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 596 |
var |
| 597 |
BoxLo, BoxHi: TOctreeSubnodeIndex; |
| 598 |
B0, B1, B2: boolean; |
| 599 |
begin |
| 600 |
if not IsLeaf then |
| 601 |
begin |
| 602 |
Result := nil; |
| 603 |
|
| 604 |
{ Visit every subnode containing this box, and look for collision there. } |
| 605 |
SubnodesWithBox(ABox, BoxLo, BoxHi); |
| 606 |
|
| 607 |
for B0 := BoxLo[0] to BoxHi[0] do |
| 608 |
for B1 := BoxLo[1] to BoxHi[1] do |
| 609 |
for B2 := BoxLo[2] to BoxHi[2] do |
| 610 |
begin |
| 611 |
Result := TBaseTrianglesOctreeNode(TreeSubNodes[B0, B1, B2]). |
| 612 |
BoxCollision(ABox, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 613 |
if Result <> nil then Exit; |
| 614 |
end; |
| 615 |
end else |
| 616 |
begin |
| 617 |
Result := CommonBoxLeaf(ABox, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 618 |
end; |
| 619 |
end; |
| 620 |
|
| 621 |
function TBaseTrianglesOctreeNode.CommonSegment( |
| 622 |
out Intersection: TVector3; |
| 623 |
out IntersectionDistance: Single; |
| 624 |
const Pos1, Pos2: TVector3; |
| 625 |
const Tag: TMailboxTag; |
| 626 |
const ReturnClosestIntersection: boolean; |
| 627 |
const TriangleToIgnore: PTriangle; |
| 628 |
const IgnoreMarginAtStart: boolean; |
| 629 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 630 |
{$define SEGMENT_COLLISION} |
| 631 |
{$I castleinternalbasetriangleoctree_raysegment_nonleaf.inc} |
| 632 |
{$undef SEGMENT_COLLISION} |
| 633 |
|
| 634 |
function TBaseTrianglesOctreeNode.CommonRay( |
| 635 |
out Intersection: TVector3; |
| 636 |
out IntersectionDistance: Single; |
| 637 |
const RayOrigin, RayDirection: TVector3; |
| 638 |
const Tag: TMailboxTag; |
| 639 |
const ReturnClosestIntersection: boolean; |
| 640 |
const TriangleToIgnore: PTriangle; |
| 641 |
const IgnoreMarginAtStart: boolean; |
| 642 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 643 |
{$I castleinternalbasetriangleoctree_raysegment_nonleaf.inc} |
| 644 |
|
| 645 |
{ TBaseTrianglesOctree --------------------------------------------------- } |
| 646 |
|
| 647 |
function TBaseTrianglesOctree.SegmentCollision( |
| 648 |
out Intersection: TVector3; |
| 649 |
out IntersectionDistance: Single; |
| 650 |
const pos1, pos2: TVector3; |
| 651 |
const ReturnClosestIntersection: boolean; |
| 652 |
const TriangleToIgnore: PTriangle; |
| 653 |
const IgnoreMarginAtStart: boolean; |
| 654 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 655 |
begin |
| 656 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).SegmentCollision( |
| 657 |
Intersection, IntersectionDistance, |
| 658 |
Pos1, Pos2, |
| 659 |
AssignNewTag, |
| 660 |
ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart, |
| 661 |
TrianglesToIgnoreFunc); |
| 662 |
end; |
| 663 |
|
| 664 |
function TBaseTrianglesOctree.SegmentCollision( |
| 665 |
out Intersection: TVector3; |
| 666 |
const pos1, pos2: TVector3; |
| 667 |
const ReturnClosestIntersection: boolean; |
| 668 |
const TriangleToIgnore: PTriangle; |
| 669 |
const IgnoreMarginAtStart: boolean; |
| 670 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc |
| 671 |
): PTriangle; |
| 672 |
var |
| 673 |
IntersectionDistance: Single; |
| 674 |
begin |
| 675 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).SegmentCollision( |
| 676 |
Intersection, IntersectionDistance, |
| 677 |
Pos1, Pos2, |
| 678 |
AssignNewTag, |
| 679 |
ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart, |
| 680 |
TrianglesToIgnoreFunc); |
| 681 |
end; |
| 682 |
|
| 683 |
function TBaseTrianglesOctree.SegmentCollision( |
| 684 |
out IntersectionDistance: Single; |
| 685 |
const pos1, pos2: TVector3; |
| 686 |
const ReturnClosestIntersection: boolean; |
| 687 |
const TriangleToIgnore: PTriangle; |
| 688 |
const IgnoreMarginAtStart: boolean; |
| 689 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 690 |
var |
| 691 |
Intersection: TVector3; |
| 692 |
begin |
| 693 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).SegmentCollision( |
| 694 |
Intersection, IntersectionDistance, |
| 695 |
Pos1, Pos2, |
| 696 |
AssignNewTag, |
| 697 |
ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart, |
| 698 |
TrianglesToIgnoreFunc); |
| 699 |
end; |
| 700 |
|
| 701 |
function TBaseTrianglesOctree.SegmentCollision( |
| 702 |
const pos1, pos2: TVector3; |
| 703 |
const ReturnClosestIntersection: boolean; |
| 704 |
const TriangleToIgnore: PTriangle; |
| 705 |
const IgnoreMarginAtStart: boolean; |
| 706 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 707 |
var |
| 708 |
Intersection: TVector3; |
| 709 |
IntersectionDistance: Single; |
| 710 |
begin |
| 711 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).SegmentCollision( |
| 712 |
Intersection, IntersectionDistance, |
| 713 |
Pos1, Pos2, |
| 714 |
AssignNewTag, |
| 715 |
ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart, |
| 716 |
TrianglesToIgnoreFunc); |
| 717 |
end; |
| 718 |
|
| 719 |
function TBaseTrianglesOctree.IsSegmentCollision( |
| 720 |
const pos1, pos2: TVector3; |
| 721 |
const TriangleToIgnore: PTriangle; |
| 722 |
const IgnoreMarginAtStart: boolean; |
| 723 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 724 |
begin |
| 725 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).IsSegmentCollision( |
| 726 |
Pos1, Pos2, |
| 727 |
AssignNewTag, |
| 728 |
TriangleToIgnore, IgnoreMarginAtStart, |
| 729 |
TrianglesToIgnoreFunc); |
| 730 |
end; |
| 731 |
|
| 732 |
function TBaseTrianglesOctree.SphereCollision(const pos: TVector3; |
| 733 |
const Radius: Single; |
| 734 |
const TriangleToIgnore: PTriangle; |
| 735 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 736 |
begin |
| 737 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).SphereCollision( |
| 738 |
Pos, Radius, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 739 |
end; |
| 740 |
|
| 741 |
function TBaseTrianglesOctree.IsSphereCollision(const pos: TVector3; |
| 742 |
const Radius: Single; |
| 743 |
const TriangleToIgnore: PTriangle; |
| 744 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 745 |
begin |
| 746 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).IsSphereCollision( |
| 747 |
Pos, Radius, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 748 |
end; |
| 749 |
|
| 750 |
function TBaseTrianglesOctree.SphereCollision2D(const pos: TVector2; |
| 751 |
const Radius: Single; |
| 752 |
const TriangleToIgnore: PTriangle; |
| 753 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 754 |
begin |
| 755 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).SphereCollision2D( |
| 756 |
Pos, Radius, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 757 |
end; |
| 758 |
|
| 759 |
function TBaseTrianglesOctree.IsSphereCollision2D(const pos: TVector2; |
| 760 |
const Radius: Single; |
| 761 |
const TriangleToIgnore: PTriangle; |
| 762 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 763 |
begin |
| 764 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).IsSphereCollision2D( |
| 765 |
Pos, Radius, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 766 |
end; |
| 767 |
|
| 768 |
function TBaseTrianglesOctree.PointCollision2D(const Point: TVector2; |
| 769 |
const TriangleToIgnore: PTriangle; |
| 770 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 771 |
begin |
| 772 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).PointCollision2D( |
| 773 |
Point, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 774 |
end; |
| 775 |
|
| 776 |
function TBaseTrianglesOctree.IsPointCollision2D(const Point: TVector2; |
| 777 |
const TriangleToIgnore: PTriangle; |
| 778 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 779 |
begin |
| 780 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).IsPointCollision2D( |
| 781 |
Point, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 782 |
end; |
| 783 |
|
| 784 |
function TBaseTrianglesOctree.BoxCollision(const ABox: TBox3D; |
| 785 |
const TriangleToIgnore: PTriangle; |
| 786 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 787 |
begin |
| 788 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).BoxCollision( |
| 789 |
ABox, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 790 |
end; |
| 791 |
|
| 792 |
function TBaseTrianglesOctree.IsBoxCollision(const ABox: TBox3D; |
| 793 |
const TriangleToIgnore: PTriangle; |
| 794 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 795 |
begin |
| 796 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).IsBoxCollision( |
| 797 |
ABox, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 798 |
end; |
| 799 |
|
| 800 |
function TBaseTrianglesOctree.RayCollision( |
| 801 |
out Intersection: TVector3; |
| 802 |
out IntersectionDistance: Single; |
| 803 |
const RayOrigin, RayDirection: TVector3; |
| 804 |
const ReturnClosestIntersection: boolean; |
| 805 |
const TriangleToIgnore: PTriangle; |
| 806 |
const IgnoreMarginAtStart: boolean; |
| 807 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 808 |
begin |
| 809 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).RayCollision( |
| 810 |
Intersection, IntersectionDistance, |
| 811 |
RayOrigin, RayDirection, |
| 812 |
AssignNewTag, |
| 813 |
ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart, |
| 814 |
TrianglesToIgnoreFunc); |
| 815 |
end; |
| 816 |
|
| 817 |
function TBaseTrianglesOctree.RayCollision( |
| 818 |
out Intersection: TVector3; |
| 819 |
const RayOrigin, RayDirection: TVector3; |
| 820 |
const ReturnClosestIntersection: boolean; |
| 821 |
const TriangleToIgnore: PTriangle; |
| 822 |
const IgnoreMarginAtStart: boolean; |
| 823 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 824 |
var |
| 825 |
IntersectionDistance: Single; |
| 826 |
begin |
| 827 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).RayCollision( |
| 828 |
Intersection, IntersectionDistance, |
| 829 |
RayOrigin, RayDirection, |
| 830 |
AssignNewTag, |
| 831 |
ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart, |
| 832 |
TrianglesToIgnoreFunc); |
| 833 |
end; |
| 834 |
|
| 835 |
|
| 836 |
function TBaseTrianglesOctree.RayCollision( |
| 837 |
out IntersectionDistance: Single; |
| 838 |
const RayOrigin, RayDirection: TVector3; |
| 839 |
const ReturnClosestIntersection: boolean; |
| 840 |
const TriangleToIgnore: PTriangle; |
| 841 |
const IgnoreMarginAtStart: boolean; |
| 842 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 843 |
var |
| 844 |
Intersection: TVector3; |
| 845 |
begin |
| 846 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).RayCollision( |
| 847 |
Intersection, IntersectionDistance, |
| 848 |
RayOrigin, RayDirection, |
| 849 |
AssignNewTag, |
| 850 |
ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart, |
| 851 |
TrianglesToIgnoreFunc); |
| 852 |
end; |
| 853 |
|
| 854 |
|
| 855 |
function TBaseTrianglesOctree.RayCollision( |
| 856 |
const RayOrigin, RayDirection: TVector3; |
| 857 |
const ReturnClosestIntersection: boolean; |
| 858 |
const TriangleToIgnore: PTriangle; |
| 859 |
const IgnoreMarginAtStart: boolean; |
| 860 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; |
| 861 |
var |
| 862 |
Intersection: TVector3; |
| 863 |
IntersectionDistance: Single; |
| 864 |
begin |
| 865 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).RayCollision( |
| 866 |
Intersection, IntersectionDistance, |
| 867 |
RayOrigin, RayDirection, |
| 868 |
AssignNewTag, |
| 869 |
ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart, |
| 870 |
TrianglesToIgnoreFunc); |
| 871 |
end; |
| 872 |
|
| 873 |
|
| 874 |
function TBaseTrianglesOctree.IsRayCollision( |
| 875 |
const RayOrigin, RayDirection: TVector3; |
| 876 |
const TriangleToIgnore: PTriangle; |
| 877 |
const IgnoreMarginAtStart: boolean; |
| 878 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 879 |
begin |
| 880 |
Result := TBaseTrianglesOctreeNode(InternalTreeRoot).IsRayCollision( |
| 881 |
RayOrigin, RayDirection, |
| 882 |
AssignNewTag, |
| 883 |
TriangleToIgnore, IgnoreMarginAtStart, |
| 884 |
TrianglesToIgnoreFunc); |
| 885 |
end; |
| 886 |
|
| 887 |
{ XxxCollision methods ------------------------------------------------------- } |
| 888 |
|
| 889 |
function TBaseTrianglesOctree.MoveCollision( |
| 890 |
const OldPos, NewPos: TVector3; |
| 891 |
const IsRadius: boolean; const Radius: Single; |
| 892 |
const OldBox, NewBox: TBox3D; |
| 893 |
const TriangleToIgnore: PTriangle; |
| 894 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 895 |
begin |
| 896 |
if IsRadius then |
| 897 |
Result := |
| 898 |
(not IsSegmentCollision(OldPos, NewPos, |
| 899 |
TriangleToIgnore, false, TrianglesToIgnoreFunc)) and |
| 900 |
(not IsSphereCollision(NewPos, Radius, |
| 901 |
TriangleToIgnore, TrianglesToIgnoreFunc)) else |
| 902 |
Result := |
| 903 |
(not IsSegmentCollision(OldPos, NewPos, |
| 904 |
TriangleToIgnore, false, TrianglesToIgnoreFunc)) and |
| 905 |
(not IsBoxCollision(NewBox, |
| 906 |
TriangleToIgnore, TrianglesToIgnoreFunc)); |
| 907 |
end; |
| 908 |
|
| 909 |
function TBaseTrianglesOctree.MoveCollision( |
| 910 |
const OldPos, ProposedNewPos: TVector3; out NewPos: TVector3; |
| 911 |
const IsRadius: boolean; const Radius: Single; |
| 912 |
const OldBox, NewBox: TBox3D; |
| 913 |
const TriangleToIgnore: PTriangle; |
| 914 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 915 |
|
| 916 |
{ $define DEBUG_WALL_SLIDING} |
| 917 |
|
| 918 |
const |
| 919 |
{ For wall-sliding inside MoveAlongTheBlocker implementations, |
| 920 |
we want to position ourselves slightly farther away than |
| 921 |
Radius. (Exactly on Radius would mean that it's |
| 922 |
sensitive to floating point imprecision, and sometimes the sphere |
| 923 |
could be considered colliding with Blocker anyway, instead |
| 924 |
of sliding along it. And final MoveCollision (without wall-sliding) call |
| 925 |
will then fail, making wall-sliding non-working.) |
| 926 |
|
| 927 |
So this must be something slightly larger than 1. |
| 928 |
And obviously must be close to 1 |
| 929 |
(otherwise NewPos will not be sensible). } |
| 930 |
RadiusEnlarge = 1.01; |
| 931 |
|
| 932 |
{ This is the worse version of wall-sliding: |
| 933 |
we don't know the 3D point of intersection with blocker, |
| 934 |
which means we can't really calculate a vector to make |
| 935 |
proper wall-sliding. We do some tricks to still perform wall-sliding |
| 936 |
in many positions, but it's not perfect. } |
| 937 |
function MoveAlongTheBlocker(Blocker: PTriangle): boolean; overload; |
| 938 |
var |
| 939 |
{$ifdef CONSERVE_TRIANGLE_MEMORY_MORE} |
| 940 |
Plane: TVector4; |
| 941 |
{$endif} |
| 942 |
PlanePtr: PVector4; |
| 943 |
PlaneNormalPtr: PVector3; |
| 944 |
NewPosShift: TVector3; |
| 945 |
begin |
| 946 |
{$ifdef CONSERVE_TRIANGLE_MEMORY_MORE} |
| 947 |
Plane := Blocker^.World.Plane; |
| 948 |
PlanePtr := @Plane; |
| 949 |
{$else} |
| 950 |
PlanePtr := @(Blocker^.World.Plane); |
| 951 |
{$endif} |
| 952 |
PlaneNormalPtr := PVector3(PlanePtr); |
| 953 |
|
| 954 |
{ project ProposedNewPos on a plane of blocking object } |
| 955 |
NewPos := PointOnPlaneClosestToPoint(PlanePtr^, ProposedNewPos); |
| 956 |
|
| 957 |
{ now NewPos must be on the same plane side as OldPos is, |
| 958 |
and it must be at the distance slightly larger than Radius from the plane } |
| 959 |
if VectorsSamePlaneDirections(PlaneNormalPtr^, |
| 960 |
ProposedNewPos - NewPos, PlanePtr^) then |
| 961 |
NewPosShift := PlaneNormalPtr^ * ( Radius * RadiusEnlarge) |
| 962 |
else |
| 963 |
NewPosShift := PlaneNormalPtr^ * (-Radius * RadiusEnlarge); |
| 964 |
NewPos := NewPos + NewPosShift; |
| 965 |
|
| 966 |
{ Even though I calculated NewPos so that it's not blocked by object |
| 967 |
Blocker, I must check whether it's not blocked by something else |
| 968 |
(e.g. if player is trying to walk into the corner (two walls)). |
| 969 |
I can do it by using my simple MoveCollision. } |
| 970 |
|
| 971 |
Result := MoveCollision(OldPos, NewPos, IsRadius, Radius, OldBox, NewBox, |
| 972 |
TriangleToIgnore, TrianglesToIgnoreFunc); |
| 973 |
|
| 974 |
{$ifdef DEBUG_WALL_SLIDING} |
| 975 |
Writeln('Wall-sliding: WORSE version without 3d intersection. Blocker ', PointerToStr(Blocker), '.'); |
| 976 |
{$endif} |
| 977 |
end; |
| 978 |
|
| 979 |
{ The better wall-sliding implementation, that can calculate |
| 980 |
nice vector along which to slide. |
| 981 |
|
| 982 |
It requires as input BlockerIntersection, this is the 3D point |
| 983 |
of intersection between player move line (from OldPos to ProposedNewPos) |
| 984 |
and the Blocker.World.Plane. |
| 985 |
|
| 986 |
SegmentCollision says whether segment OldPos->ProposedNewPos was detected |
| 987 |
as colliding with Blocker.World.Plane (IOW, ProposedNewPos is on the other |
| 988 |
side of the blocker plane) or not (IOW, ProposedNewPos is on the same |
| 989 |
side of the blocker plane). } |
| 990 |
function MoveAlongTheBlocker( |
| 991 |
const BlockerIntersection: TVector3; |
| 992 |
SegmentCollision: boolean; |
| 993 |
Blocker: PTriangle): boolean; overload; |
| 994 |
var |
| 995 |
{$ifdef CONSERVE_TRIANGLE_MEMORY_MORE} |
| 996 |
Plane: TVector4; |
| 997 |
{$endif} |
| 998 |
PlanePtr: PVector4; |
| 999 |
Slide, Projected: TVector3; |
| 1000 |
NewBlocker: PTriangle; |
| 1001 |
NewBlockerIntersection: TVector3; |
| 1002 |
begin |
| 1003 |
{$ifdef CONSERVE_TRIANGLE_MEMORY_MORE} |
| 1004 |
Plane := Blocker^.World.Plane; |
| 1005 |
PlanePtr := @Plane; |
| 1006 |
{$else} |
| 1007 |
PlanePtr := @(Blocker^.World.Plane); |
| 1008 |
{$endif} |
| 1009 |
|
| 1010 |
{$ifdef DEBUG_WALL_SLIDING} |
| 1011 |
Write('Wall-sliding: Better version (with 3d intersection). '); |
| 1012 |
if SegmentCollision then |
| 1013 |
Write('Segment collided. ') else |
| 1014 |
Write('Sphere collided . '); |
| 1015 |
Writeln('Blocker ', PointerToStr(Blocker), '.'); |
| 1016 |
{$endif} |
| 1017 |
|
| 1018 |
{ Project ProposedNewPos or OldPos on Blocker plane. |
| 1019 |
The idea is that knowing this projection, and knowing BlockerIntersection, |
| 1020 |
we can calculate Slide (= vector that will move us parallel to |
| 1021 |
Blocker plane). |
| 1022 |
|
| 1023 |
We could always project ProposedNewPos. But for |
| 1024 |
SegmentCollision = @false, OldPos is also good to use, |
| 1025 |
and it's farther from BlockerIntersection than ProposedNewPos |
| 1026 |
--- this is good, as we want Slide vector to be long, to avoid |
| 1027 |
floating point imprecision when Slide is very very short vector. } |
| 1028 |
if SegmentCollision then |
| 1029 |
begin |
| 1030 |
Projected := PointOnPlaneClosestToPoint(PlanePtr^, ProposedNewPos); |
| 1031 |
Slide := Projected - BlockerIntersection; |
| 1032 |
end else |
| 1033 |
begin |
| 1034 |
Projected := PointOnPlaneClosestToPoint(PlanePtr^, OldPos); |
| 1035 |
Slide := BlockerIntersection - Projected; |
| 1036 |
end; |
| 1037 |
|
| 1038 |
if not Slide.IsZero then |
| 1039 |
begin |
| 1040 |
{ Move by Slide. |
| 1041 |
|
| 1042 |
Length of Slide is taken from the distance between |
| 1043 |
OldPos and ProposedNewPos. This is Ok, as we do not try to |
| 1044 |
make perfect wall-sliding (that would first move as close to Blocker |
| 1045 |
plane as possible, and then move along the blocker). |
| 1046 |
Instead we move all the way along the blocker. This is in practice Ok. } |
| 1047 |
|
| 1048 |
Slide := Slide.AdjustToLength(PointsDistance(OldPos, ProposedNewPos)); |
| 1049 |
|
| 1050 |
NewPos := OldPos + Slide; |
| 1051 |
|
| 1052 |
{ Even though I calculated NewPos so that it's not blocked by object |
| 1053 |
Blocker, I must check whether it's not blocked by something else |
| 1054 |
(e.g. if player is trying to walk into the corner (two walls)). |
| 1055 |
I can do it by using my simple MoveCollision. } |
| 1056 |
|
| 1057 |
Result := MoveCollision(OldPos, NewPos, |
| 1058 |
IsRadius, Radius, OldBox, NewBox, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 1059 |
|
| 1060 |
{$ifdef DEBUG_WALL_SLIDING} Writeln('Wall-sliding: Final check of sliding result: ', Result); {$endif} |
| 1061 |
|
| 1062 |
if (not Result) and (not SegmentCollision) then |
| 1063 |
begin |
| 1064 |
{ When going through corners, previous code will not necessarily make |
| 1065 |
good wall-sliding, because our Blocker may be taken from sphere |
| 1066 |
collision. So it's not really a good plane to slide along. |
| 1067 |
Let's try harder to to get a better blocker: use RayCollision |
| 1068 |
in the previous Slide direction, |
| 1069 |
and check is result still within our sphere. |
| 1070 |
|
| 1071 |
We preserve below the old value of Blocker (have our own NewBlocker |
| 1072 |
and NewBlockerIntersection), but the rest of variables may be |
| 1073 |
mercilessly overriden by code below: |
| 1074 |
PlanePtr, Projected, Slide helpers. |
| 1075 |
|
| 1076 |
Check that it works: e.g. test beginning of castle_hall_final.wrl, |
| 1077 |
new_acts.wrl. } |
| 1078 |
|
| 1079 |
NewBlocker := RayCollision( |
| 1080 |
OldPos, Slide, true { return closest blocker }, |
| 1081 |
TriangleToIgnore, false, TrianglesToIgnoreFunc); |
| 1082 |
|
| 1083 |
if (NewBlocker <> nil) and |
| 1084 |
(NewBlocker <> Blocker) and |
| 1085 |
IsTriangleSphereCollision( |
| 1086 |
NewBlocker^.World.Triangle, |
| 1087 |
NewBlocker^.World.Plane, |
| 1088 |
ProposedNewPos, |
| 1089 |
{ NewBlocker is accepted more generously, within 2 * normal radius. } |
| 1090 |
Radius * 2) and |
| 1091 |
TryPlaneLineIntersection(NewBlockerIntersection, |
| 1092 |
NewBlocker^.World.Plane, |
| 1093 |
OldPos, ProposedNewPos - OldPos) then |
| 1094 |
begin |
| 1095 |
{$ifdef DEBUG_WALL_SLIDING} Writeln('Wall-sliding: Better blocker found: ', PointerToStr(NewBlocker), '.'); {$endif} |
| 1096 |
|
| 1097 |
{ Below we essentially make the wall-sliding computation again. |
| 1098 |
We know that we're in sphere collision case |
| 1099 |
(checked above that "not SegmentCollision"). } |
| 1100 |
|
| 1101 |
{$ifdef CONSERVE_TRIANGLE_MEMORY_MORE} |
| 1102 |
Plane := NewBlocker^.World.Plane; |
| 1103 |
PlanePtr := @Plane; |
| 1104 |
{$else} |
| 1105 |
PlanePtr := @(NewBlocker^.World.Plane); |
| 1106 |
{$endif} |
| 1107 |
Projected := PointOnPlaneClosestToPoint(PlanePtr^, OldPos); |
| 1108 |
Slide := NewBlockerIntersection - Projected; |
| 1109 |
|
| 1110 |
if not Slide.IsZero then |
| 1111 |
begin |
| 1112 |
Slide := Slide.AdjustToLength(PointsDistance(OldPos, ProposedNewPos)); |
| 1113 |
NewPos := OldPos + Slide; |
| 1114 |
Result := MoveCollision(OldPos, NewPos, |
| 1115 |
IsRadius, Radius, OldBox, NewBox, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 1116 |
|
| 1117 |
{$ifdef DEBUG_WALL_SLIDING} Writeln('Wall-sliding: Better blocker final check of sliding result: ', Result); {$endif} |
| 1118 |
end; |
| 1119 |
end else |
| 1120 |
if NewBlocker <> nil then |
| 1121 |
begin |
| 1122 |
{$ifdef DEBUG_WALL_SLIDING} |
| 1123 |
Writeln('Wall-sliding: Better blocker NOT found: ', PointerToStr(NewBlocker), ' ', |
| 1124 |
IsTriangleSphereCollision( |
| 1125 |
NewBlocker^.World.Triangle, |
| 1126 |
NewBlocker^.World.Plane, |
| 1127 |
ProposedNewPos, Radius), ' ', |
| 1128 |
TryPlaneLineIntersection(NewBlockerIntersection, |
| 1129 |
NewBlocker^.World.Plane, |
| 1130 |
OldPos, ProposedNewPos - OldPos), '.'); |
| 1131 |
{$endif} |
| 1132 |
end; |
| 1133 |
end; |
| 1134 |
end else |
| 1135 |
begin |
| 1136 |
{ Fallback to worse wall-sliding version. } |
| 1137 |
{$ifdef DEBUG_WALL_SLIDING} Writeln('Wall-sliding: Need to fallback to worse version (Slide = 0)'); {$endif} |
| 1138 |
Result := MoveAlongTheBlocker(Blocker); |
| 1139 |
end; |
| 1140 |
end; |
| 1141 |
|
| 1142 |
var |
| 1143 |
Blocker: PTriangle; |
| 1144 |
BlockerIntersection: TVector3; |
| 1145 |
begin |
| 1146 |
if not IsRadius then |
| 1147 |
begin |
| 1148 |
{ for IsRadius = false, for now just fallback to simple yes-no check, |
| 1149 |
without wall-sliding. We can improve this one day to make wall-sliding |
| 1150 |
even in this case (NewBox in this case will be shifted |
| 1151 |
like ProposedNewPos->NewPos). } |
| 1152 |
Result := MoveCollision(OldPos, ProposedNewPos, |
| 1153 |
IsRadius, Radius, OldBox, NewBox, TriangleToIgnore, TrianglesToIgnoreFunc); |
| 1154 |
NewPos := ProposedNewPos; |
| 1155 |
Exit; |
| 1156 |
end; |
| 1157 |
|
| 1158 |
Blocker := SegmentCollision( |
| 1159 |
BlockerIntersection, OldPos, ProposedNewPos, |
| 1160 |
true { return closest blocker }, |
| 1161 |
TriangleToIgnore, false, TrianglesToIgnoreFunc); |
| 1162 |
if Blocker = nil then |
| 1163 |
begin |
| 1164 |
Blocker := SphereCollision(ProposedNewPos, Radius, |
| 1165 |
TriangleToIgnore, TrianglesToIgnoreFunc); |
| 1166 |
if Blocker = nil then |
| 1167 |
begin |
| 1168 |
Result := true; |
| 1169 |
NewPos := ProposedNewPos; |
| 1170 |
end else |
| 1171 |
if TryPlaneLineIntersection(BlockerIntersection, Blocker^.World.Plane, |
| 1172 |
OldPos, ProposedNewPos - OldPos) then |
| 1173 |
Result := MoveAlongTheBlocker(BlockerIntersection, false, Blocker) else |
| 1174 |
Result := MoveAlongTheBlocker(Blocker); |
| 1175 |
end else |
| 1176 |
Result := MoveAlongTheBlocker(BlockerIntersection, true, Blocker); |
| 1177 |
end; |
| 1178 |
|
| 1179 |
function TBaseTrianglesOctree.HeightCollision( |
| 1180 |
const Position, GravityUp: TVector3; |
| 1181 |
out AboveHeight: Single; out AboveGround: PTriangle; |
| 1182 |
const TriangleToIgnore: PTriangle; |
| 1183 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 1184 |
begin |
| 1185 |
AboveGround := RayCollision(AboveHeight, Position, -GravityUp, |
| 1186 |
true, TriangleToIgnore, false, TrianglesToIgnoreFunc); |
| 1187 |
Result := AboveGround <> nil; |
| 1188 |
if not Result then |
| 1189 |
AboveHeight := MaxSingle; |
| 1190 |
end; |
| 1191 |
|
| 1192 |
{ Other TBaseTrianglesOctree utils ----------------------------------------------- } |
| 1193 |
|
| 1194 |
var |
| 1195 |
{ Next tag that will be allocated for ray/segment and such by AssignNewTag. |
| 1196 |
Can be read/written only by AssignNewTag. |
| 1197 |
|
| 1198 |
That's right, this is a global variable. Reason: octree instances |
| 1199 |
are sometimes freed / recreated (consider e.g. TShapeOctree |
| 1200 |
recreated when Trasform changes). New octree may want to immediately |
| 1201 |
do some collision checks. However, records about tags from old octree |
| 1202 |
may still be remembered somewhere -- for example, TShape |
| 1203 |
remembers MailboxSavedTag (given for Shape.RayCollision from |
| 1204 |
TShapeOctreeNode.CommonRayLeaf). So, if this would be a field |
| 1205 |
of TBaseTrianglesOctree, then the newly created octree could |
| 1206 |
create the same tag, and hit the mailbox mechanism of already existing |
| 1207 |
shape. |
| 1208 |
|
| 1209 |
This actually happened with SphereSensor tests, |
| 1210 |
e.g. on unison.x3dv from |
| 1211 |
http://www.web3d.org/x3d/content/examples/Conformance/Sensors/SphereSensor/index.html . |
| 1212 |
When you rotate the box, it's Transform.rotation changed, causing |
| 1213 |
a rebuild of shapes octree. (But actual TShape and it's local triangles |
| 1214 |
octree stay unmodified.) Without moving NextFreeTag to global variable, |
| 1215 |
the new created octree would have NextFreeTag that was already recorded. |
| 1216 |
In effect, @link(Height) (for camera gravity) were returning a result |
| 1217 |
for previous mouse ray pick, temporary showing our "height above the ground" |
| 1218 |
even though we were not standing on the ground. } |
| 1219 |
NextFreeTag: TMailboxTag; |
| 1220 |
|
| 1221 |
function TBaseTrianglesOctree.AssignNewTag: TMailboxTag; |
| 1222 |
begin |
| 1223 |
result := NextFreeTag; |
| 1224 |
Inc(NextFreeTag); |
| 1225 |
end; |
| 1226 |
|
| 1227 |
end. |