| 1 |
{%MainUnit castletransform.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Implementation of all XxxCollision methods in CastleTransform unit. } |
| 18 |
|
| 19 |
{ TCastleTransform -------------------------------------------------------------------- } |
| 20 |
|
| 21 |
function TCastleTransform.LocalHeightCollision(const APosition, GravityUp: TVector3; |
| 22 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc; |
| 23 |
out AboveHeight: Single; out AboveGround: PTriangle): boolean; |
| 24 |
var |
| 25 |
I: Integer; |
| 26 |
NewResult: boolean; |
| 27 |
NewAboveHeight: Single; |
| 28 |
NewAboveGround: PTriangle; |
| 29 |
begin |
| 30 |
Result := false; |
| 31 |
AboveHeight := MaxSingle; |
| 32 |
AboveGround := nil; |
| 33 |
|
| 34 |
if CheckCollides then |
| 35 |
begin |
| 36 |
for I := 0 to List.Count - 1 do |
| 37 |
begin |
| 38 |
NewResult := List[I].HeightCollision(APosition, GravityUp, TrianglesToIgnoreFunc, |
| 39 |
NewAboveHeight, NewAboveGround); |
| 40 |
|
| 41 |
if NewAboveHeight < AboveHeight then |
| 42 |
begin |
| 43 |
Result := NewResult; |
| 44 |
AboveHeight := NewAboveHeight; |
| 45 |
AboveGround := NewAboveGround; |
| 46 |
end; |
| 47 |
end; |
| 48 |
end; |
| 49 |
end; |
| 50 |
|
| 51 |
function TCastleTransform.LocalMoveCollision( |
| 52 |
const OldPos, ProposedNewPos: TVector3; out NewPos: TVector3; |
| 53 |
const IsRadius: boolean; const Radius: Single; |
| 54 |
const OldBox, NewBox: TBox3D; |
| 55 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 56 |
var |
| 57 |
I: Integer; |
| 58 |
begin |
| 59 |
Result := true; |
| 60 |
NewPos := ProposedNewPos; |
| 61 |
|
| 62 |
if CheckCollides then |
| 63 |
begin |
| 64 |
{ We call MoveCollision with separate ProposedNewPos and NewPos |
| 65 |
only on the first scene. |
| 66 |
This means that only first scene collisions provide wall sliding. |
| 67 |
Collisions with other 3D objects will simply block the player. |
| 68 |
|
| 69 |
Otherwise, various MoveCollision could modify NewPos |
| 70 |
making it colliding with other items, already checked. This would |
| 71 |
be wrong. |
| 72 |
|
| 73 |
TODO: this could be improved, to call MoveCollision |
| 74 |
with separate ProposedNewPos and NewPos |
| 75 |
on the first scene |
| 76 |
where the simple move is not allowed. This would make it more general, |
| 77 |
although also slower. Is there any way to make it as fast and |
| 78 |
more general? } |
| 79 |
|
| 80 |
if List.Count <> 0 then |
| 81 |
begin |
| 82 |
Result := List[0].MoveCollision(OldPos, ProposedNewPos, NewPos, |
| 83 |
IsRadius, Radius, OldBox, NewBox, TrianglesToIgnoreFunc); |
| 84 |
if not Result then Exit; |
| 85 |
|
| 86 |
for I := 1 to List.Count - 1 do |
| 87 |
begin |
| 88 |
Result := List[I].MoveCollision(OldPos, NewPos, |
| 89 |
IsRadius, Radius, OldBox, NewBox, TrianglesToIgnoreFunc); |
| 90 |
if not Result then Exit; |
| 91 |
end; |
| 92 |
end; |
| 93 |
end; |
| 94 |
end; |
| 95 |
|
| 96 |
function TCastleTransform.LocalMoveCollision( |
| 97 |
const OldPos, NewPos: TVector3; |
| 98 |
const IsRadius: boolean; const Radius: Single; |
| 99 |
const OldBox, NewBox: TBox3D; |
| 100 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 101 |
var |
| 102 |
I: Integer; |
| 103 |
begin |
| 104 |
Result := true; |
| 105 |
|
| 106 |
if CheckCollides then |
| 107 |
begin |
| 108 |
for I := 0 to List.Count - 1 do |
| 109 |
begin |
| 110 |
Result := List[I].MoveCollision(OldPos, NewPos, |
| 111 |
IsRadius, Radius, OldBox, NewBox, TrianglesToIgnoreFunc); |
| 112 |
if not Result then Exit; |
| 113 |
end; |
| 114 |
end; |
| 115 |
end; |
| 116 |
|
| 117 |
function TCastleTransform.LocalSegmentCollision(const Pos1, Pos2: TVector3; |
| 118 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc; |
| 119 |
const ALineOfSight: boolean): boolean; |
| 120 |
var |
| 121 |
I: Integer; |
| 122 |
begin |
| 123 |
Result := false; |
| 124 |
|
| 125 |
if CheckCollides or (ALineOfSight and Exists) then |
| 126 |
begin |
| 127 |
for I := 0 to List.Count - 1 do |
| 128 |
begin |
| 129 |
Result := List[I].SegmentCollision(Pos1, Pos2, TrianglesToIgnoreFunc, ALineOfSight); |
| 130 |
if Result then Exit; |
| 131 |
end; |
| 132 |
end; |
| 133 |
end; |
| 134 |
|
| 135 |
function TCastleTransform.LocalSphereCollision(const Pos: TVector3; const Radius: Single; |
| 136 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 137 |
var |
| 138 |
I: Integer; |
| 139 |
begin |
| 140 |
Result := false; |
| 141 |
|
| 142 |
if CheckCollides then |
| 143 |
begin |
| 144 |
for I := 0 to List.Count - 1 do |
| 145 |
begin |
| 146 |
Result := List[I].SphereCollision(Pos, Radius, TrianglesToIgnoreFunc); |
| 147 |
if Result then Exit; |
| 148 |
end; |
| 149 |
end; |
| 150 |
end; |
| 151 |
|
| 152 |
function TCastleTransform.LocalSphereCollision2D(const Pos: TVector2; const Radius: Single; |
| 153 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc; |
| 154 |
const Details: TCollisionDetails): boolean; |
| 155 |
var |
| 156 |
I: Integer; |
| 157 |
begin |
| 158 |
Result := false; |
| 159 |
|
| 160 |
if CheckCollides then |
| 161 |
begin |
| 162 |
for I := 0 to List.Count - 1 do |
| 163 |
begin |
| 164 |
Result := List[I].SphereCollision2D(Pos, Radius, TrianglesToIgnoreFunc, Details); |
| 165 |
if Result then |
| 166 |
begin |
| 167 |
if Details <> nil then |
| 168 |
Details.Add(Self); |
| 169 |
Exit; |
| 170 |
end; |
| 171 |
end; |
| 172 |
end; |
| 173 |
end; |
| 174 |
|
| 175 |
function TCastleTransform.LocalPointCollision2D(const Point: TVector2; |
| 176 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 177 |
var |
| 178 |
I: Integer; |
| 179 |
begin |
| 180 |
Result := false; |
| 181 |
|
| 182 |
if CheckCollides then |
| 183 |
begin |
| 184 |
for I := 0 to List.Count - 1 do |
| 185 |
begin |
| 186 |
Result := List[I].PointCollision2D(Point, TrianglesToIgnoreFunc); |
| 187 |
if Result then Exit; |
| 188 |
end; |
| 189 |
end; |
| 190 |
end; |
| 191 |
|
| 192 |
function TCastleTransform.LocalBoxCollision(const Box: TBox3D; |
| 193 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 194 |
var |
| 195 |
I: Integer; |
| 196 |
begin |
| 197 |
Result := false; |
| 198 |
|
| 199 |
if CheckCollides then |
| 200 |
begin |
| 201 |
for I := 0 to List.Count - 1 do |
| 202 |
begin |
| 203 |
Result := List[I].BoxCollision(Box, TrianglesToIgnoreFunc); |
| 204 |
if Result then Exit; |
| 205 |
end; |
| 206 |
end; |
| 207 |
end; |
| 208 |
|
| 209 |
function TCastleTransform.LocalRayCollision(const RayOrigin, RayDirection: TVector3; |
| 210 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): TRayCollision; |
| 211 |
|
| 212 |
procedure AddNewResult(NewResult: TRayCollision); |
| 213 |
begin |
| 214 |
if NewResult <> nil then |
| 215 |
begin |
| 216 |
if (Result = nil) or (NewResult.Distance < Result.Distance) then |
| 217 |
begin |
| 218 |
SysUtils.FreeAndNil(Result); |
| 219 |
Result := NewResult; |
| 220 |
end else |
| 221 |
FreeAndNil(NewResult); |
| 222 |
end; |
| 223 |
end; |
| 224 |
|
| 225 |
var |
| 226 |
I: Integer; |
| 227 |
|
| 228 |
NewNode, PreviousNode: PRayCollisionNode; |
| 229 |
begin |
| 230 |
Result := nil; |
| 231 |
|
| 232 |
if CheckPickable then |
| 233 |
begin |
| 234 |
for I := 0 to List.Count - 1 do |
| 235 |
AddNewResult(List[I].RayCollision(RayOrigin, RayDirection, TrianglesToIgnoreFunc)); |
| 236 |
|
| 237 |
if Result <> nil then |
| 238 |
begin |
| 239 |
NewNode := PRayCollisionNode(Result.Add); |
| 240 |
PreviousNode := PRayCollisionNode(Result.Ptr(Result.Count - 2)); |
| 241 |
NewNode^.Item := Self; |
| 242 |
NewNode^.Point := PreviousNode^.Point; |
| 243 |
NewNode^.Triangle := nil; |
| 244 |
NewNode^.RayOrigin := RayOrigin; |
| 245 |
NewNode^.RayDirection := RayDirection; |
| 246 |
end; |
| 247 |
end; |
| 248 |
end; |
| 249 |
|
| 250 |
{ TCastleTransform ----------------------------------------------------------- } |
| 251 |
|
| 252 |
function TCastleTransform.HeightCollision(const APosition, GravityUp: TVector3; |
| 253 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc; |
| 254 |
out AboveHeight: Single; out AboveGround: PTriangle): boolean; |
| 255 |
var |
| 256 |
MInverse: TMatrix4; |
| 257 |
begin |
| 258 |
{ LocalHeightCollision will check these anyway. But by checking them here, |
| 259 |
we can potentially avoid the cost of transforming into local space. } |
| 260 |
if not CheckCollides then |
| 261 |
begin |
| 262 |
Result := false; |
| 263 |
AboveHeight := MaxSingle; |
| 264 |
AboveGround := nil; |
| 265 |
Exit; |
| 266 |
end; |
| 267 |
|
| 268 |
if FOnlyTranslation then |
| 269 |
Result := LocalHeightCollision( |
| 270 |
APosition - Translation, GravityUp, TrianglesToIgnoreFunc, |
| 271 |
AboveHeight, AboveGround) else |
| 272 |
begin |
| 273 |
MInverse := InverseTransform; |
| 274 |
Result := LocalHeightCollision( |
| 275 |
MInverse.MultPoint(APosition), |
| 276 |
MInverse.MultDirection(GravityUp), TrianglesToIgnoreFunc, |
| 277 |
AboveHeight, AboveGround); |
| 278 |
{ Note that we should not scale resulting AboveHeight by AverageScale. |
| 279 |
That is because AboveHeight is relative to GravityUp length, |
| 280 |
so it's automatically correct. } |
| 281 |
end; |
| 282 |
end; |
| 283 |
|
| 284 |
function TCastleTransform.MoveCollision( |
| 285 |
const OldPos, ProposedNewPos: TVector3; out NewPos: TVector3; |
| 286 |
const IsRadius: boolean; const Radius: Single; |
| 287 |
const OldBox, NewBox: TBox3D; |
| 288 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 289 |
var |
| 290 |
T: TVector3; |
| 291 |
Tr: TTransformation; |
| 292 |
begin |
| 293 |
{ LocalMoveCollision will check these anyway. But by checking them here, |
| 294 |
we can potentially avoid the cost of transforming into local space. } |
| 295 |
if not CheckCollides then |
| 296 |
begin |
| 297 |
NewPos := ProposedNewPos; |
| 298 |
Exit(true); |
| 299 |
end; |
| 300 |
|
| 301 |
if FOnlyTranslation then |
| 302 |
begin |
| 303 |
T := Translation; |
| 304 |
Result := LocalMoveCollision( |
| 305 |
OldPos - T, |
| 306 |
ProposedNewPos - T, NewPos, |
| 307 |
IsRadius, Radius, |
| 308 |
OldBox.AntiTranslate(T), |
| 309 |
NewBox.AntiTranslate(T), TrianglesToIgnoreFunc); |
| 310 |
{ translate calculated NewPos back } |
| 311 |
if Result then |
| 312 |
NewPos := NewPos + T; |
| 313 |
end else |
| 314 |
begin |
| 315 |
InternalTransformation(Tr); |
| 316 |
Result := LocalMoveCollision( |
| 317 |
Tr.InverseTransform.MultPoint(OldPos), |
| 318 |
Tr.InverseTransform.MultPoint(ProposedNewPos), NewPos, |
| 319 |
IsRadius, Radius / AverageScale, |
| 320 |
OldBox.Transform(Tr.InverseTransform), |
| 321 |
NewBox.Transform(Tr.InverseTransform), TrianglesToIgnoreFunc); |
| 322 |
{ transform calculated NewPos back } |
| 323 |
if Result then |
| 324 |
NewPos := Tr.Transform.MultPoint(NewPos); |
| 325 |
end; |
| 326 |
end; |
| 327 |
|
| 328 |
function TCastleTransform.MoveCollision( |
| 329 |
const OldPos, NewPos: TVector3; |
| 330 |
const IsRadius: boolean; const Radius: Single; |
| 331 |
const OldBox, NewBox: TBox3D; |
| 332 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 333 |
var |
| 334 |
T: TVector3; |
| 335 |
MInverse: TMatrix4; |
| 336 |
begin |
| 337 |
{ LocalMoveCollision will check these anyway. But by checking them here, |
| 338 |
we can potentially avoid the cost of transforming into local space. } |
| 339 |
if not CheckCollides then Exit(true); |
| 340 |
|
| 341 |
if FOnlyTranslation then |
| 342 |
begin |
| 343 |
{ I have to check collision between |
| 344 |
Items + Translation and (OldPos, NewPos). |
| 345 |
So it's equivalent to checking for collision between |
| 346 |
Items and (OldPos, NewPos) - Translation |
| 347 |
And this way I can use LocalMoveCollision. } |
| 348 |
T := Translation; |
| 349 |
Result := LocalMoveCollision( |
| 350 |
OldPos - T, |
| 351 |
NewPos - T, |
| 352 |
IsRadius, Radius, |
| 353 |
OldBox.AntiTranslate(T), |
| 354 |
NewBox.AntiTranslate(T), TrianglesToIgnoreFunc); |
| 355 |
end else |
| 356 |
begin |
| 357 |
MInverse := InverseTransform; |
| 358 |
Result := LocalMoveCollision( |
| 359 |
MInverse.MultPoint(OldPos), |
| 360 |
MInverse.MultPoint(NewPos), |
| 361 |
IsRadius, Radius / AverageScale, |
| 362 |
OldBox.Transform(MInverse), |
| 363 |
NewBox.Transform(MInverse), TrianglesToIgnoreFunc); |
| 364 |
end; |
| 365 |
end; |
| 366 |
|
| 367 |
function TCastleTransform.SegmentCollision(const Pos1, Pos2: TVector3; |
| 368 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc; |
| 369 |
const ALineOfSight: boolean): boolean; |
| 370 |
var |
| 371 |
T: TVector3; |
| 372 |
MInverse: TMatrix4; |
| 373 |
begin |
| 374 |
{ LocalSegmentCollision will check these anyway. But by checking them here, |
| 375 |
we can potentially avoid the cost of transforming into local space. } |
| 376 |
if not (CheckCollides or (ALineOfSight and Exists)) then Exit(false); |
| 377 |
|
| 378 |
if FOnlyTranslation then |
| 379 |
begin |
| 380 |
T := Translation; |
| 381 |
Result := LocalSegmentCollision(Pos1 - T, Pos2 - T, TrianglesToIgnoreFunc, ALineOfSight); |
| 382 |
end else |
| 383 |
begin |
| 384 |
MInverse := InverseTransform; |
| 385 |
Result := LocalSegmentCollision( |
| 386 |
MInverse.MultPoint(Pos1), |
| 387 |
MInverse.MultPoint(Pos2), TrianglesToIgnoreFunc, ALineOfSight); |
| 388 |
end; |
| 389 |
end; |
| 390 |
|
| 391 |
function TCastleTransform.SphereCollision( |
| 392 |
const Pos: TVector3; const Radius: Single; |
| 393 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 394 |
begin |
| 395 |
{ LocalSphereCollision will check these anyway. But by checking them here, |
| 396 |
we can potentially avoid the cost of transforming into local space. } |
| 397 |
if not CheckCollides then Exit(false); |
| 398 |
|
| 399 |
if FOnlyTranslation then |
| 400 |
Result := LocalSphereCollision( |
| 401 |
Pos - Translation, Radius, TrianglesToIgnoreFunc) else |
| 402 |
Result := LocalSphereCollision( |
| 403 |
InverseTransform.MultPoint(Pos), Radius / AverageScale, TrianglesToIgnoreFunc); |
| 404 |
end; |
| 405 |
|
| 406 |
function TCastleTransform.SphereCollision2D( |
| 407 |
const Pos: TVector2; const Radius: Single; |
| 408 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc; |
| 409 |
const Details: TCollisionDetails): boolean; |
| 410 |
begin |
| 411 |
{ LocalSphereCollision2D will check these anyway. But by checking them here, |
| 412 |
we can potentially avoid the cost of transforming into local space. } |
| 413 |
if not CheckCollides then Exit(false); |
| 414 |
|
| 415 |
if FOnlyTranslation then |
| 416 |
Result := LocalSphereCollision2D( |
| 417 |
Pos - TranslationXY, Radius, TrianglesToIgnoreFunc, Details) else |
| 418 |
Result := LocalSphereCollision2D( |
| 419 |
InverseTransform.MultPoint(Pos), Radius / AverageScale2D, TrianglesToIgnoreFunc, Details); |
| 420 |
end; |
| 421 |
|
| 422 |
function TCastleTransform.PointCollision2D( |
| 423 |
const Point: TVector2; |
| 424 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 425 |
begin |
| 426 |
{ LocalPointCollision2D will check these anyway. But by checking them here, |
| 427 |
we can potentially avoid the cost of transforming into local space. } |
| 428 |
if not CheckCollides then Exit(false); |
| 429 |
|
| 430 |
if FOnlyTranslation then |
| 431 |
Result := LocalPointCollision2D( |
| 432 |
Point - TranslationXY, TrianglesToIgnoreFunc) else |
| 433 |
Result := LocalPointCollision2D( |
| 434 |
InverseTransform.MultPoint(Point), TrianglesToIgnoreFunc); |
| 435 |
end; |
| 436 |
|
| 437 |
function TCastleTransform.BoxCollision( |
| 438 |
const Box: TBox3D; |
| 439 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; |
| 440 |
begin |
| 441 |
{ LocalBoxCollision will check these anyway. But by checking them here, |
| 442 |
we can potentially avoid the cost of transforming into local space. } |
| 443 |
if not CheckCollides then Exit(false); |
| 444 |
|
| 445 |
if FOnlyTranslation then |
| 446 |
Result := LocalBoxCollision( |
| 447 |
Box.AntiTranslate(Translation), TrianglesToIgnoreFunc) |
| 448 |
else |
| 449 |
Result := LocalBoxCollision( |
| 450 |
Box.Transform(InverseTransform), TrianglesToIgnoreFunc); |
| 451 |
end; |
| 452 |
|
| 453 |
function TCastleTransform.OutsideToLocal(const Pos: TVector3): TVector3; |
| 454 |
begin |
| 455 |
if FOnlyTranslation then |
| 456 |
Result := Pos - Translation |
| 457 |
else |
| 458 |
Result := InverseTransform.MultPoint(Pos); |
| 459 |
end; |
| 460 |
|
| 461 |
function TCastleTransform.LocalToOutside(const Pos: TVector3): TVector3; |
| 462 |
begin |
| 463 |
if FOnlyTranslation then |
| 464 |
Result := Pos + Translation |
| 465 |
else |
| 466 |
Result := Transform.MultPoint(Pos); |
| 467 |
end; |
| 468 |
|
| 469 |
function TCastleTransform.WorldToLocal(const Pos: TVector3): TVector3; |
| 470 |
begin |
| 471 |
Result := WorldInverseTransform.MultPoint(Pos); |
| 472 |
end; |
| 473 |
|
| 474 |
function TCastleTransform.LocalToWorld(const Pos: TVector3): TVector3; |
| 475 |
begin |
| 476 |
Result := WorldTransform.MultPoint(Pos); |
| 477 |
end; |
| 478 |
|
| 479 |
function TCastleTransform.WorldToLocalDirection(const Dir: TVector3): TVector3; |
| 480 |
begin |
| 481 |
Result := WorldInverseTransform.MultDirection(Dir); |
| 482 |
end; |
| 483 |
|
| 484 |
function TCastleTransform.LocalToWorldDirection(const Dir: TVector3): TVector3; |
| 485 |
begin |
| 486 |
Result := WorldTransform.MultDirection(Dir); |
| 487 |
end; |
| 488 |
|
| 489 |
function TCastleTransform.LocalToWorldDistance(const Distance: Single): Single; |
| 490 |
var |
| 491 |
S: Single; |
| 492 |
begin |
| 493 |
S := Approximate3DScale(WorldTransform); |
| 494 |
Result := Distance * S; |
| 495 |
end; |
| 496 |
|
| 497 |
function TCastleTransform.WorldToLocalDistance(const Distance: Single): Single; |
| 498 |
var |
| 499 |
S: Single; |
| 500 |
begin |
| 501 |
S := 1 / Approximate3DScale(WorldTransform); |
| 502 |
|
| 503 |
{ In principle, this should be the same: |
| 504 |
S := Approximate3DScale(WorldInverseTransform); |
| 505 |
Except there may be some edge cases in case of e.g. zero scale. |
| 506 |
So WorldInverseTransform and WorldTransform do not transform everything perfectly |
| 507 |
in both directions without any loss. |
| 508 |
And we want to satisfy the common expectation that |
| 509 |
LocalToWorldDistance and WorldToLocalDistance invert each other. } |
| 510 |
|
| 511 |
Result := Distance * S; |
| 512 |
end; |
| 513 |
|
| 514 |
function TCastleTransform.InternalRayCollision(const RayOrigin, RayDirection: TVector3): TRayCollision; |
| 515 |
begin |
| 516 |
Result := RayCollision(RayOrigin, RayDirection, nil); |
| 517 |
end; |
| 518 |
|
| 519 |
function TCastleTransform.RayCollision(const RayOrigin, RayDirection: TVector3; |
| 520 |
const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): TRayCollision; |
| 521 |
var |
| 522 |
T: TVector3; |
| 523 |
LastNode: PRayCollisionNode; |
| 524 |
Tr: TTransformation; |
| 525 |
begin |
| 526 |
{ LocalRayCollision will check these anyway. But by checking them here, |
| 527 |
we can potentially avoid the cost of transforming into local space. } |
| 528 |
if not CheckPickable then Exit(nil); |
| 529 |
|
| 530 |
if FOnlyTranslation then |
| 531 |
begin |
| 532 |
T := Translation; |
| 533 |
Result := LocalRayCollision(RayOrigin - T, RayDirection, TrianglesToIgnoreFunc); |
| 534 |
if Result <> nil then |
| 535 |
begin |
| 536 |
LastNode := PRayCollisionNode(Result.Ptr(Result.Count - 1)); |
| 537 |
LastNode^.Point := LastNode^.Point + T; |
| 538 |
{ untransform the ray } |
| 539 |
LastNode^.RayOrigin := RayOrigin; |
| 540 |
LastNode^.RayDirection := RayDirection; |
| 541 |
end; |
| 542 |
end else |
| 543 |
begin |
| 544 |
InternalTransformation(Tr); |
| 545 |
Result := LocalRayCollision( |
| 546 |
Tr.InverseTransform.MultPoint(RayOrigin), |
| 547 |
Tr.InverseTransform.MultDirection(RayDirection), TrianglesToIgnoreFunc); |
| 548 |
if Result <> nil then |
| 549 |
begin |
| 550 |
LastNode := PRayCollisionNode(Result.Ptr(Result.Count - 1)); |
| 551 |
LastNode^.Point := Tr.Transform.MultPoint(LastNode^.Point); |
| 552 |
{ untransform the ray } |
| 553 |
LastNode^.RayOrigin := RayOrigin; |
| 554 |
LastNode^.RayDirection := RayDirection; |
| 555 |
|
| 556 |
{ Note that we should not scale Result.Distance by AverageScale. |
| 557 |
That is because Result.Distance is relative to RayDirection length, |
| 558 |
so it's automatically correct. } |
| 559 |
end; |
| 560 |
end; |
| 561 |
end; |