| 1 |
{%MainUnit castletransform.pas} |
| 2 |
{ |
| 3 |
Copyright 2017-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 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
//type |
| 20 |
{ Result of @link(TCastleAbstractRootTransform.PhysicsRayCast) and |
| 21 |
@link(TCastleRigidBody.PhysicsRayCast). } |
| 22 |
TPhysicsRayCastResult = record |
| 23 |
{ Other fields are guaranteed to be all zero if Hit = @false. } |
| 24 |
Hit: Boolean; |
| 25 |
Transform: TCastleTransform; |
| 26 |
{ Distance from ray origin to the hit point. } |
| 27 |
Distance: Single; |
| 28 |
Point, Normal: TVector3; |
| 29 |
end; |
| 30 |
|
| 31 |
{ Root of transformations and scenes (tree of TCastleTransform and TCastleScene). |
| 32 |
This is the base abstract class, the non-abstract descendant is @link(TCastleRootTransform). } |
| 33 |
TCastleAbstractRootTransform = class(TCastleTransform) |
| 34 |
strict private |
| 35 |
WasPhysicsStep: boolean; |
| 36 |
TimeAccumulator: TFloatTime; |
| 37 |
FEnablePhysics: boolean; |
| 38 |
FMoveLimit: TBox3D; |
| 39 |
FPhysicsProperties: TPhysicsProperties; |
| 40 |
UpdateFrameId: TFrameId; |
| 41 |
FTimeScale: Single; |
| 42 |
FPaused: boolean; |
| 43 |
FMainCamera: TCastleCamera; |
| 44 |
FMainCameraObserver: TFreeNotificationObserver; |
| 45 |
FInternalPressReleaseListeners: TCastleTransformList; |
| 46 |
procedure SetPaused(const Value: boolean); |
| 47 |
procedure SetMainCamera(const Value: TCastleCamera); |
| 48 |
procedure MainCameraFreeNotification(const Sender: TFreeNotificationObserver); |
| 49 |
{ Check all conditions (EnablePhysics, CastleApplicationMode) to find out |
| 50 |
is physics running. } |
| 51 |
function IsPhysicsRunning: Boolean; |
| 52 |
private |
| 53 |
FKraftEngine: TKraft; |
| 54 |
{ Create FKraftEngine, if not assigned yet. } |
| 55 |
procedure InitializePhysicsEngine; |
| 56 |
{ Destroy everything related to physics, if present. } |
| 57 |
procedure DestroyPhysicsEngine; |
| 58 |
procedure RegisterPressRelease(const T: TCastleTransform); |
| 59 |
procedure UnregisterPressRelease(const T: TCastleTransform); |
| 60 |
protected |
| 61 |
{ Ignore (return @true) transparent triangles |
| 62 |
(with Material.Transparency > 0). |
| 63 |
|
| 64 |
This is suitable for TTriangleIgnoreFunc function, you can pass |
| 65 |
this to RayCollision and such. } |
| 66 |
function IgnoreTransparentItem( |
| 67 |
const Sender: TObject; const Triangle: PTriangle): Boolean; virtual; abstract; |
| 68 |
public |
| 69 |
InternalOnNodesFree: TNotifyEvent; |
| 70 |
|
| 71 |
{ Event to render whole world. |
| 72 |
Used by generated textures to update their contents. |
| 73 |
@exclude } |
| 74 |
InternalRenderEverythingEvent: TRenderFromViewFunction; |
| 75 |
|
| 76 |
{ Projection near/far used. May be used when updating generated textures, |
| 77 |
to determine their projection parameters. |
| 78 |
@exclude } |
| 79 |
InternalProjectionNear, InternalProjectionFar: Single; |
| 80 |
|
| 81 |
{ Changes every time MainCamera vectors change. |
| 82 |
Allows to track camera changes in scenes. |
| 83 |
@exclude } |
| 84 |
InternalMainCameraStateId: TFrameId; |
| 85 |
|
| 86 |
{ Latest frame when VisibleChangeNotification was called (with any params, even empty). } |
| 87 |
InternalVisibleStateId: TFrameId; |
| 88 |
{ Latest frame when VisibleChangeNotification was called (with vcVisibleGeometry). } |
| 89 |
InternalVisibleGeometryStateId: TFrameId; |
| 90 |
{ Latest frame when VisibleChangeNotification was called (with vcVisibleNonGeometry). } |
| 91 |
InternalVisibleNonGeometryStateId: TFrameId; |
| 92 |
|
| 93 |
constructor Create(AOwner: TComponent); override; |
| 94 |
destructor Destroy; override; |
| 95 |
procedure Update(const SecondsPassed: Single; var RemoveMe: TRemoveType); override; |
| 96 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 97 |
procedure CustomSerialization(const SerializationProcess: TSerializationProcess); override; |
| 98 |
|
| 99 |
{ @exclude |
| 100 |
TCastleTransform instances that listen on press/release. |
| 101 |
May be @nil, equivalent to empty. } |
| 102 |
property InternalPressReleaseListeners: TCastleTransformList read FInternalPressReleaseListeners; |
| 103 |
|
| 104 |
{ The major axis of gravity vector: 0, 1 or 2. |
| 105 |
This is trivially derived from the known camera |
| 106 |
GravityUp value. It can only truly express |
| 107 |
GravityUp vector values (1,0,0) or (0,1,0) or (0,0,1), |
| 108 |
although in practice this is enough for normal games (normal 3D scenes |
| 109 |
use up either +Y or +Z). |
| 110 |
|
| 111 |
We try to avoid using it in |
| 112 |
the engine, and use full GravityUp vector wherever possible. |
| 113 |
Full GravityUp vector may allow for more fun with weird gravity |
| 114 |
in future games. } |
| 115 |
function GravityCoordinate: Integer; |
| 116 |
|
| 117 |
function GravityUp: TVector3; |
| 118 |
// TODO: I would like to deprecate it, |
| 119 |
// but it is so often useful, and the alternative MainCamera.GravityUp looks convoluted. |
| 120 |
// Leave it be for now. |
| 121 |
// TODO: deprecated 'use MainCamera.GravityUp if MainCamera <> nil'; |
| 122 |
|
| 123 |
{ Is the move from OldPos to ProposedNewPos possible. |
| 124 |
Returns true and sets NewPos if some move is allowed, thus allows for wall-sliding. |
| 125 |
|
| 126 |
This checks collisions with world |
| 127 |
(everything inside this @link(TCastleAbstractRootTransform)). |
| 128 |
|
| 129 |
This checks collision with all objects that have @link(Collides) |
| 130 |
and @link(Exists) equal @true (using @link(CheckCollides)). |
| 131 |
|
| 132 |
If your query originates from some existing TCastleTransform instance, |
| 133 |
you should prefer to instead use @link(TCastleTransform.MoveAllowed) |
| 134 |
method, that automatically prevents "collisions with yourself". |
| 135 |
|
| 136 |
@seealso TCastleTransform.MoveAllowed } |
| 137 |
function WorldMoveAllowed( |
| 138 |
const OldPos, ProposedNewPos: TVector3; out NewPos: TVector3; |
| 139 |
const IsRadius: boolean; const Radius: Single; |
| 140 |
const OldBox, NewBox: TBox3D; |
| 141 |
const BecauseOfGravity: boolean): boolean; overload; |
| 142 |
|
| 143 |
{ Is the move from OldPos to NewPos possible. |
| 144 |
|
| 145 |
This checks collisions with world |
| 146 |
(everything inside this @link(TCastleAbstractRootTransform)). |
| 147 |
|
| 148 |
This checks collision with all objects that have @link(Collides) |
| 149 |
and @link(Exists) equal @true (using @link(CheckCollides)). |
| 150 |
|
| 151 |
If your query originates from some existing TCastleTransform instance, |
| 152 |
you should prefer to instead use @link(TCastleTransform.MoveAllowed) |
| 153 |
method, that automatically prevents "collisions with yourself". |
| 154 |
|
| 155 |
@seealso TCastleTransform.MoveAllowed } |
| 156 |
function WorldMoveAllowed( |
| 157 |
const OldPos, NewPos: TVector3; |
| 158 |
const IsRadius: boolean; const Radius: Single; |
| 159 |
const OldBox, NewBox: TBox3D; |
| 160 |
const BecauseOfGravity: boolean): boolean; overload; |
| 161 |
|
| 162 |
{ Get height of point APosition above the world. |
| 163 |
Looks at current @link(MainCamera) to know the gravity direction. |
| 164 |
|
| 165 |
This checks collisions with world |
| 166 |
(everything inside this @link(TCastleAbstractRootTransform)). |
| 167 |
|
| 168 |
This checks collision with all objects that have @link(Collides) |
| 169 |
and @link(Exists) equal @true (using @link(CheckCollides)). |
| 170 |
|
| 171 |
If your query originates from some existing TCastleTransform instance, |
| 172 |
you should prefer to instead use @link(TCastleTransform.MoveAllowed) |
| 173 |
method, that automatically prevents "collisions with yourself". } |
| 174 |
function WorldHeight(const APosition: TVector3; |
| 175 |
out AboveHeight: Single; out AboveGround: PTriangle): boolean; |
| 176 |
|
| 177 |
{ Check that the line segment between 2 points that not collide with anything |
| 178 |
(that has opaque material). |
| 179 |
|
| 180 |
This checks collisions with world |
| 181 |
(everything inside this @link(TCastleAbstractRootTransform)). |
| 182 |
|
| 183 |
This checks collision with all objects that have @link(Collides) |
| 184 |
and @link(Exists) equal @true (using @link(CheckCollides)). } |
| 185 |
function WorldLineOfSight(const Pos1, Pos2: TVector3): boolean; |
| 186 |
|
| 187 |
{ First object hit by the ray. |
| 188 |
|
| 189 |
This checks collisions with world |
| 190 |
(everything inside this @link(TCastleAbstractRootTransform)). |
| 191 |
|
| 192 |
This checks collision with all objects that have @link(Collides) |
| 193 |
and @link(Exists) equal @true (using @link(CheckCollides)). } |
| 194 |
function WorldRay(const RayOrigin, RayDirection: TVector3): TRayCollision; |
| 195 |
|
| 196 |
{ First object hit by the ray. |
| 197 |
|
| 198 |
Returns the TCastleTransform that is hit (this is the "leaf" TCastleTransform |
| 199 |
in the TCastleTransform tree that is hit) |
| 200 |
and a distance from RayOrigin to the hit point. |
| 201 |
Returns @nil (Distance is undefined in this case) if nothing was hit. |
| 202 |
Use @link(WorldRay) for a more advanced version of this, with more complicated result. |
| 203 |
@groupBegin } |
| 204 |
function WorldRayCast(const RayOrigin, RayDirection: TVector3; |
| 205 |
out Distance: Single): TCastleTransform; overload; |
| 206 |
function WorldRayCast(const RayOrigin, |
| 207 |
RayDirection: TVector3): TCastleTransform; overload; |
| 208 |
{ @groupEnd } |
| 209 |
|
| 210 |
{ Check whether something collides with axis-aligned box in 3D. |
| 211 |
|
| 212 |
This checks collisions with world |
| 213 |
(everything inside this @link(TCastleAbstractRootTransform)). |
| 214 |
|
| 215 |
This checks collision with all objects that have @link(Collides) |
| 216 |
and @link(Exists) equal @true (using @link(CheckCollides)). } |
| 217 |
function WorldBoxCollision(const Box: TBox3D): boolean; |
| 218 |
|
| 219 |
{ Check whether something collides with a line segment. |
| 220 |
|
| 221 |
This checks collisions with world |
| 222 |
(everything inside this @link(TCastleAbstractRootTransform)). |
| 223 |
|
| 224 |
This checks collision with all objects that have @link(Collides) |
| 225 |
and @link(Exists) equal @true (using @link(CheckCollides)). } |
| 226 |
function WorldSegmentCollision(const Pos1, Pos2: TVector3): boolean; |
| 227 |
|
| 228 |
{ Check whether something collides with a sphere. |
| 229 |
|
| 230 |
This checks collisions with world |
| 231 |
(everything inside this @link(TCastleAbstractRootTransform)). |
| 232 |
|
| 233 |
This checks collision with all objects that have @link(Collides) |
| 234 |
and @link(Exists) equal @true (using @link(CheckCollides)). } |
| 235 |
function WorldSphereCollision(const Pos: TVector3; const Radius: Single): boolean; |
| 236 |
|
| 237 |
{ Check whether something collides with a sphere in 2D |
| 238 |
(a circle, extruded to infinity along the Z axis). |
| 239 |
|
| 240 |
This checks collisions with world |
| 241 |
(everything inside this @link(TCastleAbstractRootTransform)). |
| 242 |
|
| 243 |
This checks collision with all objects that have @link(Collides) |
| 244 |
and @link(Exists) equal @true (using @link(CheckCollides)). } |
| 245 |
function WorldSphereCollision2D(const Pos: TVector2; const Radius: Single; |
| 246 |
const Details: TCollisionDetails = nil): boolean; |
| 247 |
|
| 248 |
{ Check whether something collides with a point in 2D |
| 249 |
(which is an infinite line along the Z axis in 3D). |
| 250 |
|
| 251 |
This checks collisions with world |
| 252 |
(everything inside this @link(TCastleAbstractRootTransform)). |
| 253 |
|
| 254 |
This checks collision with all objects that have @link(Collides) |
| 255 |
and @link(Exists) equal @true (using @link(CheckCollides)). } |
| 256 |
function WorldPointCollision2D(const Point: TVector2): boolean; |
| 257 |
|
| 258 |
{ First object hit by the ray, calculated using the physics engine. |
| 259 |
|
| 260 |
Returns the TPhysicsRayCastResult record. |
| 261 |
Read TPhysicsRayCastResult.Hit (boolean), TPhysicsRayCastResult.Transform |
| 262 |
(guaranteed to be @nil when TPhysicsRayCastResult.Hit is @false) and other fields |
| 263 |
to know what was hit. |
| 264 |
|
| 265 |
The given RayOrigin, RayDirection, Distance are in the world coordinate system. |
| 266 |
|
| 267 |
It returns TPhysicsRayCastResult, with information there |
| 268 |
also in the world coordinate system. |
| 269 |
|
| 270 |
This ignores the collider of this body IgnoreBody, if IgnoreBody is not @nil. |
| 271 |
|
| 272 |
Note: The @link(TCastleTransform.Pickable) property is ignored by this method, |
| 273 |
i.e. it considers all colliders regardless of their @link(TCastleTransform.Pickable) value. |
| 274 |
The @link(TCastleTransform.Pickable) is only taken into account by the routines |
| 275 |
of the old physics system |
| 276 |
(see https://castle-engine.io/physics#_old_system_for_collisions_and_gravity ). |
| 277 |
In the new system, a similar role will be filled by layers. } |
| 278 |
function PhysicsRayCast(const RayOrigin, RayDirection: TVector3; |
| 279 |
const MaxDistance: Single = MaxSingle; |
| 280 |
const IgnoreBody: TCastleRigidBody = nil): TPhysicsRayCastResult; |
| 281 |
|
| 282 |
{ Yoo can temporarily disable physics (no transformations will be updated |
| 283 |
by the physics engine) by setting this property to @false. } |
| 284 |
property EnablePhysics: boolean read FEnablePhysics write FEnablePhysics |
| 285 |
default true; |
| 286 |
|
| 287 |
{ Limit the movement allowed by @link(WorldMoveAllowed). |
| 288 |
Ignored when empty (default). |
| 289 |
|
| 290 |
This property allows to easily limit the possible places |
| 291 |
where player and creatures go. |
| 292 |
Player is honoring this if it uses @link(WorldMoveAllowed), |
| 293 |
in particular our @link(TCastleWalkNavigation) navigation honors it. |
| 294 |
Creatures honor it if they use @link(WorldMoveAllowed) for their decision. } |
| 295 |
property MoveLimit: TBox3D read FMoveLimit write FMoveLimit; |
| 296 |
|
| 297 |
{ Central camera, that controls the features that require |
| 298 |
a single "main" camera (features that do not make sense |
| 299 |
with multiple cameras from multiple viewports). |
| 300 |
|
| 301 |
This camera controls: |
| 302 |
|
| 303 |
@unorderedList( |
| 304 |
@item(Nodes that "sense" camera like TProximitySensorNode, TBillboardNode, TLODNode.) |
| 305 |
|
| 306 |
@item(TCastleBillboard.) |
| 307 |
|
| 308 |
@item(Audio listener (controlling the spatial sound).) |
| 309 |
|
| 310 |
@item(Headlight position/direction, if you enable headlight by @link(TCastleRootTransform.UseHeadlight).) |
| 311 |
|
| 312 |
@item(When X3D nodes change Viewpoint/NavigationInfo (possible if TCastleViewport.AutoCamera), |
| 313 |
they apply changes to this camera.) |
| 314 |
|
| 315 |
@item(Rendering of TRenderedTextureNode contents (which camera this texture shows).) |
| 316 |
|
| 317 |
@item(At design-time: gizmo size is determined with respect to this camera.) |
| 318 |
) |
| 319 |
|
| 320 |
Note that it means that "headlight" done by |
| 321 |
@link(TCastleRootTransform.UseHeadlight) is assigned to one camera |
| 322 |
in case of multiple viewports looking at the same world. |
| 323 |
You cannot have a different "headlight" in each viewport, |
| 324 |
this would cause subtle problems since it's not how it would work in reality |
| 325 |
(where every light is visible in all viewports), |
| 326 |
e.g. mirror textures (like GeneratedCubeMapTexture) |
| 327 |
would need different contents in different viewpoints. |
| 328 |
|
| 329 |
Note that we no longer advise using @link(TCastleRootTransform.UseHeadlight) |
| 330 |
for headlight. It's easier to just place light as child of whatever camera |
| 331 |
you want, like adding TCastleDirectionalLight as child of TCastleCamera. |
| 332 |
See examples/viewport_and_scenes/headlight/ . |
| 333 |
In this case MainCamera doesn't matter, instead you explicitly make headlight |
| 334 |
by placing light as child of chosen camera. |
| 335 |
|
| 336 |
Note that features like LOD or Billboard or ProximitySensor |
| 337 |
need to transform this camera to scene local space. |
| 338 |
Which is not possible if the same scene instance |
| 339 |
is used multiple times (e.g. under many different TCastleTransform parents). |
| 340 |
If you need to use these features of TCastleScene, |
| 341 |
then simply do not use the same scene reference multiple times |
| 342 |
(instead clone the scene by @link(TCastleScene.Clone)). |
| 343 |
|
| 344 |
By default this is set to @link(TCastleViewport.Camera) of the @link(TCastleViewport) |
| 345 |
that created this @link(TCastleAbstractRootTransform) instance. |
| 346 |
And if something changes the @link(TCastleViewport.Camera) of that viewport, |
| 347 |
then this @name is also updated. |
| 348 |
So in simple cases (when you just have one @link(TCastleViewport) |
| 349 |
and add your scenes to it's already-created @link(TCastleViewport.Items)) |
| 350 |
you don't have to do anything, it just works. |
| 351 |
In general, you can change this to any camera (currently used by some |
| 352 |
associated @link(TCastleViewport) or not), |
| 353 |
or @nil (in case no camera should be that "central" camera). } |
| 354 |
property MainCamera: TCastleCamera read FMainCamera write SetMainCamera; |
| 355 |
|
| 356 |
{ Remove all items except TCastleCamera instances. |
| 357 |
This only avoids removing TCastleCamera instances that are direct children |
| 358 |
of this TCastleAbstractRootTransform, the TCastleCamera instances that are deeper |
| 359 |
(e.g. TCastleCamera inside some TCastleTransform) will still be removed. } |
| 360 |
procedure ClearExceptCameras; |
| 361 |
published |
| 362 |
{ Adjust physics properties. } |
| 363 |
property PhysicsProperties: TPhysicsProperties read FPhysicsProperties; |
| 364 |
|
| 365 |
{ Time scale used when not @link(Paused). } |
| 366 |
property TimeScale: Single read FTimeScale write FTimeScale {$ifdef FPC}default 1{$endif}; |
| 367 |
|
| 368 |
{ Pausing means that no events (key, mouse, update) are processed. |
| 369 |
So time doesn't move, and input is not processed. |
| 370 |
|
| 371 |
Navigation also doesn't work (this part is implemented by TCastleViewport |
| 372 |
and each TCastleNavigation). |
| 373 |
|
| 374 |
This is useful if you want to unconditionally make your world temporary |
| 375 |
still (for example, useful when entering some modal dialog box |
| 376 |
and you want the world to behave as a still background). |
| 377 |
|
| 378 |
@italic(See also): For other pausing methods, |
| 379 |
there are other methods of pausing / disabling |
| 380 |
some events processing for the world: |
| 381 |
|
| 382 |
@unorderedList( |
| 383 |
@item(You can set TCastleScene.TimePlaying to @false. |
| 384 |
This is roughly equivalent to not running their @link(Update) methods. |
| 385 |
This means that time will "stand still" for them, |
| 386 |
so their animations will not play. Although they may |
| 387 |
still react and change in response to mouse clicks / key presses, |
| 388 |
if TCastleScene.ProcessEvents.) |
| 389 |
|
| 390 |
@item(You can set TCastleScene.ProcessEvents to @false. |
| 391 |
This means that scene will not receive and process any |
| 392 |
key / mouse and other events (through VRML/X3D sensors). |
| 393 |
Some animations (not depending on VRML/X3D events processing) |
| 394 |
may still run, for example MovieTexture will still animate, |
| 395 |
if only TCastleScene.TimePlaying.) |
| 396 |
|
| 397 |
@item(For navigation, you can set @code(TCastleNavigation.Input := []) to ignore |
| 398 |
key / mouse clicks. |
| 399 |
|
| 400 |
Or you can set @code(TCastleNavigation.Exists) to @false, |
| 401 |
this is actually equivalent to what pausing does now for TCastleNavigation. |
| 402 |
) |
| 403 |
) } |
| 404 |
property Paused: boolean read FPaused write SetPaused default false; |
| 405 |
end; |
| 406 |
|
| 407 |
{$endif read_interface} |
| 408 |
|
| 409 |
{$ifdef read_implementation} |
| 410 |
|
| 411 |
{ TCastleAbstractRootTransform ------------------------------------------------------------------- } |
| 412 |
|
| 413 |
constructor TCastleAbstractRootTransform.Create(AOwner: TComponent); |
| 414 |
begin |
| 415 |
inherited; |
| 416 |
|
| 417 |
FPhysicsProperties := TPhysicsProperties.Create(Self); |
| 418 |
FPhysicsProperties.SetSubComponent(true); |
| 419 |
FPhysicsProperties.Name := 'PhysicsProperties'; |
| 420 |
FPhysicsProperties.RootTransform := Self; |
| 421 |
|
| 422 |
FMainCameraObserver := TFreeNotificationObserver.Create(Self); |
| 423 |
FMainCameraObserver.OnFreeNotification := {$ifdef FPC}@{$endif}MainCameraFreeNotification; |
| 424 |
|
| 425 |
FTimeScale := 1; |
| 426 |
FMoveLimit := TBox3D.Empty; |
| 427 |
FEnablePhysics := true; |
| 428 |
|
| 429 |
{ everything inside is part of this world } |
| 430 |
AddToWorld(Self); |
| 431 |
end; |
| 432 |
|
| 433 |
destructor TCastleAbstractRootTransform.Destroy; |
| 434 |
begin |
| 435 |
DestroyPhysicsEngine; |
| 436 |
FreeAndNil(FInternalPressReleaseListeners); |
| 437 |
inherited; |
| 438 |
end; |
| 439 |
|
| 440 |
function TCastleAbstractRootTransform.GravityCoordinate: Integer; |
| 441 |
begin |
| 442 |
Result := MaxAbsVectorCoord(GravityUp); |
| 443 |
end; |
| 444 |
|
| 445 |
function TCastleAbstractRootTransform.WorldBoxCollision(const Box: TBox3D): boolean; |
| 446 |
begin |
| 447 |
Result := BoxCollision(Box, nil); |
| 448 |
end; |
| 449 |
|
| 450 |
function TCastleAbstractRootTransform.WorldSegmentCollision(const Pos1, Pos2: TVector3): boolean; |
| 451 |
begin |
| 452 |
Result := SegmentCollision(Pos1, Pos2, nil, false); |
| 453 |
end; |
| 454 |
|
| 455 |
function TCastleAbstractRootTransform.WorldSphereCollision(const Pos: TVector3; |
| 456 |
const Radius: Single): boolean; |
| 457 |
begin |
| 458 |
Result := SphereCollision(Pos, Radius, nil); |
| 459 |
end; |
| 460 |
|
| 461 |
function TCastleAbstractRootTransform.WorldSphereCollision2D(const Pos: TVector2; |
| 462 |
const Radius: Single; |
| 463 |
const Details: TCollisionDetails): boolean; |
| 464 |
begin |
| 465 |
Result := SphereCollision2D(Pos, Radius, nil, Details); |
| 466 |
end; |
| 467 |
|
| 468 |
function TCastleAbstractRootTransform.WorldPointCollision2D(const Point: TVector2): boolean; |
| 469 |
begin |
| 470 |
Result := PointCollision2D(Point, nil); |
| 471 |
end; |
| 472 |
|
| 473 |
function TCastleAbstractRootTransform.IsPhysicsRunning: Boolean; |
| 474 |
begin |
| 475 |
Result := |
| 476 |
EnablePhysics and |
| 477 |
(FKraftEngine <> nil) and |
| 478 |
(CastleApplicationMode in [appRunning, appSimulation]); |
| 479 |
end; |
| 480 |
|
| 481 |
function TCastleAbstractRootTransform.WorldHeight(const APosition: TVector3; |
| 482 |
out AboveHeight: Single; out AboveGround: PTriangle): boolean; |
| 483 |
begin |
| 484 |
Result := HeightCollision(APosition, GravityUp, nil, |
| 485 |
AboveHeight, AboveGround); |
| 486 |
end; |
| 487 |
|
| 488 |
function TCastleAbstractRootTransform.WorldLineOfSight(const Pos1, Pos2: TVector3): boolean; |
| 489 |
begin |
| 490 |
Result := not SegmentCollision(Pos1, Pos2, |
| 491 |
{ Ignore transparent materials, this means that creatures can see through |
| 492 |
glass --- even though they can't walk through it. } |
| 493 |
{$ifdef FPC}@{$endif} IgnoreTransparentItem, |
| 494 |
true); |
| 495 |
end; |
| 496 |
|
| 497 |
function TCastleAbstractRootTransform.WorldRay( |
| 498 |
const RayOrigin, RayDirection: TVector3): TRayCollision; |
| 499 |
begin |
| 500 |
Result := RayCollision(RayOrigin, RayDirection, nil); |
| 501 |
end; |
| 502 |
|
| 503 |
function TCastleAbstractRootTransform.WorldRayCast(const RayOrigin, RayDirection: TVector3; out Distance: Single): TCastleTransform; |
| 504 |
var |
| 505 |
RayColl: TRayCollision; |
| 506 |
begin |
| 507 |
Result := nil; |
| 508 |
Distance := 0; // just to make it defined |
| 509 |
|
| 510 |
RayColl := WorldRay(RayOrigin, RayDirection); |
| 511 |
if RayColl <> nil then |
| 512 |
try |
| 513 |
if RayColl.Count <> 0 then |
| 514 |
begin |
| 515 |
Result := RayColl.First.Item; |
| 516 |
Distance := RayColl.Distance; |
| 517 |
end; |
| 518 |
finally FreeAndNil(RayColl) end; |
| 519 |
end; |
| 520 |
|
| 521 |
function TCastleAbstractRootTransform.WorldRayCast(const RayOrigin, RayDirection: TVector3): TCastleTransform; |
| 522 |
var |
| 523 |
IgnoredDistance: Single; |
| 524 |
begin |
| 525 |
Result := WorldRayCast(RayOrigin, RayDirection, IgnoredDistance); |
| 526 |
end; |
| 527 |
|
| 528 |
function TCastleAbstractRootTransform.WorldMoveAllowed( |
| 529 |
const OldPos, ProposedNewPos: TVector3; out NewPos: TVector3; |
| 530 |
const IsRadius: boolean; const Radius: Single; |
| 531 |
const OldBox, NewBox: TBox3D; |
| 532 |
const BecauseOfGravity: boolean): boolean; |
| 533 |
begin |
| 534 |
Result := MoveCollision(OldPos, ProposedNewPos, NewPos, IsRadius, Radius, |
| 535 |
OldBox, NewBox, nil); |
| 536 |
if Result then |
| 537 |
Result := MoveLimit.IsEmpty or MoveLimit.Contains(NewPos); |
| 538 |
end; |
| 539 |
|
| 540 |
function TCastleAbstractRootTransform.WorldMoveAllowed( |
| 541 |
const OldPos, NewPos: TVector3; |
| 542 |
const IsRadius: boolean; const Radius: Single; |
| 543 |
const OldBox, NewBox: TBox3D; |
| 544 |
const BecauseOfGravity: boolean): boolean; |
| 545 |
begin |
| 546 |
Result := MoveCollision(OldPos, NewPos, IsRadius, Radius, |
| 547 |
OldBox, NewBox, nil); |
| 548 |
if Result then |
| 549 |
Result := MoveLimit.IsEmpty or MoveLimit.Contains(NewPos); |
| 550 |
end; |
| 551 |
|
| 552 |
procedure TCastleAbstractRootTransform.SetPaused(const Value: boolean); |
| 553 |
begin |
| 554 |
if FPaused <> Value then |
| 555 |
begin |
| 556 |
FPaused := Value; |
| 557 |
{ TODO: update the viewport cursor when Paused changed. } |
| 558 |
// RecalculateCursor(Self); |
| 559 |
end; |
| 560 |
end; |
| 561 |
|
| 562 |
procedure TCastleAbstractRootTransform.SetMainCamera(const Value: TCastleCamera); |
| 563 |
begin |
| 564 |
if FMainCamera <> Value then |
| 565 |
begin |
| 566 |
FMainCameraObserver.Observed := Value; |
| 567 |
FMainCamera := Value; |
| 568 |
VisibleChangeHere([]); |
| 569 |
end; |
| 570 |
end; |
| 571 |
|
| 572 |
procedure TCastleAbstractRootTransform.MainCameraFreeNotification(const Sender: TFreeNotificationObserver); |
| 573 |
begin |
| 574 |
MainCamera := nil; |
| 575 |
end; |
| 576 |
|
| 577 |
function TCastleAbstractRootTransform.GravityUp: TVector3; |
| 578 |
begin |
| 579 |
if MainCamera = nil then |
| 580 |
{ This is only to keep deprecated GravityUp/GravityCoordinate |
| 581 |
sensible, even for old code that doesn't check MainCamera <> nil. } |
| 582 |
Result := DefaultCameraUp |
| 583 |
else |
| 584 |
Result := MainCamera.GravityUp; |
| 585 |
end; |
| 586 |
|
| 587 |
procedure TCastleAbstractRootTransform.RegisterPressRelease(const T: TCastleTransform); |
| 588 |
begin |
| 589 |
if FInternalPressReleaseListeners = nil then |
| 590 |
FInternalPressReleaseListeners := TCastleTransformList.Create(false); |
| 591 |
FInternalPressReleaseListeners.Add(T); |
| 592 |
end; |
| 593 |
|
| 594 |
procedure TCastleAbstractRootTransform.UnregisterPressRelease(const T: TCastleTransform); |
| 595 |
var |
| 596 |
I: Integer; |
| 597 |
begin |
| 598 |
if FInternalPressReleaseListeners = nil then |
| 599 |
I := -1 |
| 600 |
else |
| 601 |
I := FInternalPressReleaseListeners.IndexOf(T); |
| 602 |
|
| 603 |
if I = -1 then |
| 604 |
begin |
| 605 |
WritelnWarning('Transformation called UnregisterPressRelease, but it was not listening to Press/Release'); |
| 606 |
Exit; |
| 607 |
end; |
| 608 |
|
| 609 |
FInternalPressReleaseListeners.Delete(I); |
| 610 |
end; |
| 611 |
|
| 612 |
procedure TCastleAbstractRootTransform.ClearExceptCameras; |
| 613 |
var |
| 614 |
I: Integer; |
| 615 |
begin |
| 616 |
for I := Count - 1 downto 0 do |
| 617 |
if not (Items[I] is TCastleCamera) then |
| 618 |
Delete(I); |
| 619 |
end; |
| 620 |
|
| 621 |
function TCastleAbstractRootTransform.PropertySections(const PropertyName: String): TPropertySections; |
| 622 |
begin |
| 623 |
if ArrayContainsString(PropertyName, [ |
| 624 |
'TimeScale', 'Paused', 'PhysicsProperties' |
| 625 |
]) then |
| 626 |
Result := [psBasic] |
| 627 |
else |
| 628 |
Result := inherited PropertySections(PropertyName); |
| 629 |
end; |
| 630 |
|
| 631 |
procedure TCastleAbstractRootTransform.CustomSerialization( |
| 632 |
const SerializationProcess: TSerializationProcess); |
| 633 |
|
| 634 |
function ViewportCaption: String; |
| 635 |
begin |
| 636 |
if (Owner <> nil) and (Owner.Name <> '') then |
| 637 |
Result := '"' + Owner.Name + '.Items"' |
| 638 |
else |
| 639 |
Result := 'TCastleViewport.Items'; |
| 640 |
end; |
| 641 |
|
| 642 |
var |
| 643 |
BlendingSortStr: String; |
| 644 |
begin |
| 645 |
inherited; |
| 646 |
BlendingSortStr := ''; |
| 647 |
SerializationProcess.ReadWriteString('BlendingSort', BlendingSortStr, false); |
| 648 |
if BlendingSortStr <> '' then |
| 649 |
begin |
| 650 |
WritelnWarning('%s uses "BlendingSort" = "%s", this is no longer supported on TCastleAbstractRootTransform (Viewport.Items). Instead set TCastleViewport.BlendingSort property.', [ |
| 651 |
ViewportCaption, |
| 652 |
BlendingSortStr |
| 653 |
]); |
| 654 |
end; |
| 655 |
end; |
| 656 |
|
| 657 |
{$endif read_implementation} |