| 1 |
{ |
| 2 |
Copyright 2020-2024 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 |
{ Third-person navigation (TCastleThirdPersonNavigation). } |
| 17 |
unit CastleThirdPersonNavigation; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
{.$define CASTLE_UNFINISHED_CHANGE_TRANSFORMATION_BY_FORCE} |
| 22 |
|
| 23 |
interface |
| 24 |
|
| 25 |
uses SysUtils, Classes, |
| 26 |
CastleKeysMouse, CastleScene, CastleVectors, CastleCameras, |
| 27 |
CastleTransform, CastleInputs, CastleClassUtils; |
| 28 |
|
| 29 |
type |
| 30 |
{ Used by TCastleThirdPersonNavigation.AimAvatar. } |
| 31 |
TAimAvatar = (aaNone, aaHorizontal, aaFlying); |
| 32 |
|
| 33 |
{ How does the avatar change transformation (for movement and rotations). } |
| 34 |
TChangeTransformation = ( |
| 35 |
{ Automatically determines best way to change transformation. |
| 36 |
|
| 37 |
Right now, it means we |
| 38 |
|
| 39 |
@unorderedList( |
| 40 |
@item(Behave like ctVelocity if avatar is under the control of the physics engine. |
| 41 |
This means avatar has both TCastleRigidBody and TCastleCollider behaviors and |
| 42 |
@link(TCastleRigidBody.Exists) is @true.) |
| 43 |
@item(Behave like ctDirect otherwise.) |
| 44 |
) |
| 45 |
|
| 46 |
To be precise, we look at @link(TCastleThirdPersonNavigation.AvatarHierarchy) or |
| 47 |
(if it's @nil) at @link(TCastleThirdPersonNavigation.Avatar). |
| 48 |
In this transform, we check existence of TCastleRigidBody and TCastleCollider. |
| 49 |
|
| 50 |
In the future, this auto-detection may change, |
| 51 |
to follow our best recommended practices. |
| 52 |
In particular, when ctForce approach (see below) will be fully implemented, |
| 53 |
it will likely become the new automatic behavior when |
| 54 |
TCastleRigidBody and TCastleCollider behaviors are present. } |
| 55 |
ctAuto, |
| 56 |
|
| 57 |
{ Directly change the avatar @link(TCastleTransform.Translation), |
| 58 |
@link(TCastleTransform.Rotation). |
| 59 |
|
| 60 |
You @italic(should not) have physics components (TCastleRigidBody and TCastleCollider |
| 61 |
and @link(TCastleRigidBody.Exists) = @true) |
| 62 |
set up on the @link(TCastleThirdPersonNavigation.AvatarHierarchy) or |
| 63 |
@link(TCastleThirdPersonNavigation.Avatar) in this case. |
| 64 |
Having physics components will make it impossible to change @link(TCastleTransform.Translation), |
| 65 |
@link(TCastleTransform.Rotation) each frame. |
| 66 |
|
| 67 |
This also means that if you want to have gravity (and stair climbing), |
| 68 |
you need to use deprecated @link(TCastleTransform.Gravity), |
| 69 |
@link(TCastleTransform.GrowSpeed), @link(TCastleTransform.FallSpeed). |
| 70 |
They are part of the old simple physics engine: |
| 71 |
https://castle-engine.io/physics#_old_system_for_collisions_and_gravity . |
| 72 |
|
| 73 |
TODO: Jumping and falling doesn't work in this case. |
| 74 |
} |
| 75 |
ctDirect, |
| 76 |
|
| 77 |
{ Change the avatar using rigid body @link(TCastleRigidBody.LinearVelocity), |
| 78 |
@link(TCastleRigidBody.AngularVelocity). |
| 79 |
|
| 80 |
This is not fully realistic (instead of calculating velocities explicitly |
| 81 |
we should be using forces). But it cooperates nicely with physics engine. |
| 82 |
|
| 83 |
It requires a TCastleRigidBody and TCastleCollider components |
| 84 |
to be attached to the @link(TCastleThirdPersonNavigation.AvatarHierarchy) |
| 85 |
(or @link(TCastleThirdPersonNavigation.Avatar), if @link(TCastleThirdPersonNavigation.AvatarHierarchy) is @nil). |
| 86 |
Also @link(TCastleRigidBody.Exists) must be @true to make navigation have any effect. |
| 87 |
|
| 88 |
This also means that gravity should be handled by the physics engine. |
| 89 |
You should not use deprecated @link(TCastleTransform.Gravity), |
| 90 |
@link(TCastleTransform.GrowSpeed), @link(TCastleTransform.FallSpeed) in this case. |
| 91 |
|
| 92 |
TODO: Climbing stairs doesn't work in this case (but you can jump on them). |
| 93 |
} |
| 94 |
ctVelocity |
| 95 |
|
| 96 |
{$ifdef CASTLE_UNFINISHED_CHANGE_TRANSFORMATION_BY_FORCE}, |
| 97 |
|
| 98 |
{ Change the avatar using rigid body forces like @link(TCastleRigidBody.AddForce), |
| 99 |
@link(TCastleRigidBody.AddTorque). |
| 100 |
|
| 101 |
This is realistic and cooperates nicely with physics engine. |
| 102 |
|
| 103 |
It requires a TCastleRigidBody and TCastleCollider components |
| 104 |
to be attached to the @link(TCastleThirdPersonNavigation.AvatarHierarchy) |
| 105 |
(or @link(TCastleThirdPersonNavigation.Avatar), if @link(TCastleThirdPersonNavigation.AvatarHierarchy) is @nil). |
| 106 |
Also @link(TCastleRigidBody.Exists) must be @true to make navigation have any effect. |
| 107 |
|
| 108 |
TODO: Unfinished, not really functional now. } |
| 109 |
ctForce |
| 110 |
{$endif} |
| 111 |
); |
| 112 |
|
| 113 |
TCastleThirdPersonNavigation = class; |
| 114 |
|
| 115 |
TCastleThirdPersonNavigationAnimationEvent = procedure ( |
| 116 |
const Sender: TCastleThirdPersonNavigation; |
| 117 |
const AnimationNames: array of String) of object; |
| 118 |
|
| 119 |
(* 3rd-person (with visible avatar) navigation. |
| 120 |
|
| 121 |
Create an instance of this and put as TCastleViewport child to use. |
| 122 |
|
| 123 |
Assign @link(Avatar) to automatically run proper animations on the avatar |
| 124 |
defined as a single TCastleScene. |
| 125 |
Alternatively, assign @link(AvatarHierarchy), and leave @link(Avatar) as @nil, |
| 126 |
and assign the @link(OnAnimation) event to do whatever is necessary |
| 127 |
to visualize proper animation of the avatar -- this makes sense esp. |
| 128 |
if your avatar is composed from multiple TCastleScene instances. |
| 129 |
You have to assign at least one of @link(Avatar) or @link(AvatarHierarchy), |
| 130 |
otherwise this navigation doesn't affect anything. |
| 131 |
and you only need to assign one of them for the navigation component to do the work. |
| 132 |
|
| 133 |
Call @link(Init) once the parameters that determine initial camera location |
| 134 |
are all set. |
| 135 |
|
| 136 |
Turn on @link(TCastleMouseLookNavigation.MouseLook MouseLook) to allow user to move |
| 137 |
the mouse to orbit with the camera around the avatar. |
| 138 |
When AimAvatar is aaNone (default), it allows to look at the avatar easily |
| 139 |
from any side (e.g. you can then see avatar's face easily). |
| 140 |
When @link(AimAvatar) is aaHorizontal or aaFlying, rotating allows to point |
| 141 |
the avatar at the appropriate direction. |
| 142 |
|
| 143 |
Using keys AWSD and arrows you can move and rotate the avatar, |
| 144 |
and the camera will follow. |
| 145 |
|
| 146 |
Using the mouse wheel you can get closer / further to the avatar. |
| 147 |
|
| 148 |
The implementation relies on the @link(TCastleTransform.Direction) |
| 149 |
and @link(TCastleTransform.Up) vectors being useful, i.e. they should |
| 150 |
point in the direction of the avatar and up. |
| 151 |
This works out-of-the-box if your model orientation follows glTF standard, |
| 152 |
up in +Y and direction in +Z (see DefaultOrientation, otUpYDirectionZ). |
| 153 |
Customize @link(TCastleTransform.Orientation) to make these vectors |
| 154 |
work if your avatar has non-standard orientation. |
| 155 |
|
| 156 |
The navigation will honor @url(https://castle-engine.io/physics physics) |
| 157 |
if you have configured rigid body and collider components on the avatar. |
| 158 |
Otherwise, we use "old simple physics" documented on |
| 159 |
https://castle-engine.io/physics#_old_system_for_collisions_and_gravity . |
| 160 |
Right now, the "old simple physics" may be actually simpler to control. |
| 161 |
To make them work: |
| 162 |
|
| 163 |
@unorderedList( |
| 164 |
@item(Do not attach TCastleRigidBody and TCastleCollider to the avatar.) |
| 165 |
@item(Set @link(TCastleSceneCore.PreciseCollisions) to @true on level scenes.) |
| 166 |
@item(Set @link(TCastleTransform.MiddleHeight) and |
| 167 |
@link(TCastleTransform.CollisionSphereRadius) to indicate |
| 168 |
sphere around the avatar to resolve collisions. |
| 169 |
Use @link(TDebugTransform) to visualize this.) |
| 170 |
@item(SeT @link(TCastleTransform.GrowSpeed) and |
| 171 |
@link(TCastleTransform.FallSpeed) to allow avatar |
| 172 |
to fall down and climb stairs / hills.) |
| 173 |
) |
| 174 |
|
| 175 |
This is an example @code(Start) view method implementation |
| 176 |
to setup the navigation: |
| 177 |
|
| 178 |
@longCode(# |
| 179 |
procedure TViewMain.Start; |
| 180 |
var |
| 181 |
DebugAvatar: TDebugTransform; |
| 182 |
begin |
| 183 |
inherited; |
| 184 |
|
| 185 |
{ Uncomment and adjust if your avatar has different direction / up |
| 186 |
than standard in glTF. |
| 187 |
This is critical to make camera orbiting around and movement of avatar |
| 188 |
to follow proper direction and up. } |
| 189 |
// AvatarTransform.Orientation := ot...; |
| 190 |
|
| 191 |
ThirdPersonNavigation.MouseLook := true; |
| 192 |
|
| 193 |
{ Configure parameters to move nicely using old simple physics, |
| 194 |
see examples/third_person_navigation for comments. |
| 195 |
Use these if you decide to move using "direct" method |
| 196 |
(when AvatarTransform.ChangeTransform = ctDirect, |
| 197 |
or when AvatarTransform.ChangeTransform = ctAuto and |
| 198 |
AvatarTransform has no rigid body and collider). } |
| 199 |
AvatarTransform.MiddleHeight := 0.9; |
| 200 |
AvatarTransform.CollisionSphereRadius := 0.5; |
| 201 |
AvatarTransform.GrowSpeed := 10.0; |
| 202 |
AvatarTransform.FallSpeed := 10.0; |
| 203 |
|
| 204 |
ThirdPersonNavigation.Init; |
| 205 |
|
| 206 |
DebugAvatar := TDebugTransform.Create(FreeAtStop); |
| 207 |
DebugAvatar.Parent := AvatarTransform; |
| 208 |
DebugAvatar.Exists := true; |
| 209 |
end; |
| 210 |
#) |
| 211 |
|
| 212 |
See also the news post with demo movie about this component: |
| 213 |
https://castle-engine.io/wp/2020/06/29/third-person-navigation-with-avatar-component-in-castle-game-engine/ |
| 214 |
*) |
| 215 |
TCastleThirdPersonNavigation = class(TCastleMouseLookNavigation) |
| 216 |
strict private |
| 217 |
FAvatar: TCastleScene; |
| 218 |
FAvatarHierarchy: TCastleTransform; |
| 219 |
FAvatarRotationSpeed: Single; |
| 220 |
FInitialHeightAboveTarget: Single; |
| 221 |
FDistanceToAvatarTarget: Single; |
| 222 |
FAimAvatar: TAimAvatar; |
| 223 |
FAvatarTarget: TVector3; |
| 224 |
FCameraSpeed: Single; |
| 225 |
{$ifdef AVATAR_TARGET_FORWARD} |
| 226 |
FAvatarTargetForward: TVector3; |
| 227 |
{$endif} |
| 228 |
FMoveSpeed, FCrouchSpeed, FRunSpeed, FJumpSpeed: Single; |
| 229 |
FRotationSpeed: Single; |
| 230 |
FInput_Forward: TInputShortcut; |
| 231 |
FInput_Backward: TInputShortcut; |
| 232 |
FInput_RightRotate: TInputShortcut; |
| 233 |
FInput_LeftRotate: TInputShortcut; |
| 234 |
FInput_RightStrafe: TInputShortcut; |
| 235 |
FInput_LeftStrafe: TInputShortcut; |
| 236 |
FInput_Crouch: TInputShortcut; |
| 237 |
FInput_Run: TInputShortcut; |
| 238 |
FInput_Jump: TInputShortcut; |
| 239 |
FCameraDistanceChangeSpeed: Single; |
| 240 |
FMinDistanceToAvatarTarget: Single; |
| 241 |
FMaxDistanceToAvatarTarget: Single; |
| 242 |
FImmediatelyFixBlockedCamera: Boolean; |
| 243 |
FAnimationIdle: String; |
| 244 |
FAnimationRotate: String; |
| 245 |
FAnimationWalk: String; |
| 246 |
FAnimationRun: String; |
| 247 |
FAnimationCrouch: String; |
| 248 |
FAnimationCrouchIdle: String; |
| 249 |
FAnimationCrouchRotate: String; |
| 250 |
FAnimationJump: String; |
| 251 |
FAnimationFall: String; |
| 252 |
FCameraFollows: Boolean; |
| 253 |
FAvatarFreeObserver: TFreeNotificationObserver; |
| 254 |
FAvatarHierarchyFreeObserver: TFreeNotificationObserver; |
| 255 |
SetAnimationWarningsDone: Cardinal; |
| 256 |
FChangeTransformation: TChangeTransformation; |
| 257 |
FWasJumpInput: Boolean; |
| 258 |
{ Zero we can't control avatar in air, one we have full control } |
| 259 |
FAirMovementControl: Single; |
| 260 |
FAirRotationControl: Single; |
| 261 |
WarningDonePhysicsNotNecessary, |
| 262 |
WarningDoneRigidBodyNecessary, |
| 263 |
WarningDoneColliderNecessary: Boolean; |
| 264 |
FOnAnimation: TCastleThirdPersonNavigationAnimationEvent; |
| 265 |
function RealAvatarHierarchy: TCastleTransform; |
| 266 |
procedure SetAvatar(const Value: TCastleScene); |
| 267 |
procedure SetAvatarHierarchy(const Value: TCastleTransform); |
| 268 |
function CameraPositionInitial(const A: TCastleTransform): TVector3; overload; |
| 269 |
{ Get (returns) initial camera position to look at avatar, |
| 270 |
and get (as TargetWorldPos) target look position. |
| 271 |
Note: TargetWorldPos may be equal to result of CameraPositionInitial |
| 272 |
in the extreme cases. } |
| 273 |
function CameraPositionInitial(const A: TCastleTransform; out TargetWorldPos: TVector3): TVector3; overload; |
| 274 |
{ Returns MaxSingle if no limit. |
| 275 |
Note that CameraDir doesn't have to be normalized. } |
| 276 |
function CameraMaxDistanceToTarget(const A: TCastleTransform; const CameraLookPos: TVector3; |
| 277 |
const CameraDir: TVector3): Single; |
| 278 |
{ Update camera, to avoid having something collidable between camera position and AvatarTarget. |
| 279 |
Note that CameraDir doesn't have to be normalized. } |
| 280 |
procedure FixCameraForCollisions(var CameraPos: TVector3; const CameraDir: TVector3); |
| 281 |
{ Return V rotated such that it is |
| 282 |
orthogonal to GravUp. This way it returns V projected |
| 283 |
on the gravity horizontal plane. |
| 284 |
Result retains the V length (so it is always normalized if V is normalized). |
| 285 |
|
| 286 |
Note that when V and GravUp are parallel, |
| 287 |
this just returns current V --- because in such case |
| 288 |
we can't project V on the horizontal plane. } |
| 289 |
function ToGravityPlane(const V: TVector3; const GravUp: TVector3): TVector3; |
| 290 |
function AnimationIdleStored: Boolean; |
| 291 |
function AnimationRotateStored: Boolean; |
| 292 |
function AnimationWalkStored: Boolean; |
| 293 |
function AnimationRunStored: Boolean; |
| 294 |
function AnimationCrouchStored: Boolean; |
| 295 |
function AnimationCrouchIdleStored: Boolean; |
| 296 |
function AnimationCrouchRotateStored: Boolean; |
| 297 |
function AnimationJumpStored: Boolean; |
| 298 |
function AnimationFallStored: Boolean; |
| 299 |
procedure SetInitialHeightAboveTarget(const Value: Single); |
| 300 |
procedure SetDistanceToAvatarTarget(const Value: Single); |
| 301 |
procedure MySetAvatarTargetForPersistent(const AValue: TVector3); |
| 302 |
procedure SetCameraFollows(const Value: Boolean); |
| 303 |
procedure AvatarFreeNotification(const Sender: TFreeNotificationObserver); |
| 304 |
procedure AvatarHierarchyFreeNotification(const Sender: TFreeNotificationObserver); |
| 305 |
function MovementControlFactor(const AvatarOnGround: Boolean): Single; |
| 306 |
function RotationControlFactor(const AvatarOnGround: Boolean): Single; |
| 307 |
protected |
| 308 |
procedure ProcessMouseLookDelta(const Delta: TVector2); override; |
| 309 |
function Zoom(const Factor: Single): Boolean; override; |
| 310 |
|
| 311 |
{ Make avatar play given animation. |
| 312 |
|
| 313 |
The desired animation is specified as a list of strings, |
| 314 |
from the most preferred name of the animation to the least preferred. |
| 315 |
This allows to perform a "fallback" mechanism in case some animations |
| 316 |
are missing on the model. |
| 317 |
For example if AnimationCrouch is not available, |
| 318 |
we will use AnimationCrouchIdle instead, |
| 319 |
and if that is also not available we will use AnimationIdle. |
| 320 |
Some basic animations must still exist -- if even AnimationIdle |
| 321 |
doesn't exist, we'll just show a warning. |
| 322 |
|
| 323 |
The default implementation in @className changes |
| 324 |
@link(TCastleSceneCore.AutoAnimation Avatar.AutoAnimation) method. |
| 325 |
It does nothing if @link(Avatar) is @nil. |
| 326 |
|
| 327 |
Moreover the default implementation implements a "fallback" mechanism |
| 328 |
in case some animations are not available in the scene. |
| 329 |
This is checked using @link(TCastleSceneCore.HasAnimation Avatar.HasAnimation). |
| 330 |
|
| 331 |
To apply animations in custom way, either override this method, |
| 332 |
or assign event to @link(OnAnimation). |
| 333 |
E.g. you can animate a hierarchy of scenes composed from MD3 |
| 334 |
pieces. When overriding this, you don't need to call @code(inherited). |
| 335 |
If you override this to apply the animation to something else than @link(Avatar), |
| 336 |
then it may even be reasonable to leave @link(Avatar) as @nil, |
| 337 |
and only set @link(AvatarHierarchy). |
| 338 |
|
| 339 |
The implementation of this method (after performing the "fallback" mechanism |
| 340 |
described above to find the real name on the AnimationName list) |
| 341 |
should check whether the object is not @italic(already playing the same animation). |
| 342 |
This is important to avoid unnecessary animation restarts. } |
| 343 |
procedure SetAnimation(const AnimationNames: array of String); virtual; |
| 344 |
public |
| 345 |
const |
| 346 |
DefaultInitialHeightAboveTarget = 1.0; |
| 347 |
DefaultDistanceToAvatarTarget = 4.0; |
| 348 |
DefaultAvatarRotationSpeed = 10; |
| 349 |
DefaultAvatarTarget: TVector3 = (X: 0; Y: 2; Z: 0); |
| 350 |
DefaultCameraSpeed = 10; |
| 351 |
{$ifdef AVATAR_TARGET_FORWARD} |
| 352 |
DefaultAvatarTargetForward: TVector3 = (X: 0; Y: 2; Z: 0); |
| 353 |
{$endif} |
| 354 |
DefaultMoveSpeed = 1.0; |
| 355 |
DefaultCrouchSpeed = 0.5; |
| 356 |
DefaultRunSpeed = 2.0; |
| 357 |
DefaultJumpSpeed = 5.0; |
| 358 |
DefaultRotationSpeed = Pi * 150 / 180; |
| 359 |
DefaultCameraDistanceChangeSpeed = 1; |
| 360 |
DefaultMinDistanceToAvatarTarget = 0.5; |
| 361 |
DefaultMaxDistanceToAvatarTarget = 10; |
| 362 |
DefaultAnimationIdle = 'idle'; |
| 363 |
DefaultAnimationRotate = 'rotate'; |
| 364 |
DefaultAnimationWalk = 'walk'; |
| 365 |
DefaultAnimationRun = 'run'; |
| 366 |
DefaultAnimationCrouch = 'crouch'; |
| 367 |
DefaultAnimationCrouchIdle = 'crouch_idle'; |
| 368 |
DefaultAnimationCrouchRotate = 'crouch_rotate'; |
| 369 |
DefaultAnimationJump = 'jump'; |
| 370 |
DefaultAnimationFall = 'fall'; |
| 371 |
DefaultAirMovementControl = 0.5; |
| 372 |
DefaultAirRotationControl = 0.5; |
| 373 |
|
| 374 |
constructor Create(AOwner: TComponent); override; |
| 375 |
destructor Destroy; override; |
| 376 |
procedure Update(const SecondsPassed: Single; |
| 377 |
var HandleInput: Boolean); override; |
| 378 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 379 |
|
| 380 |
{ Makes camera be positioned with respect to the current properties and avatar. |
| 381 |
Always call this explicitly once. |
| 382 |
Use this after setting properties like @link(Avatar), |
| 383 |
@link(AvatarHierarchy), @link(DistanceToAvatarTarget), |
| 384 |
@link(InitialHeightAboveTarget). |
| 385 |
|
| 386 |
At design-time (in CGE editor), this is automatically called after |
| 387 |
changing relevant properties of this navigation. } |
| 388 |
procedure Init; |
| 389 |
|
| 390 |
{ Translation, from the avatar origin, to the "target" of the avatar where camera |
| 391 |
looks at. This is usually head, and this vector should just describe the height |
| 392 |
of head above the ground. |
| 393 |
By default this is DefaultAvatarTarget = (0, 2, 0). } |
| 394 |
property AvatarTarget: TVector3 read FAvatarTarget write FAvatarTarget; |
| 395 |
|
| 396 |
{$ifdef AVATAR_TARGET_FORWARD} |
| 397 |
{ When the camera looks directly behind the avatar's back, |
| 398 |
it points at AvatarTargetForward, not AvatarTarget. |
| 399 |
This allows to place AvatarTargetForward more in forward (and maybe higher/lower) |
| 400 |
than avatar's head. |
| 401 |
This allows to look more "ahead". |
| 402 |
|
| 403 |
The effective target is a result of lerp between |
| 404 |
AvatarTargetForward and AvatarTarget, depending on how much is camera now close to |
| 405 |
the initial position "looking from the back of avatar". |
| 406 |
|
| 407 |
The camera is still always rotating around AvatarTarget |
| 408 |
(so you rotate around avatar's head, even if you look far ahead). |
| 409 |
By default this is DefaultAvatarTargetForward = (0, 2, 0). |
| 410 |
|
| 411 |
TODO: Not implemented now. I made initial implementation using Lerp, |
| 412 |
and it just works awful -- camera rotates unexpectedly, the user feels |
| 413 |
losing control of the camera at certain angles. |
| 414 |
} |
| 415 |
property AvatarTargetForward: TVector3 read FAvatarTargetForward write FAvatarTargetForward; |
| 416 |
{$endif} |
| 417 |
|
| 418 |
property Input_Forward: TInputShortcut read FInput_Forward; |
| 419 |
property Input_Backward: TInputShortcut read FInput_Backward; |
| 420 |
property Input_LeftRotate: TInputShortcut read FInput_LeftRotate; |
| 421 |
property Input_RightRotate: TInputShortcut read FInput_RightRotate; |
| 422 |
property Input_LeftStrafe: TInputShortcut read FInput_LeftStrafe; |
| 423 |
property Input_RightStrafe: TInputShortcut read FInput_RightStrafe; |
| 424 |
property Input_Crouch: TInputShortcut read FInput_Crouch; |
| 425 |
property Input_Run: TInputShortcut read FInput_Run; |
| 426 |
property Input_Jump: TInputShortcut read FInput_Jump; |
| 427 |
|
| 428 |
function Input_CameraCloser: TInputShortcut; deprecated 'use Input_ZoomIn'; |
| 429 |
function Input_CameraFurther: TInputShortcut; deprecated 'use Input_ZoomOut'; |
| 430 |
published |
| 431 |
{ Zooming in this navigation mode makes camera move closer/further from avatar. } |
| 432 |
property ZoomEnabled default true; |
| 433 |
|
| 434 |
property MouseLookHorizontalSensitivity; |
| 435 |
property MouseLookVerticalSensitivity; |
| 436 |
property InvertVerticalMouseLook; |
| 437 |
|
| 438 |
{ Does camera follow the avatar, by default yes. |
| 439 |
|
| 440 |
When this is @false, camera remains unchanged by anything here |
| 441 |
(avatar movement/rotations, mouse look, even by calling @link(Init)). |
| 442 |
Some properties of this then are meaningless (e.g. @link(DistanceToAvatarTarget)). |
| 443 |
|
| 444 |
However, all the inputs to control the avatar continue to work. } |
| 445 |
property CameraFollows: Boolean read FCameraFollows write SetCameraFollows default true; |
| 446 |
|
| 447 |
{ Avatar scene, that is animated, moved and rotated when this navigation changes. |
| 448 |
This navigation component will just call @code(Avatar.AutoAnimation := 'xxx') |
| 449 |
when necessary. |
| 450 |
|
| 451 |
We will use animation names configured using properties like |
| 452 |
@link(AnimationWalk), @link(AnimationIdle) and other `AnimationXxx`. |
| 453 |
By default they are just equal to simple names @code('walk'), @code('idle') |
| 454 |
and such, but you can change @link(AnimationWalk), @link(AnimationIdle) |
| 455 |
if your model exposes different animation names. |
| 456 |
|
| 457 |
When AvatarHierarchy is @nil, then @name is directly moved and rotated |
| 458 |
to move avatar. |
| 459 |
|
| 460 |
Otherwise (when AvatarHierarchy is assigned), |
| 461 |
then AvatarHierarchy is moved, and @name should be inside AvatarHierarchy. |
| 462 |
In this case, the only purpose of @name is to let navigation |
| 463 |
automatically adjust animations. But you can leave @name unassigned, |
| 464 |
and override @link(SetAnimation) to do this. |
| 465 |
|
| 466 |
This scene assigned here should be part of @link(TCastleViewport.Items) |
| 467 |
to make this navigation work, in particular when you call @link(Init). } |
| 468 |
property Avatar: TCastleScene read FAvatar write SetAvatar; |
| 469 |
|
| 470 |
{ Optional avatar hierarchy that is moved and rotated when this navigation changes. |
| 471 |
When this is @nil, we just move and rotate the @link(Avatar). |
| 472 |
When this is non-nil, then we only move and rotate this AvatarHierarchy. |
| 473 |
|
| 474 |
If @link(AvatarHierarchy) is non-nil, then it should contain |
| 475 |
@link(Avatar) (if it is assigned) |
| 476 |
as a child. @link(AvatarHierarchy) can even be equal to @link(Avatar) |
| 477 |
(it is equivalent to just leaving @link(AvatarHierarchy) as @nil). |
| 478 |
|
| 479 |
This object should be part of @link(TCastleViewport.Items) |
| 480 |
to make this navigation work, in particular when you call @link(Init). } |
| 481 |
property AvatarHierarchy: TCastleTransform read FAvatarHierarchy write SetAvatarHierarchy; |
| 482 |
|
| 483 |
{ When @link(AimAvatar), this is avatar's rotation speed (in radians per second). |
| 484 |
Should make avatar rotation "catch up" (with some delay after camera rotation. } |
| 485 |
property AvatarRotationSpeed: Single read FAvatarRotationSpeed write FAvatarRotationSpeed |
| 486 |
{$ifdef FPC}default DefaultAvatarRotationSpeed{$endif}; |
| 487 |
|
| 488 |
{ Camera position tracks the desired position with given speed (in units per second). |
| 489 |
This makes camera adjust to avatar moving (because of input, or because of gravity |
| 490 |
or other external code) and to not being blocked by the collider. } |
| 491 |
property CameraSpeed: Single read FCameraSpeed write FCameraSpeed |
| 492 |
{$ifdef FPC}default DefaultCameraSpeed{$endif}; |
| 493 |
|
| 494 |
{ If not aaNone then rotating the camera also rotates (with some delay) the avatar, |
| 495 |
to face the same direction as the camera. |
| 496 |
This allows to rotate the avatar with mouse look (which is comfortable), |
| 497 |
on the other hand it takes away some flexibility, |
| 498 |
e.g. you cannot look at avatar's face for a long time anymore. } |
| 499 |
property AimAvatar: TAimAvatar read FAimAvatar write FAimAvatar default aaNone; |
| 500 |
|
| 501 |
{ Initial height of camera above the AvatarTarget. |
| 502 |
Together with DistanceToAvatarTarget this determines the initial camera position, |
| 503 |
set by @link(Init). |
| 504 |
It is not used outside of @link(Init). } |
| 505 |
property InitialHeightAboveTarget: Single read FInitialHeightAboveTarget write SetInitialHeightAboveTarget |
| 506 |
{$ifdef FPC}default DefaultInitialHeightAboveTarget{$endif}; |
| 507 |
|
| 508 |
{ Immediately (not with delay of CameraSpeed) update camera to never block avatar |
| 509 |
view by a wall, enemy etc. When it is @true, we avoid seeing an invalid geometry |
| 510 |
(e.g. from the wrong side of the wall or inside a creature) @italic(ever), |
| 511 |
but in exchange the camera sometimes has to be adjusted very abrtupty (testcase: |
| 512 |
third_person_navigation demo, stand in the middle of moving enemies, and look around). } |
| 513 |
property ImmediatelyFixBlockedCamera: Boolean read FImmediatelyFixBlockedCamera write FImmediatelyFixBlockedCamera |
| 514 |
default false; |
| 515 |
|
| 516 |
{ Preferred distance from camera to the avatar target (head). |
| 517 |
User can change it with Input_ZoomIn, Input_ZoomOut if you set these inputs |
| 518 |
to some key/mouse button/mouse wheel. } |
| 519 |
property DistanceToAvatarTarget: Single read FDistanceToAvatarTarget write SetDistanceToAvatarTarget |
| 520 |
{$ifdef FPC}default DefaultDistanceToAvatarTarget{$endif}; |
| 521 |
{ Speed with which Input_ZoomIn, Input_ZoomOut can change DistanceToAvatarTarget. } |
| 522 |
property CameraDistanceChangeSpeed: Single read FCameraDistanceChangeSpeed write FCameraDistanceChangeSpeed |
| 523 |
{$ifdef FPC}default DefaultCameraDistanceChangeSpeed{$endif}; |
| 524 |
{ Limit of the distance to avatar, used when changing DistanceToAvatarTarget, |
| 525 |
and also when deciding how to adjust camera to avoid collisions. |
| 526 |
@groupBegin } |
| 527 |
property MinDistanceToAvatarTarget: Single read FMinDistanceToAvatarTarget write FMinDistanceToAvatarTarget |
| 528 |
{$ifdef FPC}default DefaultMinDistanceToAvatarTarget{$endif}; |
| 529 |
property MaxDistanceToAvatarTarget: Single read FMaxDistanceToAvatarTarget write FMaxDistanceToAvatarTarget |
| 530 |
{$ifdef FPC}default DefaultMaxDistanceToAvatarTarget{$endif}; |
| 531 |
{ @groupEnd } |
| 532 |
|
| 533 |
{ Speed of movement by keys. } |
| 534 |
property MoveSpeed: Single read FMoveSpeed write FMoveSpeed |
| 535 |
{$ifdef FPC}default DefaultMoveSpeed{$endif}; |
| 536 |
{ Speed of movement by keys, when crouching. } |
| 537 |
property CrouchSpeed: Single read FCrouchSpeed write FCrouchSpeed |
| 538 |
{$ifdef FPC}default DefaultCrouchSpeed{$endif}; |
| 539 |
{ Speed of movement by keys, when running. } |
| 540 |
property RunSpeed: Single read FRunSpeed write FRunSpeed |
| 541 |
{$ifdef FPC}default DefaultRunSpeed{$endif}; |
| 542 |
{ Speed of jump by keys. } |
| 543 |
property JumpSpeed: Single read FJumpSpeed write FJumpSpeed |
| 544 |
{$ifdef FPC}default DefaultJumpSpeed{$endif}; |
| 545 |
{ Speed of rotating by keys, in radians per second. } |
| 546 |
property RotationSpeed: Single read FRotationSpeed write FRotationSpeed |
| 547 |
{$ifdef FPC}default DefaultRotationSpeed{$endif}; |
| 548 |
|
| 549 |
{ Should we have control on the avatar movement in the air. Must be >= 0. |
| 550 |
|
| 551 |
@unorderedList( |
| 552 |
@item(0 -> no control in the air) |
| 553 |
@item(1 -> full control in the air, just like on the ground) |
| 554 |
@item(between 0 and 1 -> limited control, smoothly changes between no control and full control) |
| 555 |
@item(above 1 -> in the air you move even faster than on the ground) |
| 556 |
) |
| 557 |
} |
| 558 |
property AirMovementControl: Single read FAirMovementControl write FAirMovementControl |
| 559 |
{$ifdef FPC}default DefaultAirMovementControl{$endif}; |
| 560 |
|
| 561 |
{ Should we have control on the avatar rotation in the air. |
| 562 |
|
| 563 |
@unorderedList( |
| 564 |
@item(0 -> no control in the air) |
| 565 |
@item(1 -> full control in the air, just like on the ground) |
| 566 |
@item(between 0 and 1 -> limited control, smoothly changes between no control and full control) |
| 567 |
@item(above 1 -> in the air you rotate even faster than on the ground) |
| 568 |
) |
| 569 |
} |
| 570 |
property AirRotationControl: Single read FAirRotationControl write FAirRotationControl |
| 571 |
{$ifdef FPC}default DefaultAirRotationControl{$endif}; |
| 572 |
|
| 573 |
{ Animation when character is not moving, not rotating and not crouching. |
| 574 |
Default 'idle'. } |
| 575 |
property AnimationIdle: String read FAnimationIdle write FAnimationIdle stored AnimationIdleStored nodefault; |
| 576 |
{ Animation when character is rotating, but otherwise remains in place |
| 577 |
(not moving) and it is not crouching. |
| 578 |
Default 'rotate'. } |
| 579 |
property AnimationRotate: String read FAnimationRotate write FAnimationRotate stored AnimationRotateStored nodefault; |
| 580 |
{ Animation when character is walking. |
| 581 |
Default 'walk'. } |
| 582 |
property AnimationWalk: String read FAnimationWalk write FAnimationWalk stored AnimationWalkStored nodefault; |
| 583 |
{ Animation when character is running. |
| 584 |
Default 'run'. } |
| 585 |
property AnimationRun: String read FAnimationRun write FAnimationRun stored AnimationRunStored nodefault; |
| 586 |
{ Animation when character is moving while crouching. |
| 587 |
Default 'crouch'. } |
| 588 |
property AnimationCrouch: String read FAnimationCrouch write FAnimationCrouch stored AnimationCrouchStored nodefault; |
| 589 |
{ Animation when character is crouching (Input_Crouch is pressed) but not moving or rotating. |
| 590 |
Default 'crouch_idle'. } |
| 591 |
property AnimationCrouchIdle: String read FAnimationCrouchIdle write FAnimationCrouchIdle stored AnimationCrouchIdleStored nodefault; |
| 592 |
{ Animation when character is crouching (Input_Crouch is pressed) and rotating, but not moving. |
| 593 |
Default 'crouch_rotate'.} |
| 594 |
property AnimationCrouchRotate: String read FAnimationCrouchRotate write FAnimationCrouchRotate stored AnimationCrouchRotateStored nodefault; |
| 595 |
{ Animation when character is jumping (Input_Jump is pressed). |
| 596 |
Default 'jump'.} |
| 597 |
property AnimationJump: String read FAnimationJump write FAnimationJump stored AnimationJumpStored nodefault; |
| 598 |
{ Animation when character is fall. |
| 599 |
Default 'fall'.} |
| 600 |
property AnimationFall: String read FAnimationFall write FAnimationFall stored AnimationFallStored nodefault; |
| 601 |
|
| 602 |
{ Camera will keep at least this distance from walls. } |
| 603 |
property Radius; |
| 604 |
|
| 605 |
{ How does the avatar change transformation (for movement and rotations). |
| 606 |
This determines whether we update @link(TCastleTransform.Translation), |
| 607 |
@link(TCastleTransform.Rotation) directly or use physics (TCastleRigidBody) |
| 608 |
velocities or forces. |
| 609 |
|
| 610 |
See TChangeTransformation for possible values are their meaning. |
| 611 |
|
| 612 |
By default, this is ctAuto, which means that we detect whether you have |
| 613 |
physics behaviors (TCastleRigidBody, TCastleCollider, with TCastleRigidBody.Exists) |
| 614 |
set up on the avatar. |
| 615 |
|
| 616 |
@unorderedList( |
| 617 |
@item(If yes, we will use physics behaviors, and change transformation |
| 618 |
using the velocity of TCastleRigidBody.) |
| 619 |
@item(Otherwise (if you don't have physics behaviors), we will directly change |
| 620 |
@link(TCastleTransform.Translation), @link(TCastleTransform.Rotation).) |
| 621 |
) |
| 622 |
} |
| 623 |
property ChangeTransformation: TChangeTransformation read FChangeTransformation write FChangeTransformation |
| 624 |
default ctAuto; |
| 625 |
|
| 626 |
{ Update the animation displayed by the current avatar. |
| 627 |
|
| 628 |
See @link(SetAnimation) for details. |
| 629 |
|
| 630 |
Assigning event to this makes sense if your avatar is composed from |
| 631 |
multiple TCastleScene instances. |
| 632 |
This may be called very often, to update animation every frame, |
| 633 |
so do call @code(SubScene.PlayAnimation) from here. |
| 634 |
Rather, set @code(SubScene.AutoAnimation) to not restart the animation |
| 635 |
if it's already running. For example: |
| 636 |
|
| 637 |
@longCode(# |
| 638 |
procedure TViewMain.NavigationSetAnimation(const Sender: TCastleThirdPersonNavigation; |
| 639 |
const AnimationNames: array of String); |
| 640 |
begin |
| 641 |
if AnimationNames[0] = 'idle' then |
| 642 |
begin |
| 643 |
SceneLegs.AutoAnimation := 'legs_idle'; |
| 644 |
SceneTorso.AutoAnimation := 'torso_idle'; |
| 645 |
end else |
| 646 |
begin |
| 647 |
SceneLegs.AutoAnimation := 'legs_walk'; |
| 648 |
SceneTorso.AutoAnimation := 'torso_walk'; |
| 649 |
end' |
| 650 |
end; |
| 651 |
#) |
| 652 |
} |
| 653 |
property OnAnimation: TCastleThirdPersonNavigationAnimationEvent |
| 654 |
read FOnAnimation write FOnAnimation; |
| 655 |
|
| 656 |
{$define read_interface_class} |
| 657 |
{$I auto_generated_persistent_vectors/tcastlethirdpersonnavigation_persistent_vectors.inc} |
| 658 |
{$undef read_interface_class} |
| 659 |
end; |
| 660 |
|
| 661 |
implementation |
| 662 |
|
| 663 |
uses Math, |
| 664 |
CastleUtils, CastleStringUtils, CastleComponentSerialize, CastleLog, CastleBoxes; |
| 665 |
|
| 666 |
{ TCastleThirdPersonNavigation ----------------------------------------------- } |
| 667 |
|
| 668 |
constructor TCastleThirdPersonNavigation.Create(AOwner: TComponent); |
| 669 |
begin |
| 670 |
inherited; |
| 671 |
FCameraFollows := true; |
| 672 |
FAvatarTarget := DefaultAvatarTarget; |
| 673 |
{$ifdef AVATAR_TARGET_FORWARD} |
| 674 |
FAvatarTargetForward := DefaultAvatarTargetForward; |
| 675 |
{$endif} |
| 676 |
FAvatarRotationSpeed := DefaultAvatarRotationSpeed; |
| 677 |
FAimAvatar := aaNone; |
| 678 |
FCameraSpeed := DefaultCameraSpeed; |
| 679 |
FInitialHeightAboveTarget := DefaultInitialHeightAboveTarget; |
| 680 |
FDistanceToAvatarTarget := DefaultDistanceToAvatarTarget; |
| 681 |
FMoveSpeed := DefaultMoveSpeed; |
| 682 |
FCrouchSpeed := DefaultCrouchSpeed; |
| 683 |
FRunSpeed := DefaultRunSpeed; |
| 684 |
FJumpSpeed := DefaultJumpSpeed; |
| 685 |
FRotationSpeed := DefaultRotationSpeed; |
| 686 |
FCameraDistanceChangeSpeed := DefaultCameraDistanceChangeSpeed; |
| 687 |
FMinDistanceToAvatarTarget := DefaultMinDistanceToAvatarTarget; |
| 688 |
FMaxDistanceToAvatarTarget := DefaultMaxDistanceToAvatarTarget; |
| 689 |
FAnimationIdle := DefaultAnimationIdle; |
| 690 |
FAnimationRotate := DefaultAnimationRotate; |
| 691 |
FAnimationWalk := DefaultAnimationWalk; |
| 692 |
FAnimationRun := DefaultAnimationRun; |
| 693 |
FAnimationCrouch := DefaultAnimationCrouch; |
| 694 |
FAnimationCrouchIdle := DefaultAnimationCrouchIdle; |
| 695 |
FAnimationCrouchRotate := DefaultAnimationCrouchRotate; |
| 696 |
FAnimationJump := DefaultAnimationJump; |
| 697 |
FAnimationFall := DefaultAnimationFall; |
| 698 |
ZoomEnabled := true; |
| 699 |
|
| 700 |
FAvatarFreeObserver := TFreeNotificationObserver.Create(Self); |
| 701 |
FAvatarFreeObserver.OnFreeNotification := {$ifdef FPC}@{$endif}AvatarFreeNotification; |
| 702 |
FAvatarHierarchyFreeObserver := TFreeNotificationObserver.Create(Self); |
| 703 |
FAvatarHierarchyFreeObserver.OnFreeNotification := {$ifdef FPC}@{$endif}AvatarHierarchyFreeNotification; |
| 704 |
|
| 705 |
FInput_Forward := TInputShortcut.Create(Self); |
| 706 |
FInput_Backward := TInputShortcut.Create(Self); |
| 707 |
FInput_LeftRotate := TInputShortcut.Create(Self); |
| 708 |
FInput_RightRotate := TInputShortcut.Create(Self); |
| 709 |
FInput_LeftStrafe := TInputShortcut.Create(Self); |
| 710 |
FInput_RightStrafe := TInputShortcut.Create(Self); |
| 711 |
FInput_Crouch := TInputShortcut.Create(Self); |
| 712 |
FInput_Run := TInputShortcut.Create(Self); |
| 713 |
FInput_Jump := TInputShortcut.Create(Self); |
| 714 |
|
| 715 |
Input_Forward .Assign(keyW, keyArrowUp); |
| 716 |
Input_Backward .Assign(keyS, keyArrowDown); |
| 717 |
Input_LeftRotate .Assign(keyArrowLeft, keyA); |
| 718 |
Input_RightRotate .Assign(keyArrowRight, keyD); |
| 719 |
Input_LeftStrafe .Assign(keyNone); |
| 720 |
Input_RightStrafe .Assign(keyNone); |
| 721 |
Input_Crouch .Assign(keyCtrl); |
| 722 |
Input_Run .Assign(keyShift); |
| 723 |
Input_Jump .Assign(keySpace); |
| 724 |
|
| 725 |
Input_Forward .SetSubComponent(true); |
| 726 |
Input_Backward .SetSubComponent(true); |
| 727 |
Input_LeftRotate .SetSubComponent(true); |
| 728 |
Input_RightRotate .SetSubComponent(true); |
| 729 |
Input_LeftStrafe .SetSubComponent(true); |
| 730 |
Input_RightStrafe .SetSubComponent(true); |
| 731 |
Input_Crouch .SetSubComponent(true); |
| 732 |
Input_Run .SetSubComponent(true); |
| 733 |
Input_Jump .SetSubComponent(true); |
| 734 |
|
| 735 |
Input_Forward .Name := 'Input_Forward'; |
| 736 |
Input_Backward .Name := 'Input_Backward'; |
| 737 |
Input_LeftRotate .Name := 'Input_LeftRotate'; |
| 738 |
Input_RightRotate .Name := 'Input_RightRotate'; |
| 739 |
Input_LeftStrafe .Name := 'Input_LeftStrafe'; |
| 740 |
Input_RightStrafe .Name := 'Input_RightStrafe'; |
| 741 |
Input_Crouch .Name := 'Input_Crouch'; |
| 742 |
Input_Run .Name := 'Input_Run'; |
| 743 |
Input_Jump .Name := 'Input_Jump'; |
| 744 |
|
| 745 |
{$define read_implementation_constructor} |
| 746 |
{$I auto_generated_persistent_vectors/tcastlethirdpersonnavigation_persistent_vectors.inc} |
| 747 |
{$undef read_implementation_constructor} |
| 748 |
|
| 749 |
// override vector change method, to call Init in design mode when this changes |
| 750 |
AvatarTargetPersistent.InternalSetValue := {$ifdef FPC}@{$endif}MySetAvatarTargetForPersistent; |
| 751 |
|
| 752 |
FChangeTransformation := ctAuto; |
| 753 |
FWasJumpInput := false; |
| 754 |
FAirMovementControl := DefaultAirMovementControl; |
| 755 |
FAirRotationControl := DefaultAirRotationControl; |
| 756 |
end; |
| 757 |
|
| 758 |
procedure TCastleThirdPersonNavigation.MySetAvatarTargetForPersistent(const AValue: TVector3); |
| 759 |
begin |
| 760 |
SetAvatarTargetForPersistent(AValue); |
| 761 |
end; |
| 762 |
|
| 763 |
destructor TCastleThirdPersonNavigation.Destroy; |
| 764 |
begin |
| 765 |
{ set to nil by SetXxx, to detach free notification } |
| 766 |
Avatar := nil; |
| 767 |
AvatarHierarchy := nil; |
| 768 |
|
| 769 |
{$define read_implementation_destructor} |
| 770 |
{$I auto_generated_persistent_vectors/tcastlethirdpersonnavigation_persistent_vectors.inc} |
| 771 |
{$undef read_implementation_destructor} |
| 772 |
inherited; |
| 773 |
end; |
| 774 |
|
| 775 |
function TCastleThirdPersonNavigation.RealAvatarHierarchy: TCastleTransform; |
| 776 |
begin |
| 777 |
if AvatarHierarchy <> nil then |
| 778 |
Result := AvatarHierarchy |
| 779 |
else |
| 780 |
Result := Avatar; |
| 781 |
end; |
| 782 |
|
| 783 |
procedure TCastleThirdPersonNavigation.SetAvatar(const Value: TCastleScene); |
| 784 |
begin |
| 785 |
if FAvatar <> Value then |
| 786 |
begin |
| 787 |
FAvatar := Value; |
| 788 |
FAvatarFreeObserver.Observed := Value; |
| 789 |
SetAnimationWarningsDone := 0; |
| 790 |
end; |
| 791 |
end; |
| 792 |
|
| 793 |
procedure TCastleThirdPersonNavigation.SetAvatarHierarchy(const Value: TCastleTransform); |
| 794 |
begin |
| 795 |
if FAvatarHierarchy <> Value then |
| 796 |
begin |
| 797 |
FAvatarHierarchy := Value; |
| 798 |
FAvatarHierarchyFreeObserver.Observed := Value; |
| 799 |
end; |
| 800 |
end; |
| 801 |
|
| 802 |
function TCastleThirdPersonNavigation.CameraPositionInitial(const A: TCastleTransform): TVector3; |
| 803 |
var |
| 804 |
TargetWorldPos: TVector3; |
| 805 |
begin |
| 806 |
Result := CameraPositionInitial(A, TargetWorldPos); // ignore resulting TargetWorldPos |
| 807 |
end; |
| 808 |
|
| 809 |
function TCastleThirdPersonNavigation.ToGravityPlane(const V: TVector3; const GravUp: TVector3): TVector3; |
| 810 |
begin |
| 811 |
Result := V; |
| 812 |
if not VectorsParallel(Result, GravUp) then |
| 813 |
MakeVectorsOrthoOnTheirPlane(Result, GravUp); |
| 814 |
end; |
| 815 |
|
| 816 |
function TCastleThirdPersonNavigation.CameraPositionInitial(const A: TCastleTransform; out TargetWorldPos: TVector3): TVector3; |
| 817 |
var |
| 818 |
GravUp: TVector3; |
| 819 |
TargetWorldDir: TVector3; |
| 820 |
HorizontalShiftFromTarget: Single; |
| 821 |
begin |
| 822 |
TargetWorldPos := A.WorldTransform.MultPoint(AvatarTarget); |
| 823 |
TargetWorldDir := A.WorldTransform.MultDirection(TCastleTransform.DefaultDirection[A.Orientation]); |
| 824 |
|
| 825 |
if DistanceToAvatarTarget < InitialHeightAboveTarget then |
| 826 |
begin |
| 827 |
WritelnWarning('DistanceToAvatarTarget (%f) should not be smaller than InitialHeightAboveTarget (%f)', [ |
| 828 |
DistanceToAvatarTarget, |
| 829 |
InitialHeightAboveTarget |
| 830 |
]); |
| 831 |
// This effectively assumes that DistanceToAvatarTarget = InitialHeightAboveTarget |
| 832 |
HorizontalShiftFromTarget := 0; |
| 833 |
end else |
| 834 |
begin |
| 835 |
{ InitialHeightAboveTarget, HorizontalShiftFromTarget, DistanceToAvatarTarget |
| 836 |
create a right triangle, so |
| 837 |
InitialHeightAboveTarget^2 + HorizontalShiftFromTarget^2 = DistanceToAvatarTarget^2 |
| 838 |
} |
| 839 |
HorizontalShiftFromTarget := Sqrt(Sqr(DistanceToAvatarTarget) - Sqr(InitialHeightAboveTarget)); |
| 840 |
end; |
| 841 |
|
| 842 |
GravUp := Camera.GravityUp; |
| 843 |
|
| 844 |
Result := TargetWorldPos |
| 845 |
+ GravUp * InitialHeightAboveTarget |
| 846 |
- ToGravityPlane(TargetWorldDir, GravUp) * HorizontalShiftFromTarget; |
| 847 |
end; |
| 848 |
|
| 849 |
procedure TCastleThirdPersonNavigation.FixCameraForCollisions( |
| 850 |
var CameraPos: TVector3; const CameraDir: TVector3); |
| 851 |
var |
| 852 |
MaxDistance: Single; |
| 853 |
A: TCastleTransform; |
| 854 |
TargetWorldPos: TVector3; |
| 855 |
begin |
| 856 |
A := RealAvatarHierarchy; |
| 857 |
if (A <> nil) and (InternalViewport <> nil) then |
| 858 |
begin |
| 859 |
TargetWorldPos := A.WorldTransform.MultPoint(AvatarTarget); |
| 860 |
MaxDistance := CameraMaxDistanceToTarget(A, TargetWorldPos, CameraDir); |
| 861 |
if PointsDistanceSqr(CameraPos, TargetWorldPos) > Sqr(MaxDistance) then |
| 862 |
// Note that CameraDir is not necessarily normalized now |
| 863 |
CameraPos := TargetWorldPos - CameraDir.AdjustToLength(MaxDistance); |
| 864 |
end; |
| 865 |
end; |
| 866 |
|
| 867 |
function TCastleThirdPersonNavigation.CameraMaxDistanceToTarget( |
| 868 |
const A: TCastleTransform; const CameraLookPos: TVector3; |
| 869 |
const CameraDir: TVector3): Single; |
| 870 |
var |
| 871 |
CollisionDistance: Single; |
| 872 |
SavedAPickable: Boolean; |
| 873 |
begin |
| 874 |
Result := MaxSingle; |
| 875 |
SavedAPickable := A.Pickable; |
| 876 |
A.Pickable := false; |
| 877 |
try |
| 878 |
if A.World.WorldRayCast(CameraLookPos, -CameraDir, CollisionDistance) <> nil then |
| 879 |
begin |
| 880 |
{ Use MinDistanceToAvatarTarget to secure in case wall is closer than Radius |
| 881 |
(CollisionDistance - Radius negative) |
| 882 |
or just to close to head. |
| 883 |
Then use MinDistanceToAvatarTarget. } |
| 884 |
Result := Max(MinDistanceToAvatarTarget, CollisionDistance - Radius); |
| 885 |
end; |
| 886 |
finally A.Pickable := SavedAPickable end; |
| 887 |
end; |
| 888 |
|
| 889 |
procedure TCastleThirdPersonNavigation.Init; |
| 890 |
var |
| 891 |
GravUp: TVector3; |
| 892 |
A: TCastleTransform; |
| 893 |
CameraPos, CameraDir, CameraUp, TargetWorldPos: TVector3; |
| 894 |
begin |
| 895 |
A := RealAvatarHierarchy; |
| 896 |
if (A <> nil) and (InternalViewport <> nil) then |
| 897 |
begin |
| 898 |
if CameraFollows then |
| 899 |
begin |
| 900 |
GravUp := Camera.GravityUp; |
| 901 |
|
| 902 |
CameraPos := CameraPositionInitial(A, TargetWorldPos); |
| 903 |
CameraDir := TargetWorldPos - CameraPos; |
| 904 |
if CameraDir.IsZero then |
| 905 |
begin |
| 906 |
{ This condition didn't occur in actual tests, this is paranoid check. } |
| 907 |
WritelnWarning('Increase DistanceToAvatarTarget (%f) and/or InitialHeightAboveTarget (%f) to make initial camera further from target', [ |
| 908 |
DistanceToAvatarTarget, |
| 909 |
InitialHeightAboveTarget |
| 910 |
]); |
| 911 |
CameraDir := TVector3.One[0]; |
| 912 |
end; |
| 913 |
CameraUp := GravUp; // will be adjusted to be orthogonal to Dir by SetView |
| 914 |
FixCameraForCollisions(CameraPos, CameraDir); |
| 915 |
Camera.SetWorldView(CameraPos, CameraDir, CameraUp); |
| 916 |
end; |
| 917 |
|
| 918 |
SetAnimation([AnimationIdle]); |
| 919 |
if Avatar <> nil then |
| 920 |
Avatar.ForceInitialAnimationPose; |
| 921 |
end; |
| 922 |
end; |
| 923 |
|
| 924 |
procedure TCastleThirdPersonNavigation.ProcessMouseLookDelta(const Delta: TVector2); |
| 925 |
var |
| 926 |
ToCamera, GravUp: TVector3; |
| 927 |
A: TCastleTransform; |
| 928 |
|
| 929 |
{ Change ToCamera by applying DeltaY from mouse look. } |
| 930 |
procedure ProcessVertical(DeltaY: Single); |
| 931 |
const |
| 932 |
{ Do not allow to look exactly up or exactly down, |
| 933 |
as then further vertical moves would be undefined, |
| 934 |
so you would not be able to "get out" of such rotation. } |
| 935 |
MinAngleFromZenith = 0.1; |
| 936 |
var |
| 937 |
Side: TVector3; |
| 938 |
AngleToUp, AngleToDown, MaxChange: Single; |
| 939 |
begin |
| 940 |
Side := -TVector3.CrossProduct(ToCamera, GravUp); |
| 941 |
if DeltaY > 0 then |
| 942 |
begin |
| 943 |
AngleToDown := AngleRadBetweenVectors(ToCamera, -GravUp); |
| 944 |
MaxChange := Max(0, AngleToDown - MinAngleFromZenith); |
| 945 |
if DeltaY > MaxChange then |
| 946 |
DeltaY := MaxChange; |
| 947 |
end else |
| 948 |
begin |
| 949 |
AngleToUp := AngleRadBetweenVectors(ToCamera, GravUp); |
| 950 |
MaxChange := Max(0, AngleToUp - MinAngleFromZenith); |
| 951 |
if DeltaY < -MaxChange then |
| 952 |
DeltaY := -MaxChange; |
| 953 |
end; |
| 954 |
ToCamera := RotatePointAroundAxisRad(DeltaY, ToCamera, Side); |
| 955 |
end; |
| 956 |
|
| 957 |
procedure ProcessHorizontal(const DeltaX: Single); |
| 958 |
begin |
| 959 |
ToCamera := RotatePointAroundAxisRad(-DeltaX, ToCamera, GravUp); |
| 960 |
end; |
| 961 |
|
| 962 |
var |
| 963 |
CameraPos, CameraDir, CameraUp, TargetWorldPos, LookPos: TVector3; |
| 964 |
begin |
| 965 |
inherited; |
| 966 |
if not CameraFollows then |
| 967 |
Exit; |
| 968 |
|
| 969 |
A := RealAvatarHierarchy; |
| 970 |
if (A <> nil) and (InternalViewport <> nil) then |
| 971 |
begin |
| 972 |
Camera.GetWorldView(CameraPos, CameraDir, CameraUp); |
| 973 |
GravUp := Camera.GravityUp; |
| 974 |
|
| 975 |
TargetWorldPos := A.WorldTransform.MultPoint(AvatarTarget); |
| 976 |
// Since camera may update with some delay, we may not look exactly at TargetWorldPos if avatar moved |
| 977 |
LookPos := PointOnLineClosestToPoint(CameraPos, CameraDir, TargetWorldPos); |
| 978 |
|
| 979 |
ToCamera := CameraPos - LookPos; |
| 980 |
if ToCamera.IsZero then |
| 981 |
begin |
| 982 |
WritelnWarning('TCastleThirdPersonNavigation camera position at look target, increase DistanceToAvatarTarget'); |
| 983 |
Exit; |
| 984 |
end; |
| 985 |
|
| 986 |
ProcessVertical(Delta[1]); |
| 987 |
ProcessHorizontal(Delta[0]); |
| 988 |
|
| 989 |
CameraPos := LookPos + ToCamera; |
| 990 |
CameraDir := LookPos - CameraPos; |
| 991 |
CameraUp := GravUp; // will be adjusted to be orthogonal to Dir by SetWorldView |
| 992 |
if ImmediatelyFixBlockedCamera then |
| 993 |
FixCameraForCollisions(CameraPos, CameraDir); |
| 994 |
Camera.SetWorldView(CameraPos, CameraDir, CameraUp); |
| 995 |
end; |
| 996 |
end; |
| 997 |
|
| 998 |
function TCastleThirdPersonNavigation.Zoom(const Factor: Single): Boolean; |
| 999 |
var |
| 1000 |
A: TCastleTransform; |
| 1001 |
|
| 1002 |
procedure CameraDistanceChange(DistanceChange: Single); |
| 1003 |
begin |
| 1004 |
DistanceChange := DistanceChange * CameraDistanceChangeSpeed; |
| 1005 |
DistanceToAvatarTarget := Clamped(DistanceToAvatarTarget + DistanceChange, |
| 1006 |
MinDistanceToAvatarTarget, MaxDistanceToAvatarTarget); |
| 1007 |
|
| 1008 |
{ The actual change in Camera.Translation, caused by changing DistanceToAvatarTarget, |
| 1009 |
will be done smoothly in UpdateCamera. } |
| 1010 |
end; |
| 1011 |
|
| 1012 |
begin |
| 1013 |
Result := false; |
| 1014 |
if not Valid then Exit; |
| 1015 |
|
| 1016 |
A := RealAvatarHierarchy; |
| 1017 |
if (A <> nil) and (InternalViewport <> nil) then |
| 1018 |
begin |
| 1019 |
CameraDistanceChange(-Factor); |
| 1020 |
Result := true; |
| 1021 |
end; |
| 1022 |
end; |
| 1023 |
|
| 1024 |
procedure TCastleThirdPersonNavigation.SetAnimation(const AnimationNames: array of String); |
| 1025 |
const |
| 1026 |
MaxSetAnimationWarnings = 10; |
| 1027 |
var |
| 1028 |
AnimName: String; |
| 1029 |
begin |
| 1030 |
if Avatar <> nil then |
| 1031 |
begin |
| 1032 |
Assert(High(AnimationNames) >= 0); // at least one animation name provided |
| 1033 |
for AnimName in AnimationNames do |
| 1034 |
if Avatar.HasAnimation(AnimName) then |
| 1035 |
begin |
| 1036 |
if not CastleDesignMode then |
| 1037 |
Avatar.AutoAnimation := AnimName; // do not change serialized AutoAnimation |
| 1038 |
Exit; |
| 1039 |
end; |
| 1040 |
if SetAnimationWarningsDone < MaxSetAnimationWarnings then |
| 1041 |
begin |
| 1042 |
WritelnWarning('No useful animation exists on the avatar to show in the current state.' +NL + |
| 1043 |
'Tried: %s.' +NL + |
| 1044 |
'Add the animations to your model, or set the TCastleThirdPersonNavigation.AnimationXxx properties to point to the existing animations.', [ |
| 1045 |
GlueStrings(AnimationNames, ', ') |
| 1046 |
]); |
| 1047 |
Inc(SetAnimationWarningsDone); |
| 1048 |
if SetAnimationWarningsDone = MaxSetAnimationWarnings then |
| 1049 |
WritelnWarning('Further warnings about avatar animations will not be done, to not flood the log, until you assign new Avatar value'); |
| 1050 |
end; |
| 1051 |
end; |
| 1052 |
|
| 1053 |
if Assigned(OnAnimation) then |
| 1054 |
OnAnimation(Self, AnimationNames) |
| 1055 |
end; |
| 1056 |
|
| 1057 |
procedure TCastleThirdPersonNavigation.Update(const SecondsPassed: Single; |
| 1058 |
var HandleInput: Boolean); |
| 1059 |
type |
| 1060 |
TIsOnGround = (igGround, igJumping, igFalling); |
| 1061 |
TSpeedType = (stNormal, stCrouch, stRun); |
| 1062 |
var |
| 1063 |
{ Variables useful in all nested routines below } |
| 1064 |
A: TCastleTransform; |
| 1065 |
RBody: TCastleRigidBody; |
| 1066 |
Collider: TCastleCollider; |
| 1067 |
Speed: Single; |
| 1068 |
|
| 1069 |
{ Warn if the avatar has TCastleRigidBody and TCastleCollider and TCastleRigidBody.Exists. } |
| 1070 |
procedure CheckNotPhysics; |
| 1071 |
begin |
| 1072 |
if (RBody <> nil) and (Collider <> nil) and RBody.Exists then |
| 1073 |
begin |
| 1074 |
if not WarningDonePhysicsNotNecessary then |
| 1075 |
begin |
| 1076 |
WarningDonePhysicsNotNecessary := true; |
| 1077 |
WritelnWarning('For this TCastleThirdPersonNavigation.Transformation, remove physics behaviors (TCastleRigidBody, TCastleCollider) from avatar or set TCastleRigidBody.Exists to false'); |
| 1078 |
end; |
| 1079 |
end; |
| 1080 |
end; |
| 1081 |
|
| 1082 |
{ Realize ctDirect transformation method. } |
| 1083 |
procedure DoDirect(var MovingHorizontally, Rotating: Boolean; var IsOnGround: TIsOnGround); |
| 1084 |
var |
| 1085 |
T: TVector3; |
| 1086 |
begin |
| 1087 |
CheckNotPhysics; |
| 1088 |
|
| 1089 |
T := TVector3.Zero; |
| 1090 |
if Input_Forward.IsPressed(Container) then |
| 1091 |
begin |
| 1092 |
MovingHorizontally := true; |
| 1093 |
T := T + A.Direction * Speed * SecondsPassed; |
| 1094 |
end; |
| 1095 |
if Input_Backward.IsPressed(Container) then |
| 1096 |
begin |
| 1097 |
MovingHorizontally := true; |
| 1098 |
T := T - A.Direction * Speed * SecondsPassed; |
| 1099 |
end; |
| 1100 |
if Input_RightStrafe.IsPressed(Container) then |
| 1101 |
begin |
| 1102 |
MovingHorizontally := true; |
| 1103 |
T := T + TVector3.CrossProduct(A.Direction, A.Up) * Speed * SecondsPassed; |
| 1104 |
end; |
| 1105 |
if Input_LeftStrafe.IsPressed(Container) then |
| 1106 |
begin |
| 1107 |
MovingHorizontally := true; |
| 1108 |
T := T - TVector3.CrossProduct(A.Direction, A.Up) * Speed * SecondsPassed; |
| 1109 |
end; |
| 1110 |
|
| 1111 |
if Input_RightRotate.IsPressed(Container) then |
| 1112 |
begin |
| 1113 |
MovingHorizontally := true; |
| 1114 |
A.Direction := RotatePointAroundAxisRad(-RotationSpeed * SecondsPassed, A.Direction, A.Up); |
| 1115 |
{ TODO: when AimAvatar, this is overridden by UpdateAimAvatar soon. |
| 1116 |
In effect, keys AD don't work when AimAvatar <> aaNone. } |
| 1117 |
end; |
| 1118 |
if Input_LeftRotate.IsPressed(Container) then |
| 1119 |
begin |
| 1120 |
MovingHorizontally := true; |
| 1121 |
A.Direction := RotatePointAroundAxisRad(RotationSpeed * SecondsPassed, A.Direction, A.Up); |
| 1122 |
{ TODO: when AimAvatar, this is overridden by UpdateAimAvatar soon. |
| 1123 |
In effect, keys AD don't work when AimAvatar <> aaNone. } |
| 1124 |
end; |
| 1125 |
|
| 1126 |
if not T.IsPerfectlyZero then |
| 1127 |
A.Move(T, false); |
| 1128 |
end; |
| 1129 |
|
| 1130 |
{ If avatar does not have TCastleRigidBody and TCastleCollider and TCastleRigidBody.Exists, |
| 1131 |
warn and return @false. } |
| 1132 |
function CheckPhysics: Boolean; |
| 1133 |
begin |
| 1134 |
if (RBody = nil) or (not RBody.Exists) then |
| 1135 |
begin |
| 1136 |
if not WarningDoneRigidBodyNecessary then |
| 1137 |
begin |
| 1138 |
WarningDoneRigidBodyNecessary := true; |
| 1139 |
WritelnWarning('For this TCastleThirdPersonNavigation.Transformation, you must add TCastleRigidBody to the avatar and leave TCastleRigidBody.Exists = true'); |
| 1140 |
end; |
| 1141 |
Exit(false); |
| 1142 |
end; |
| 1143 |
|
| 1144 |
if Collider = nil then |
| 1145 |
begin |
| 1146 |
if not WarningDoneColliderNecessary then |
| 1147 |
begin |
| 1148 |
WarningDoneColliderNecessary := true; |
| 1149 |
WritelnWarning('For this TCastleThirdPersonNavigation.Transformation, you must add TCastleCollider to the avatar'); |
| 1150 |
end; |
| 1151 |
Exit(false); |
| 1152 |
end; |
| 1153 |
|
| 1154 |
Result := true; |
| 1155 |
end; |
| 1156 |
|
| 1157 |
{ Realize ctVelocity transformation method. } |
| 1158 |
procedure DoVelocity(var MovingHorizontally, Rotating: Boolean; var IsOnGround: TIsOnGround); |
| 1159 |
var |
| 1160 |
IsOnGroundBool: Boolean; |
| 1161 |
Vel: TVector3; |
| 1162 |
VLength: Single; |
| 1163 |
AvatarBoundingBox: TBox3D; |
| 1164 |
AvatarHeight: Single; |
| 1165 |
MaxHorizontalVelocityChange: Single; |
| 1166 |
Acceleration: Single; |
| 1167 |
HVelocity: TVector3; |
| 1168 |
VVelocity: Single; |
| 1169 |
MoveDirection: TVector3; |
| 1170 |
GroundRayCast: TPhysicsRayCastResult; |
| 1171 |
DistanceToGround: Single; |
| 1172 |
Jump: Single; |
| 1173 |
RayOrigin: TVector3; |
| 1174 |
DeltaSpeed: Single; |
| 1175 |
DeltaAngular: Single; |
| 1176 |
begin |
| 1177 |
if not CheckPhysics then |
| 1178 |
Exit; |
| 1179 |
|
| 1180 |
{ How fast should avatar change it's speed } |
| 1181 |
Acceleration := Speed * 3 / 60; |
| 1182 |
MaxHorizontalVelocityChange := Acceleration * 60; |
| 1183 |
DeltaSpeed := 0; |
| 1184 |
|
| 1185 |
{ Check player is on ground, we use avatar size multiplied by ten to try |
| 1186 |
found ground. Distance is used to check we should set animation to fall |
| 1187 |
or we are almost on ground so use default animation. |
| 1188 |
|
| 1189 |
We need add Collider.Translation because sometimes rigid body origin can be |
| 1190 |
under the collider. And ray will be casted under the floor. } |
| 1191 |
AvatarBoundingBox := A.BoundingBox; |
| 1192 |
AvatarHeight := AvatarBoundingBox.SizeY; |
| 1193 |
RayOrigin := A.Translation + Collider.Translation; |
| 1194 |
|
| 1195 |
{ TODO: In the ideal world, the way we check for ground collisions |
| 1196 |
(and determine Ground, IsOnGround) |
| 1197 |
should be independent from ChangeTransformation. |
| 1198 |
|
| 1199 |
ChangeTransformation says how we change the transformation. |
| 1200 |
|
| 1201 |
We should still have option to use |
| 1202 |
|
| 1203 |
- PhysicsRayCast (maybe from TCastleAbstractRootTransform, as it should |
| 1204 |
not require having TCastleRigidBody on avatar) to detect ground |
| 1205 |
- or Height / WorldHeight calls that cooperate with old simple physics. |
| 1206 |
|
| 1207 |
And we should update IsOnGround in all ChangeTransformation modes. |
| 1208 |
|
| 1209 |
But in practice, now ctDirect forces to do gravity using old physics |
| 1210 |
(because it forbids TCastleRigidBody on avatar), |
| 1211 |
and ctVelocity forces to do gravity using new physics |
| 1212 |
(because it requires TCastleRigidBody on avatar). |
| 1213 |
|
| 1214 |
So checking for ground (collisions) is not independent from ChangeTransformation. |
| 1215 |
When ctVelocity, we have to check for ground using real physics (PhysicsRayCast), |
| 1216 |
it would make no sense to use old simple physics. } |
| 1217 |
|
| 1218 |
GroundRayCast := RBody.PhysicsRayCast( |
| 1219 |
RayOrigin, |
| 1220 |
Vector3(0, -1, 0), |
| 1221 |
AvatarHeight * 3 |
| 1222 |
); |
| 1223 |
|
| 1224 |
{ Four more checks - player should slide down when player just |
| 1225 |
on the edge, but sometimes it stay and center ray don't "see" that we are |
| 1226 |
on ground } |
| 1227 |
if not GroundRayCast.Hit then |
| 1228 |
GroundRayCast := RBody.PhysicsRayCast( |
| 1229 |
RayOrigin + Vector3(AvatarBoundingBox.SizeX * 0.49, 0, 0), |
| 1230 |
Vector3(0, -1, 0), |
| 1231 |
AvatarHeight * 3 |
| 1232 |
); |
| 1233 |
|
| 1234 |
if not GroundRayCast.Hit then |
| 1235 |
GroundRayCast := RBody.PhysicsRayCast( |
| 1236 |
RayOrigin + Vector3(-AvatarBoundingBox.SizeX * 0.49, 0, 0), |
| 1237 |
Vector3(0, -1, 0), |
| 1238 |
AvatarHeight * 3 |
| 1239 |
); |
| 1240 |
|
| 1241 |
if not GroundRayCast.Hit then |
| 1242 |
GroundRayCast := RBody.PhysicsRayCast( |
| 1243 |
RayOrigin + Vector3(0, 0, AvatarBoundingBox.SizeZ * 0.49), |
| 1244 |
Vector3(0, -1, 0), |
| 1245 |
AvatarHeight * 3 |
| 1246 |
); |
| 1247 |
|
| 1248 |
if not GroundRayCast.Hit then |
| 1249 |
GroundRayCast := RBody.PhysicsRayCast( |
| 1250 |
RayOrigin + Vector3(0, 0, -AvatarBoundingBox.SizeZ * 0.49), |
| 1251 |
Vector3(0, -1, 0), |
| 1252 |
AvatarHeight * 3 |
| 1253 |
); |
| 1254 |
|
| 1255 |
if GroundRayCast.Hit then |
| 1256 |
begin |
| 1257 |
DistanceToGround := GroundRayCast.Distance; |
| 1258 |
|
| 1259 |
{ When collider has own translation we need substract it from distance |
| 1260 |
becouse distance will be too big } |
| 1261 |
DistanceToGround := DistanceToGround - Collider.Translation.Y; |
| 1262 |
|
| 1263 |
{ Sometimes rigid body center point can be under the collider so |
| 1264 |
the distance can be negative } |
| 1265 |
if DistanceToGround < 0 then |
| 1266 |
DistanceToGround := 0; |
| 1267 |
|
| 1268 |
IsOnGroundBool := DistanceToGround < AvatarHeight * 0.1; |
| 1269 |
end else |
| 1270 |
begin |
| 1271 |
IsOnGroundBool := false; |
| 1272 |
DistanceToGround := -1; // For animation checking |
| 1273 |
end; |
| 1274 |
|
| 1275 |
if Input_Forward.IsPressed(Container) then |
| 1276 |
begin |
| 1277 |
MovingHorizontally := true; |
| 1278 |
DeltaSpeed := MaxHorizontalVelocityChange * SecondsPassed * MovementControlFactor(IsOnGroundBool); |
| 1279 |
MoveDirection := A.Direction; |
| 1280 |
end; |
| 1281 |
if Input_Backward.IsPressed(Container) then |
| 1282 |
begin |
| 1283 |
MovingHorizontally := true; |
| 1284 |
DeltaSpeed := MaxHorizontalVelocityChange * SecondsPassed * MovementControlFactor(IsOnGroundBool); |
| 1285 |
MoveDirection := -A.Direction; |
| 1286 |
end; |
| 1287 |
if IsOnGroundBool and Input_RightStrafe.IsPressed(Container) then |
| 1288 |
begin |
| 1289 |
MovingHorizontally := true; |
| 1290 |
DeltaSpeed := MaxHorizontalVelocityChange * SecondsPassed; |
| 1291 |
MoveDirection := TVector3.CrossProduct(A.Direction, A.Up); |
| 1292 |
end; |
| 1293 |
if IsOnGroundBool and Input_LeftStrafe.IsPressed(Container) then |
| 1294 |
begin |
| 1295 |
MovingHorizontally := true; |
| 1296 |
DeltaSpeed := MaxHorizontalVelocityChange * SecondsPassed * MovementControlFactor(IsOnGroundBool); |
| 1297 |
MoveDirection := -TVector3.CrossProduct(A.Direction, A.Up); |
| 1298 |
end; |
| 1299 |
|
| 1300 |
Jump := 0; |
| 1301 |
if Input_Jump.IsPressed(Container) and (not FWasJumpInput) and IsOnGroundBool then |
| 1302 |
begin |
| 1303 |
//if and (not FWasJumpInput) and IsOnGroundBool |
| 1304 |
FWasJumpInput := true; |
| 1305 |
MovingHorizontally := false; |
| 1306 |
Jump := JumpSpeed; |
| 1307 |
end else |
| 1308 |
FWasJumpInput := false; |
| 1309 |
|
| 1310 |
DeltaAngular := 0; |
| 1311 |
if Input_RightRotate.IsPressed(Container) then |
| 1312 |
begin |
| 1313 |
DeltaAngular := -RotationSpeed * 60 * SecondsPassed * RotationControlFactor(IsOnGroundBool); |
| 1314 |
end; |
| 1315 |
if Input_LeftRotate.IsPressed(Container) then |
| 1316 |
begin |
| 1317 |
DeltaAngular := RotationSpeed * 60 * SecondsPassed * RotationControlFactor(IsOnGroundBool); |
| 1318 |
end; |
| 1319 |
|
| 1320 |
// jumping |
| 1321 |
if not IsZero(Jump) then |
| 1322 |
begin |
| 1323 |
Vel := RBody.LinearVelocity; |
| 1324 |
Vel.Y := Jump; |
| 1325 |
RBody.LinearVelocity := Vel; |
| 1326 |
end else |
| 1327 |
// moving |
| 1328 |
if not IsZero(DeltaSpeed) then |
| 1329 |
begin |
| 1330 |
Vel := RBody.LinearVelocity; |
| 1331 |
if IsOnGroundBool then |
| 1332 |
begin |
| 1333 |
{ On ground we simply change direction to current one that's |
| 1334 |
helps do things like strafe or fast change direction from |
| 1335 |
forward to backward } |
| 1336 |
HVelocity := Vel; |
| 1337 |
HVelocity.Y := 0; |
| 1338 |
VVelocity := Vel.Y; |
| 1339 |
// maybe use LengthSqrt? |
| 1340 |
VLength := HVelocity.Length; |
| 1341 |
VLength := VLength + DeltaSpeed; |
| 1342 |
if VLength > Speed then |
| 1343 |
VLength := Speed; |
| 1344 |
Vel := MoveDirection * VLength; |
| 1345 |
|
| 1346 |
if IsZero(Jump) then |
| 1347 |
Vel.Y := VVelocity |
| 1348 |
else |
| 1349 |
Vel.Y := Jump; |
| 1350 |
end else |
| 1351 |
begin |
| 1352 |
{ In air we can't simply change movement direction, we will just |
| 1353 |
modify current one a little based on FAirMovementControl factor. |
| 1354 |
Notice that by default FAirMovementControl = 0 so no change |
| 1355 |
will be made. } |
| 1356 |
|
| 1357 |
Vel := Vel + MoveDirection * DeltaSpeed; |
| 1358 |
|
| 1359 |
{ Here we only check speed is not faster than max speed } |
| 1360 |
HVelocity := Vel; |
| 1361 |
HVelocity.Y := 0; |
| 1362 |
VVelocity := Vel.Y; |
| 1363 |
VLength := HVelocity.Length; |
| 1364 |
{ Check max speed } |
| 1365 |
if VLength > Speed then |
| 1366 |
begin |
| 1367 |
VLength := Speed; |
| 1368 |
Vel.Y := 0; |
| 1369 |
Vel := Vel.Normalize * VLength; |
| 1370 |
|
| 1371 |
{ Add gravity here } |
| 1372 |
Vel.Y := VVelocity; |
| 1373 |
end; |
| 1374 |
end; |
| 1375 |
|
| 1376 |
RBody.LinearVelocity := Vel; |
| 1377 |
end else |
| 1378 |
if IsOnGroundBool then |
| 1379 |
begin |
| 1380 |
// slowing down the avatar only on ground |
| 1381 |
Vel := RBody.LinearVelocity; |
| 1382 |
Vel.X := 0; |
| 1383 |
Vel.Z := 0; |
| 1384 |
RBody.LinearVelocity := Vel; |
| 1385 |
end; |
| 1386 |
|
| 1387 |
// rotation |
| 1388 |
if not IsZero(DeltaAngular) then |
| 1389 |
begin |
| 1390 |
RBody.AngularVelocity := Vector3(0, 1, 0) * DeltaAngular; |
| 1391 |
Rotating := true; |
| 1392 |
end |
| 1393 |
else |
| 1394 |
begin |
| 1395 |
RBody.AngularVelocity := Vector3(0, 0, 0); |
| 1396 |
Rotating := false; |
| 1397 |
end; |
| 1398 |
|
| 1399 |
IsOnGround := igGround; |
| 1400 |
if not IsOnGroundBool then |
| 1401 |
begin |
| 1402 |
// TODO: 0.1 should not be hardcoded |
| 1403 |
if RBody.LinearVelocity.Y > 0.1 then |
| 1404 |
IsOnGround := igJumping |
| 1405 |
else |
| 1406 |
{ When avatar falls we change animation to fall only when the distance |
| 1407 |
to ground is smaller than 1/4 of avatar height. This fixes changing |
| 1408 |
animation from walk to fall on small steps like in stairs. |
| 1409 |
|
| 1410 |
DistanceToGround < 0 means that we are in air and ground |
| 1411 |
was not found. |
| 1412 |
|
| 1413 |
TODO: 0.25 should not be hardcoded. } |
| 1414 |
if (DistanceToGround < 0) or (DistanceToGround > A.LocalBoundingBox.SizeY * 0.25) then |
| 1415 |
IsOnGround := igFalling; |
| 1416 |
end; |
| 1417 |
end; |
| 1418 |
|
| 1419 |
{$ifdef CASTLE_UNFINISHED_CHANGE_TRANSFORMATION_BY_FORCE} |
| 1420 |
// TODO: Not finished. |
| 1421 |
procedure DoForce(var MovingHorizontally, Rotating: Boolean; var IsOnGround: TIsOnGround); |
| 1422 |
var |
| 1423 |
DeltaForce: Single; |
| 1424 |
Torque: Single; |
| 1425 |
MoveDirection: TVector3; |
| 1426 |
begin |
| 1427 |
if not CheckPhysics then |
| 1428 |
Exit; |
| 1429 |
|
| 1430 |
DeltaForce := 0; |
| 1431 |
|
| 1432 |
if Input_Forward.IsPressed(Container) then |
| 1433 |
begin |
| 1434 |
MovingHorizontally := true; |
| 1435 |
DeltaForce := Speed * 2 * Collider.Mass * SecondsPassed * 60 {* MovementControlFactor(IsOnGround)}; |
| 1436 |
//WritelnLog('DeltaForce ' + FloatToStr(DeltaForce)); |
| 1437 |
//MoveDirection := A.WorldToLocal(A.Direction); // for AddCenterForce |
| 1438 |
MoveDirection := A.Direction; |
| 1439 |
end; |
| 1440 |
|
| 1441 |
if Input_Backward.IsPressed(Container) then |
| 1442 |
begin |
| 1443 |
MovingHorizontally := true; |
| 1444 |
DeltaForce := Speed * 2 * Collider.Mass * SecondsPassed * 60 {* MovementControlFactor(IsOnGround)}; |
| 1445 |
//WritelnLog('DeltaForce ' + FloatToStr(DeltaForce)); |
| 1446 |
//MoveDirection := A.WorldToLocal(-A.Direction); // for AddCenterForce |
| 1447 |
MoveDirection := -A.Direction; |
| 1448 |
end; |
| 1449 |
|
| 1450 |
Torque := 0; |
| 1451 |
if Input_RightRotate.IsPressed(Container) then |
| 1452 |
begin |
| 1453 |
MovingHorizontally := true; |
| 1454 |
Torque := -RotationSpeed * 60 * SecondsPassed {* RotationControlFactor(IsOnGround)}; |
| 1455 |
end; |
| 1456 |
if Input_LeftRotate.IsPressed(Container) then |
| 1457 |
begin |
| 1458 |
MovingHorizontally := true; |
| 1459 |
Torque := RotationSpeed * 60 * SecondsPassed {* RotationControlFactor(IsOnGround)}; |
| 1460 |
end; |
| 1461 |
|
| 1462 |
if not IsZero(Torque) then |
| 1463 |
begin |
| 1464 |
RBody.AddTorque(A.Up * Torque); |
| 1465 |
Rotating := true; |
| 1466 |
end else |
| 1467 |
if not RBody.AngularVelocity.IsZero(0.1) then |
| 1468 |
begin |
| 1469 |
Rotating := true; |
| 1470 |
{ TODO: In case of space ship this is not OK.} |
| 1471 |
end else |
| 1472 |
begin |
| 1473 |
//RBody.AngularVelocity := Vector3(0, 0, 0); |
| 1474 |
Rotating := false; |
| 1475 |
end; |
| 1476 |
|
| 1477 |
if MovingHorizontally then |
| 1478 |
begin |
| 1479 |
RBody.AddForce(MoveDirection * DeltaForce, true); |
| 1480 |
//RBody.ApplyImpulse(MoveDirection * DeltaForce, A.Translation); |
| 1481 |
//RBody.ApplyImpulse(MoveDirection * DeltaForce, Collider.Translation); |
| 1482 |
end; |
| 1483 |
end; |
| 1484 |
{$endif CASTLE_UNFINISHED_CHANGE_TRANSFORMATION_BY_FORCE} |
| 1485 |
|
| 1486 |
{ Make camera follow the A translation (in world-space, |
| 1487 |
so also account for parents' translations). |
| 1488 |
Following the character also makes sure that camera stays updated |
| 1489 |
(keeps DistanceToAvatarTarget) |
| 1490 |
when the avatar is being moved by other routines (e.g. because A.Gravity is working). |
| 1491 |
|
| 1492 |
Also avoid camera being blocked by some wall. |
| 1493 |
This needs to be redone, in case some wall blocks us e.g. because of collisions. |
| 1494 |
|
| 1495 |
Does not follow the perfect location instantly, |
| 1496 |
which makes a nice effect when character is moving fast. |
| 1497 |
It's important to avoid sudden camera moves on sudden avatar moves, |
| 1498 |
e.g. changing Y when going up/down stairs. } |
| 1499 |
procedure UpdateCamera; |
| 1500 |
var |
| 1501 |
TargetWorldPos, CameraPos, CameraDir, CameraUp, CameraPosTarget, CameraDirToTarget: TVector3; |
| 1502 |
MaxDistance: Single; |
| 1503 |
begin |
| 1504 |
if not CameraFollows then |
| 1505 |
Exit; |
| 1506 |
|
| 1507 |
TargetWorldPos := A.WorldTransform.MultPoint(AvatarTarget); |
| 1508 |
|
| 1509 |
Camera.GetWorldView(CameraPos, CameraDir, CameraUp); |
| 1510 |
|
| 1511 |
{ We use CameraDirToTarget, not CameraDir, because (since we update with delay) |
| 1512 |
camera may look at a slightly shifted point. |
| 1513 |
But we still adjust camera position to look (without blockers) at avatar. } |
| 1514 |
CameraDirToTarget := TargetWorldPos - CameraPos; |
| 1515 |
|
| 1516 |
{ We need to check both CameraPosTarget and final CameraPos for collisions. |
| 1517 |
But it would be wasteful to call FixCameraForCollisions 2 times, |
| 1518 |
to calculate mostly the same. |
| 1519 |
So we use one call to CameraMaxDistanceToTarget. } |
| 1520 |
MaxDistance := CameraMaxDistanceToTarget(A, TargetWorldPos, CameraDirToTarget); |
| 1521 |
|
| 1522 |
{ No need to use CameraDir.AdjustToLength(xxx) as we know CameraDir is normalized. |
| 1523 |
Note that this is not entirely correct: we use distance we obtained with CameraDirToTarget, |
| 1524 |
but our desired camera direction is CameraDir (i.e. unchanged from current camera direction). } |
| 1525 |
CameraPosTarget := TargetWorldPos - CameraDir * Min(MaxDistance, DistanceToAvatarTarget); |
| 1526 |
|
| 1527 |
CameraPos := SmoothTowards(CameraPos, CameraPosTarget, SecondsPassed, CameraSpeed); |
| 1528 |
if ImmediatelyFixBlockedCamera then |
| 1529 |
begin |
| 1530 |
if PointsDistanceSqr(CameraPos, TargetWorldPos) > Sqr(MaxDistance) then |
| 1531 |
CameraPos := TargetWorldPos - CameraDir * MaxDistance; |
| 1532 |
end; |
| 1533 |
|
| 1534 |
Camera.SetWorldView(CameraPos, CameraDir, CameraUp); |
| 1535 |
end; |
| 1536 |
|
| 1537 |
{ Rotate avatar if needed by AimAvatar. |
| 1538 |
Returns are we rotating now. } |
| 1539 |
function UpdateAimAvatar: Boolean; |
| 1540 |
const |
| 1541 |
AngleEpsilon = 0.01; |
| 1542 |
var |
| 1543 |
TargetDir: TVector3; |
| 1544 |
Angle: Single; |
| 1545 |
begin |
| 1546 |
Result := false; |
| 1547 |
if AimAvatar = aaNone then Exit; |
| 1548 |
|
| 1549 |
// calculate TargetDir, in the same coordinate space as A.Direction |
| 1550 |
TargetDir := Camera.Direction; |
| 1551 |
if AimAvatar = aaHorizontal then |
| 1552 |
TargetDir := ToGravityPlane(TargetDir, Camera.GravityUp); |
| 1553 |
TargetDir := A.Parent.WorldToLocalDirection(TargetDir); |
| 1554 |
|
| 1555 |
Angle := AngleRadBetweenVectors(TargetDir, A.Direction); |
| 1556 |
if Angle > AngleEpsilon then |
| 1557 |
begin |
| 1558 |
MinVar(Angle, AvatarRotationSpeed * SecondsPassed); |
| 1559 |
A.Direction := RotatePointAroundAxisRad(Angle, A.Direction, -TVector3.CrossProduct(TargetDir, A.Direction)); |
| 1560 |
Result := true; |
| 1561 |
end; |
| 1562 |
end; |
| 1563 |
|
| 1564 |
{ Call SetAnimation to set proper animation of the avatar (Avatar.AutoAnimation). |
| 1565 |
Calls SetAnimation once and exactly once (in all possible branches of this routine). } |
| 1566 |
procedure UpdateAnimation(const MovingHorizontally, Rotating: Boolean; const IsOnGround: TIsOnGround; |
| 1567 |
const SpeedType: TSpeedType); |
| 1568 |
begin |
| 1569 |
{ TODO: In case we use AimAvatar and you move mouse very slowly for short amounts, |
| 1570 |
we may switch very quickly between AnimationIdle and AnimationRotate. |
| 1571 |
This makes somewhat bad look in third_person_navigation, and though it uses |
| 1572 |
DefaultAnimationTransition <> 0. |
| 1573 |
Should we protect from it here, to introduce minimal time to change |
| 1574 |
animation between rotating/non-rotating variant? } |
| 1575 |
|
| 1576 |
case IsOnGround of |
| 1577 |
igJumping: SetAnimation([AnimationJump, AnimationIdle]); |
| 1578 |
igFalling: SetAnimation([AnimationFall, AnimationIdle]); |
| 1579 |
else |
| 1580 |
begin |
| 1581 |
if MovingHorizontally then |
| 1582 |
begin |
| 1583 |
case SpeedType of |
| 1584 |
stNormal: SetAnimation([AnimationWalk, AnimationIdle]); |
| 1585 |
stRun : SetAnimation([AnimationRun, AnimationIdle]); |
| 1586 |
stCrouch: SetAnimation([AnimationCrouch, AnimationCrouchIdle, AnimationIdle]); |
| 1587 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1588 |
else raise EInternalError.Create('SpeedType?'); |
| 1589 |
{$endif} |
| 1590 |
end; |
| 1591 |
end else |
| 1592 |
begin |
| 1593 |
if SpeedType = stCrouch then |
| 1594 |
begin |
| 1595 |
if Rotating then |
| 1596 |
SetAnimation([AnimationCrouchRotate, AnimationCrouch, AnimationCrouchIdle, AnimationIdle]) |
| 1597 |
else |
| 1598 |
SetAnimation([AnimationCrouchIdle, AnimationCrouch, AnimationIdle]); |
| 1599 |
end else |
| 1600 |
begin |
| 1601 |
{ Note that stRun behaves the same as stNormal when Moving = false. |
| 1602 |
This just means user holds Shift (Input_Run) but not actually moving by AWSD. } |
| 1603 |
if Rotating then |
| 1604 |
SetAnimation([AnimationRotate, AnimationWalk, AnimationIdle]) |
| 1605 |
else |
| 1606 |
SetAnimation([AnimationIdle]); |
| 1607 |
end; |
| 1608 |
end; |
| 1609 |
end; |
| 1610 |
end; |
| 1611 |
end; |
| 1612 |
|
| 1613 |
var |
| 1614 |
SpeedType: TSpeedType; |
| 1615 |
MovingHorizontally, Rotating: Boolean; |
| 1616 |
IsOnGround: TIsOnGround; |
| 1617 |
begin |
| 1618 |
inherited; |
| 1619 |
if not Valid then Exit; |
| 1620 |
|
| 1621 |
A := RealAvatarHierarchy; |
| 1622 |
|
| 1623 |
if (A = nil) or (InternalViewport = nil) then |
| 1624 |
Exit; |
| 1625 |
|
| 1626 |
RBody := A.FindBehavior(TCastleRigidBody) as TCastleRigidBody; |
| 1627 |
Collider := A.FindBehavior(TCastleCollider) as TCastleCollider; |
| 1628 |
|
| 1629 |
if Input_Run.IsPressed(Container) then |
| 1630 |
begin |
| 1631 |
SpeedType := stRun; |
| 1632 |
Speed := RunSpeed; |
| 1633 |
end else |
| 1634 |
if Input_Crouch.IsPressed(Container) then |
| 1635 |
begin |
| 1636 |
SpeedType := stCrouch; |
| 1637 |
Speed := CrouchSpeed; |
| 1638 |
end else |
| 1639 |
begin |
| 1640 |
SpeedType := stNormal; |
| 1641 |
Speed := MoveSpeed; |
| 1642 |
end; |
| 1643 |
|
| 1644 |
MovingHorizontally := false; |
| 1645 |
Rotating := false; |
| 1646 |
IsOnGround := igGround; |
| 1647 |
|
| 1648 |
case FChangeTransformation of |
| 1649 |
ctAuto: |
| 1650 |
if (RBody <> nil) and RBody.Exists and (Collider <> nil) then |
| 1651 |
DoVelocity(MovingHorizontally, Rotating, IsOnGround) |
| 1652 |
else |
| 1653 |
DoDirect(MovingHorizontally, Rotating, IsOnGround); |
| 1654 |
ctDirect: DoDirect(MovingHorizontally, Rotating, IsOnGround); |
| 1655 |
ctVelocity: DoVelocity(MovingHorizontally, Rotating, IsOnGround); |
| 1656 |
{$ifdef CASTLE_UNFINISHED_CHANGE_TRANSFORMATION_BY_FORCE} |
| 1657 |
ctForce: DoForce(MovingHorizontally, Rotating, IsOnGround); |
| 1658 |
{$endif CASTLE_UNFINISHED_CHANGE_TRANSFORMATION_BY_FORCE} |
| 1659 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1660 |
else raise EInternalError.Create('TCastleThirdPersonNavigation.FTransformation?'); |
| 1661 |
{$endif} |
| 1662 |
end; |
| 1663 |
|
| 1664 |
{ Note: always execute UpdateAimAvatar, regardless of Rotating. |
| 1665 |
So wite "UpdateAimAvatar or Rotating" and not "Rotating or UpdateAimAvatar". } |
| 1666 |
Rotating := UpdateAimAvatar or Rotating; |
| 1667 |
|
| 1668 |
UpdateAnimation(MovingHorizontally, Rotating, IsOnGround, SpeedType); |
| 1669 |
|
| 1670 |
UpdateCamera; |
| 1671 |
end; |
| 1672 |
|
| 1673 |
{ Since String values cannot have default properties, |
| 1674 |
we use "stored" methods to avoid storing default value. } |
| 1675 |
|
| 1676 |
function TCastleThirdPersonNavigation.AnimationIdleStored: Boolean; |
| 1677 |
begin |
| 1678 |
Result := FAnimationIdle <> DefaultAnimationIdle; |
| 1679 |
end; |
| 1680 |
|
| 1681 |
function TCastleThirdPersonNavigation.AnimationRotateStored: Boolean; |
| 1682 |
begin |
| 1683 |
Result := FAnimationRotate <> DefaultAnimationRotate; |
| 1684 |
end; |
| 1685 |
|
| 1686 |
function TCastleThirdPersonNavigation.AnimationWalkStored: Boolean; |
| 1687 |
begin |
| 1688 |
Result := FAnimationWalk <> DefaultAnimationWalk; |
| 1689 |
end; |
| 1690 |
|
| 1691 |
function TCastleThirdPersonNavigation.AnimationRunStored: Boolean; |
| 1692 |
begin |
| 1693 |
Result := FAnimationRun <> DefaultAnimationRun; |
| 1694 |
end; |
| 1695 |
|
| 1696 |
function TCastleThirdPersonNavigation.AnimationCrouchStored: Boolean; |
| 1697 |
begin |
| 1698 |
Result := FAnimationCrouch <> DefaultAnimationCrouch; |
| 1699 |
end; |
| 1700 |
|
| 1701 |
function TCastleThirdPersonNavigation.AnimationCrouchIdleStored: Boolean; |
| 1702 |
begin |
| 1703 |
Result := FAnimationCrouchIdle <> DefaultAnimationCrouchIdle; |
| 1704 |
end; |
| 1705 |
|
| 1706 |
function TCastleThirdPersonNavigation.AnimationCrouchRotateStored: Boolean; |
| 1707 |
begin |
| 1708 |
Result := FAnimationCrouchRotate <> DefaultAnimationCrouchRotate; |
| 1709 |
end; |
| 1710 |
|
| 1711 |
function TCastleThirdPersonNavigation.AnimationJumpStored: Boolean; |
| 1712 |
begin |
| 1713 |
Result := FAnimationJump <> DefaultAnimationJump; |
| 1714 |
end; |
| 1715 |
|
| 1716 |
function TCastleThirdPersonNavigation.AnimationFallStored: Boolean; |
| 1717 |
begin |
| 1718 |
Result := FAnimationFall <> DefaultAnimationFall; |
| 1719 |
end; |
| 1720 |
|
| 1721 |
procedure TCastleThirdPersonNavigation.SetInitialHeightAboveTarget(const Value: Single); |
| 1722 |
begin |
| 1723 |
if FInitialHeightAboveTarget <> Value then |
| 1724 |
begin |
| 1725 |
FInitialHeightAboveTarget := Value; |
| 1726 |
end; |
| 1727 |
end; |
| 1728 |
|
| 1729 |
procedure TCastleThirdPersonNavigation.SetDistanceToAvatarTarget(const Value: Single); |
| 1730 |
begin |
| 1731 |
if FDistanceToAvatarTarget <> Value then |
| 1732 |
begin |
| 1733 |
FDistanceToAvatarTarget := Value; |
| 1734 |
end; |
| 1735 |
end; |
| 1736 |
|
| 1737 |
procedure TCastleThirdPersonNavigation.SetCameraFollows(const Value: Boolean); |
| 1738 |
begin |
| 1739 |
if FCameraFollows <> Value then |
| 1740 |
begin |
| 1741 |
FCameraFollows := Value; |
| 1742 |
end; |
| 1743 |
end; |
| 1744 |
|
| 1745 |
function TCastleThirdPersonNavigation.PropertySections(const PropertyName: String): TPropertySections; |
| 1746 |
begin |
| 1747 |
if ArrayContainsString(PropertyName, [ |
| 1748 |
'CameraFollows', 'AvatarTarget', 'Avatar', 'AvatarHierarchy', 'Radius', |
| 1749 |
'AimAvatar', 'MoveSpeed', 'CrouchSpeed', 'RunSpeed', 'JumpSpeed', 'RotationSpeed', |
| 1750 |
'AirMovementControl', 'AirRotationControl', |
| 1751 |
'AnimationIdle', 'AnimationWalk', 'AnimationRun', 'AnimationJump', 'AnimationRotate', |
| 1752 |
'AnimationCrouch', 'AnimationCrouchIdle', 'AnimationCrouchRotate','AnimationFall', |
| 1753 |
'InitialHeightAboveTarget', 'DistanceToAvatarTarget', 'ChangeTransformation' |
| 1754 |
]) then |
| 1755 |
Result := [psBasic] |
| 1756 |
else |
| 1757 |
Result := inherited PropertySections(PropertyName); |
| 1758 |
end; |
| 1759 |
|
| 1760 |
procedure TCastleThirdPersonNavigation.AvatarFreeNotification( |
| 1761 |
const Sender: TFreeNotificationObserver); |
| 1762 |
begin |
| 1763 |
Avatar := nil; |
| 1764 |
end; |
| 1765 |
|
| 1766 |
procedure TCastleThirdPersonNavigation.AvatarHierarchyFreeNotification( |
| 1767 |
const Sender: TFreeNotificationObserver); |
| 1768 |
begin |
| 1769 |
AvatarHierarchy := nil; |
| 1770 |
end; |
| 1771 |
|
| 1772 |
function TCastleThirdPersonNavigation.MovementControlFactor( |
| 1773 |
const AvatarOnGround: Boolean): Single; |
| 1774 |
begin |
| 1775 |
if AvatarOnGround then |
| 1776 |
Exit(1.0) |
| 1777 |
else |
| 1778 |
Result := FAirMovementControl; |
| 1779 |
end; |
| 1780 |
|
| 1781 |
function TCastleThirdPersonNavigation.RotationControlFactor( |
| 1782 |
const AvatarOnGround: Boolean): Single; |
| 1783 |
begin |
| 1784 |
if AvatarOnGround then |
| 1785 |
Exit(1.0) |
| 1786 |
else |
| 1787 |
Result := FAirRotationControl; |
| 1788 |
end; |
| 1789 |
|
| 1790 |
function TCastleThirdPersonNavigation.Input_CameraCloser: TInputShortcut; |
| 1791 |
begin |
| 1792 |
Result := Input_ZoomIn; |
| 1793 |
end; |
| 1794 |
|
| 1795 |
function TCastleThirdPersonNavigation.Input_CameraFurther: TInputShortcut; |
| 1796 |
begin |
| 1797 |
Result := Input_ZoomOut; |
| 1798 |
end; |
| 1799 |
|
| 1800 |
{$define read_implementation_methods} |
| 1801 |
{$I auto_generated_persistent_vectors/tcastlethirdpersonnavigation_persistent_vectors.inc} |
| 1802 |
{$undef read_implementation_methods} |
| 1803 |
|
| 1804 |
initialization |
| 1805 |
RegisterSerializableComponent(TCastleThirdPersonNavigation, ['Navigation', 'Third-Person']); |
| 1806 |
end. |