| 1 |
{ |
| 2 |
Copyright 2003-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 |
{ Camera and navigation (TCastleCamera, TCastleExamineNavigation, TCastleWalkNavigation). } |
| 17 |
unit CastleCameras; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses SysUtils, Classes, |
| 24 |
CastleVectors, CastleUtils, CastleKeysMouse, CastleBoxes, CastleQuaternions, |
| 25 |
CastleFrustum, CastleUIControls, CastleInternalRays, CastleTimeUtils, |
| 26 |
CastleInputs, CastleTriangles, CastleRectangles, CastleClassUtils, |
| 27 |
CastleInternalCameraGestures, CastleTransform, CastleProjection; |
| 28 |
|
| 29 |
type |
| 30 |
{ Possible navigation input types for @link(TCastleNavigation.Input). } |
| 31 |
TNavigationInput = ( |
| 32 |
{ Normal input types. This includes all inputs available as |
| 33 |
Input_Xxx properties in TCastleNavigation descendants. |
| 34 |
They are all fully configurable (as TInputShortcut class), |
| 35 |
they may be mouse button presses, mouse wheel clicks, or key presses. |
| 36 |
You can always clear some shortcut (like @code(TCastleWalkNavigation.Input_Forward.MakeClear)) |
| 37 |
to disable a specific shortcut. |
| 38 |
Excluding niNormal from TCastleNavigation.Input is an easy way to disable @italic(all) |
| 39 |
shortcuts. } |
| 40 |
niNormal, |
| 41 |
|
| 42 |
{ Mouse and touch dragging. Both TCastleExamineNavigation and TCastleWalkNavigation implement their own, |
| 43 |
special reactions to mouse dragging, that allows to navigate / rotate |
| 44 |
while pressing specific mouse buttons. |
| 45 |
|
| 46 |
Note that mouse dragging is automatically disabled when |
| 47 |
@link(TCastleWalkNavigation.MouseLook) is used. } |
| 48 |
niMouseDragging, |
| 49 |
|
| 50 |
{ Touch gestures, like multi-touch pinch or pan gesture. } |
| 51 |
niGesture, |
| 52 |
|
| 53 |
{ Navigation using 3D mouse devices, like the ones from 3dconnexion. } |
| 54 |
ni3dMouse |
| 55 |
); |
| 56 |
TNavigationInputs = set of TNavigationInput; |
| 57 |
|
| 58 |
TCameraInput = TNavigationInput deprecated 'use TNavigationInput'; |
| 59 |
TCameraInputs = TNavigationInputs deprecated 'use TNavigationInputs'; |
| 60 |
|
| 61 |
EViewportNotAssigned = class(Exception); |
| 62 |
|
| 63 |
TCastleNavigation = class; |
| 64 |
|
| 65 |
{ } |
| 66 |
T3BoolInputs = array [0..2, boolean] of TInputShortcut; |
| 67 |
|
| 68 |
{ See @link(TCastleNavigation.MoveAllowed) and |
| 69 |
@link(TCastleNavigation.OnMoveAllowed) } |
| 70 |
TMoveAllowedFunc = function (const Sender: TCastleNavigation; |
| 71 |
const OldPos, ProposedNewPos: TVector3; out NewPos: TVector3; |
| 72 |
const Radius: Single; const BecauseOfGravity: Boolean): boolean of object; |
| 73 |
|
| 74 |
{ See @link(TCastleNavigation.OnFall). } |
| 75 |
TFallNotifyFunc = procedure (const Sender: TCastleNavigation; |
| 76 |
const FallHeight: Single) of object; |
| 77 |
|
| 78 |
{ Handle user input to modify viewport's camera. |
| 79 |
|
| 80 |
Once you create an instance of this class (create non-abstract descendants |
| 81 |
like TCastleExamineNavigation, TCastleWalkNavigation, TCastleThirdPersonNavigation) |
| 82 |
just and add it as @link(TCastleViewport) child. |
| 83 |
The navigation will automatically affect the current camera of parent viewport. |
| 84 |
|
| 85 |
In many ways, this is just a normal @link(TCastleUserInterface) descendant. |
| 86 |
E.g. it processes input just like any other @link(TCastleUserInterface) descendant |
| 87 |
(there isn't any special mechanism through which @link(TCastleViewport) passes |
| 88 |
input to the navigation), |
| 89 |
the @link(Exists) property works and so on. |
| 90 |
|
| 91 |
Note that you don't really @italic(need) to use any TCastleNavigation to manipulate |
| 92 |
the camera. You can just access @link(TCastleViewport.Camera) from anywhere |
| 93 |
(like TCastleView code) and move, rotate it as you wish. |
| 94 |
TCastleNavigation is just a comfortable way to encapsulate |
| 95 |
some navigation methods, but it's not the only way to manipulate the camera. |
| 96 |
|
| 97 |
Various TCastleNavigation descendants implement various navigation |
| 98 |
methods, for example TCastleExamineNavigation allows the user to rotate |
| 99 |
and scale the model (imagine that you're holding a 3D model in your |
| 100 |
hands and you look at it from various sides) and TCastleWalkNavigation |
| 101 |
implements typical navigation in the style of first-person shooter |
| 102 |
games. } |
| 103 |
TCastleNavigation = class(TCastleUserInterface) |
| 104 |
strict private |
| 105 |
FInput: TNavigationInputs; |
| 106 |
FRadius: Single; |
| 107 |
FModelBox: TBox3D; |
| 108 |
FOnMoveAllowed: TMoveAllowedFunc; |
| 109 |
FOnFall: TFallNotifyFunc; |
| 110 |
FWarningInvalidParentDone: Boolean; |
| 111 |
FCheckCollisions: Boolean; |
| 112 |
FZoomEnabled: Boolean; |
| 113 |
FInput_ZoomIn: TInputShortcut; |
| 114 |
FInput_ZoomOut: TInputShortcut; |
| 115 |
|
| 116 |
function GetIgnoreAllInputs: boolean; |
| 117 |
procedure SetIgnoreAllInputs(const Value: boolean); |
| 118 |
protected |
| 119 |
{ Needed for niMouseDragging navigation. |
| 120 |
Checking MouseDraggingStarted means that we handle only dragging that |
| 121 |
was initialized on viewport (since the viewport passed events to navigation). |
| 122 |
MouseDraggingStarted -1 means none, otherwise it's the finder index |
| 123 |
(to support multitouch). } |
| 124 |
MouseDraggingStarted: Integer; |
| 125 |
MouseDraggingStart: TVector2; |
| 126 |
|
| 127 |
function GoodModelBox: TBox3D; |
| 128 |
|
| 129 |
{ Viewport we should manipulate. |
| 130 |
This is @nil, or TCastleViewport instance, but it cannot be declared as |
| 131 |
TCastleViewport due to unit dependencies. } |
| 132 |
function InternalViewport: TCastleUserInterface; |
| 133 |
|
| 134 |
{ If this is @true, then Camera is non-nil, InternalViewport is non-nil, |
| 135 |
and navigation should function as usual. } |
| 136 |
function Valid: Boolean; |
| 137 |
|
| 138 |
{ Behave as if @link(Input) is like this. |
| 139 |
This allows to disable input when not @link(Valid). } |
| 140 |
function UsingInput: TNavigationInputs; |
| 141 |
|
| 142 |
{ Can we use mouse dragging. Checks @link(UsingInput) and so @link(Valid) already. } |
| 143 |
function ReallyEnableMouseDragging: boolean; virtual; |
| 144 |
|
| 145 |
{ Check collisions to determine how high above ground is given point (in world coordinates). |
| 146 |
Checks collisions through parent TCastleViewport, if CheckCollisions. } |
| 147 |
procedure Height(const APosition: TVector3; |
| 148 |
out AIsAbove: Boolean; |
| 149 |
out AnAboveHeight: Single; out AnAboveGround: PTriangle); |
| 150 |
|
| 151 |
{ Check collisions to determine can the object move. |
| 152 |
Object wants to move from OldPos to ProposedNewPos (in world coordinates). |
| 153 |
|
| 154 |
Returns @false if no move is allowed. |
| 155 |
Otherwise returns @true and sets NewPos to the position |
| 156 |
where user should be moved. |
| 157 |
|
| 158 |
If you're doing a simple |
| 159 |
check for collisions, you will always |
| 160 |
want to set NewPos to ProposedNewPos when returning @true. |
| 161 |
|
| 162 |
But you can also do more sophisticated calculations and |
| 163 |
sometimes not allow user to move to ProposedNewPos, but allow |
| 164 |
him to move instead to some other close position. |
| 165 |
For example when doing "wall sliding" (common in FPS games): |
| 166 |
when you're trying to walk "into the wall", you move along the wall instead. |
| 167 |
|
| 168 |
It's allowed to modify NewPos when returning @false. |
| 169 |
It makes no effect. |
| 170 |
|
| 171 |
BecauseOfGravity says whether this move is caused by gravity |
| 172 |
dragging the player down. You can use BecauseOfGravity e.g. to implement |
| 173 |
@link(TCastleViewport.PreventInfiniteFallingDown). |
| 174 |
|
| 175 |
Implementation calls OnMoveAllowed and |
| 176 |
checks collisions through parent TCastleViewport, if CheckCollisions. } |
| 177 |
function MoveAllowed( |
| 178 |
const OldPos: TVector3; ProposedNewPos: TVector3; out NewPos: TVector3; |
| 179 |
const BecauseOfGravity, CheckClimbHeight: Boolean): Boolean; virtual; |
| 180 |
|
| 181 |
{ Like Move, but you pass here final ProposedNewPos. |
| 182 |
|
| 183 |
LocalProposedNewPos is given in TCastleCamera parent coordinates, |
| 184 |
so it works naturally in the same space as TCastleCamera.Translation, Direction, Up. |
| 185 |
You can think "I want to move to Translation + MoveVector". } |
| 186 |
function MoveTo(const LocalProposedNewPos: TVector3; |
| 187 |
const BecauseOfGravity, CheckClimbHeight: boolean): boolean; |
| 188 |
|
| 189 |
{ Try to move from current Translation to Translation + MoveVector. |
| 190 |
Checks MoveAllowed, also (if CheckClimbHeight is @true) |
| 191 |
checks the ClimbHeight limit. |
| 192 |
|
| 193 |
MoveVector is given in TCastleCamera parent coordinates, |
| 194 |
so it works naturally in the same space as TCastleCamera.Translation, Direction, Up. |
| 195 |
You can think "I want to move TCastleCamera to Translation + MoveVector". |
| 196 |
|
| 197 |
Returns @false if move was not possible and Position didn't change. |
| 198 |
Returns @true is some move occured (but don't assume too much: |
| 199 |
possibly we didn't move to exactly Position + MoveVector |
| 200 |
because of wall sliding). } |
| 201 |
function Move(const MoveVector: TVector3; |
| 202 |
const BecauseOfGravity, CheckClimbHeight: boolean): boolean; |
| 203 |
|
| 204 |
{ Zoom in / out. |
| 205 |
Negative Factor makes "zoom out", positive makes "zoom in" (zero makes nothing). |
| 206 |
|
| 207 |
Called only if @link(ZoomEnabled), so no need to check it within implementation. |
| 208 |
|
| 209 |
Factor values correspond to TInputPressRelease.MouseWheelScroll values, |
| 210 |
so 1.0 should be treated like a "one operation" and some systems only generate |
| 211 |
values -1 or +1 (and never fractions). } |
| 212 |
function Zoom(const Factor: Single): Boolean; virtual; |
| 213 |
public |
| 214 |
const |
| 215 |
{ Default value for TCastleNavigation.Radius. |
| 216 |
Matches the default VRML/X3D NavigationInfo.avatarSize[0]. } |
| 217 |
DefaultRadius = 0.25; |
| 218 |
{ Default value for TCastleNavigation.PreferredHeight. |
| 219 |
Matches the default VRML/X3D NavigationInfo.avatarSize[1]. } |
| 220 |
DefaultPreferredHeight = 1.6; |
| 221 |
DefaultInput = [niNormal, niMouseDragging, ni3dMouse, niGesture]; |
| 222 |
DefaultHeadBobbingTime = 0.5; |
| 223 |
DefaultHeadBobbing = 0.02; |
| 224 |
DefaultCrouchHeight = 0.5; |
| 225 |
|
| 226 |
constructor Create(AOwner: TComponent); override; |
| 227 |
procedure Assign(Source: TPersistent); override; |
| 228 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 229 |
|
| 230 |
{ Used by @link(MoveAllowed), see there for description. |
| 231 |
You can assign this property. } |
| 232 |
property OnMoveAllowed: TMoveAllowedFunc read FOnMoveAllowed write FOnMoveAllowed; |
| 233 |
|
| 234 |
{ Notification that we have been falling down for some time due to gravity, |
| 235 |
and suddenly stopped (which means we "hit the ground"). |
| 236 |
|
| 237 |
This event can be useful in games, for example to lower player's health, |
| 238 |
and/or make a visual effect (like a "red out" indicating pain) |
| 239 |
and/or make a sound effect ("Ouch!" or "Thud!" or such sounds). |
| 240 |
You can look at FallHeight parameter, given to the callback, |
| 241 |
e.g. to gauge how much health decreases. } |
| 242 |
property OnFall: TFallNotifyFunc read FOnFall write FOnFall; |
| 243 |
|
| 244 |
{ Associated TCastleCamera of the viewport. |
| 245 |
May return @nil if the viewport camera is not assigned. |
| 246 |
@raises EViewportNotAssigned If Viewport not assigned yet. } |
| 247 |
function Camera: TCastleCamera; |
| 248 |
|
| 249 |
{$ifdef FPC} |
| 250 |
{ Deprecated, use more flexible @link(Input) instead. |
| 251 |
@code(IgnoreAllInputs := true) is equivalent to @code(Input := []), |
| 252 |
@code(IgnoreAllInputs := false) is equivalent to @code(Input := DefaultInput). |
| 253 |
@deprecated } |
| 254 |
property IgnoreAllInputs: boolean |
| 255 |
read GetIgnoreAllInputs write SetIgnoreAllInputs default false; deprecated; |
| 256 |
{$endif FPC} |
| 257 |
|
| 258 |
{ The radius of a sphere around the camera |
| 259 |
that makes collisions with the world. |
| 260 |
|
| 261 |
@unorderedList( |
| 262 |
@item(Collision detection routines use this.) |
| 263 |
@item(It determines the projection near plane (that must be slightly |
| 264 |
smaller than this radius), see also @link(TCastleCamera.ProjectionNear).) |
| 265 |
@item( |
| 266 |
Walk navigation uses this for automatically correcting |
| 267 |
PreferredHeight, otherwise weird things could happen |
| 268 |
if your avatar height is too small compared to the camera radius. |
| 269 |
See @link(CorrectPreferredHeight). |
| 270 |
|
| 271 |
Especially useful if you let |
| 272 |
user change PreferredHeight at runtime by |
| 273 |
Input_IncreasePreferredHeight, Input_DecreasePreferredHeight. |
| 274 |
) |
| 275 |
) } |
| 276 |
property Radius: Single read FRadius write FRadius {$ifdef FPC}default DefaultRadius{$endif}; |
| 277 |
|
| 278 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 279 |
function Release(const Event: TInputPressRelease): boolean; override; |
| 280 |
|
| 281 |
procedure AnimateTo(const OtherCamera: TCastleCamera; const Time: TFloatTime); overload; deprecated 'use Viewport.Camera.AnimateTo'; |
| 282 |
procedure AnimateTo(const OtherNavigation: TCastleNavigation; const Time: TFloatTime); overload; deprecated 'use AnimateTo with TCastleCamera, not TCastleNavigation'; |
| 283 |
procedure AnimateTo(const APos, ADir, AUp: TVector3; const Time: TFloatTime); overload; deprecated 'use Viewport.Camera.AnimateTo'; |
| 284 |
function Animation: boolean; deprecated 'use Viewport.Camera.Animation'; |
| 285 |
|
| 286 |
{ Approximate size of the world that is viewed. |
| 287 |
Determines the speed of zooming and (in case of TCastleExamineNavigation) of many other operations too. |
| 288 |
Initially this is an empty box. |
| 289 |
Internally we will use the Viewport.Items.BoundingBox if this is empty. } |
| 290 |
property ModelBox: TBox3D read FModelBox write FModelBox; |
| 291 |
|
| 292 |
{ Input methods available to user. See documentation of TNavigationInput |
| 293 |
type for possible values and their meaning. |
| 294 |
|
| 295 |
To disable any user interaction with this navigation |
| 296 |
you can simply set this to empty. } |
| 297 |
property Input: TNavigationInputs read FInput write FInput default DefaultInput; |
| 298 |
|
| 299 |
{ Bring camera closer to the model. Works only if @link(ZoomEnabled). |
| 300 |
By deafult mwUp (mouse wheel up). } |
| 301 |
property Input_ZoomIn: TInputShortcut read FInput_ZoomIn; |
| 302 |
|
| 303 |
{ Bring camera further from the model. Works only if @link(ZoomEnabled). |
| 304 |
By deafult mwDown (mouse wheel down). } |
| 305 |
property Input_ZoomOut: TInputShortcut read FInput_ZoomOut; |
| 306 |
published |
| 307 |
// By default this captures events from whole parent, which should be whole Viewport. |
| 308 |
property FullSize default true; |
| 309 |
|
| 310 |
// By default false, as this is invisible and would obscure viewport. |
| 311 |
property EditorSelectOnHover default false; |
| 312 |
|
| 313 |
{ Enable zooming in / out. |
| 314 |
Depending on the projection, zooming either moves camera or scales |
| 315 |
the projection size. |
| 316 |
When @false, no keys / mouse dragging / 3d mouse etc. can make a zoom. |
| 317 |
If @true, at least mouse wheel makes a zoom (som,e navigation methods |
| 318 |
may have additional ways to make zoom, they will all honor this property.) } |
| 319 |
property ZoomEnabled: Boolean read FZoomEnabled write FZoomEnabled default false; |
| 320 |
|
| 321 |
{ Check collisions when moving with the environment. |
| 322 |
|
| 323 |
Note: some descendants may ignore it for some operations. |
| 324 |
Right now, TCastleWalkNavigation checks is always, |
| 325 |
but TCastleExamineNavigation checks it only at zooming. |
| 326 |
But future engine versions may harden the collision checks (to make them always), |
| 327 |
so be sure to set CheckCollisions appropriately. } |
| 328 |
property CheckCollisions: Boolean read FCheckCollisions write FCheckCollisions default true; |
| 329 |
end; |
| 330 |
|
| 331 |
{ Navigate the 3D model in examine mode, like you would hold |
| 332 |
a box with the model inside. } |
| 333 |
TCastleExamineNavigation = class(TCastleNavigation) |
| 334 |
strict private |
| 335 |
type |
| 336 |
{ Camera pos/dir/up expressed as vectors more comfortable |
| 337 |
for Examine methods. } |
| 338 |
TExamineVectors = record |
| 339 |
Translation: TVector3; |
| 340 |
Rotations: TQuaternion; |
| 341 |
end; |
| 342 |
|
| 343 |
var |
| 344 |
FMoveEnabled: Boolean; |
| 345 |
FRotationEnabled: Boolean; |
| 346 |
FDragMoveSpeed, FKeysMoveSpeed: Single; |
| 347 |
FExactMovement: Boolean; |
| 348 |
{ Speed of rotations. Always zero when RotationAccelerate = false. |
| 349 |
|
| 350 |
This could be implemented as a quaternion, |
| 351 |
it even was implemented like this (and working!) for a couple |
| 352 |
of minutes. But this caused one problem: in Update, I want to |
| 353 |
apply FRotationsAnim to Rotations *scaled by SecondsPassed*. |
| 354 |
There's no efficient way with quaternions to say "take only SecondsPassed |
| 355 |
fraction of angle encoded in FRotationsAnim", AFAIK. |
| 356 |
The only way would be to convert FRotationsAnim back to AxisAngle, |
| 357 |
then scale angle, then convert back to quaternion... which makes |
| 358 |
the whole exercise useless. } |
| 359 |
FRotationsAnim: TVector3; |
| 360 |
FScaleFactorMin, FScaleFactorMax: Single; |
| 361 |
FRotationAccelerate: boolean; |
| 362 |
FRotationAccelerationSpeed: Single; |
| 363 |
FRotationSpeed: Single; |
| 364 |
FTurntable: boolean; |
| 365 |
FPinchGestureRecognizer: TCastlePinchPanGestureRecognizer; |
| 366 |
FCenterOfRotation: TVector3; |
| 367 |
FAutoCenterOfRotation: Boolean; |
| 368 |
FZoomSpeed: Single; |
| 369 |
|
| 370 |
FInputs_Move: T3BoolInputs; |
| 371 |
FInputs_Rotate: T3BoolInputs; |
| 372 |
FInput_ScaleLarger: TInputShortcut; |
| 373 |
FInput_ScaleSmaller: TInputShortcut; |
| 374 |
FInput_Home: TInputShortcut; |
| 375 |
FInput_StopRotating: TInputShortcut; |
| 376 |
FInput_Move: TInputShortcut; |
| 377 |
FInput_Rotate: TInputShortcut; |
| 378 |
FInput_Zoom: TInputShortcut; |
| 379 |
|
| 380 |
procedure SetRotationsAnim(const Value: TVector3); |
| 381 |
function GetRotations: TQuaternion; |
| 382 |
procedure SetRotations(const Value: TQuaternion); |
| 383 |
function GetTranslation: TVector3; |
| 384 |
procedure SetTranslation(const Value: TVector3); |
| 385 |
procedure SetRotationAccelerate(const Value: boolean); |
| 386 |
procedure OnGestureRecognized(Sender: TObject); |
| 387 |
|
| 388 |
function GetInput_MoveXInc: TInputShortcut; |
| 389 |
function GetInput_MoveXDec: TInputShortcut; |
| 390 |
function GetInput_MoveYInc: TInputShortcut; |
| 391 |
function GetInput_MoveYDec: TInputShortcut; |
| 392 |
function GetInput_MoveZInc: TInputShortcut; |
| 393 |
function GetInput_MoveZDec: TInputShortcut; |
| 394 |
function GetInput_RotateXInc: TInputShortcut; |
| 395 |
function GetInput_RotateXDec: TInputShortcut; |
| 396 |
function GetInput_RotateYInc: TInputShortcut; |
| 397 |
function GetInput_RotateYDec: TInputShortcut; |
| 398 |
function GetInput_RotateZInc: TInputShortcut; |
| 399 |
function GetInput_RotateZDec: TInputShortcut; |
| 400 |
|
| 401 |
function GetMouseNavigation: boolean; |
| 402 |
procedure SetMouseNavigation(const Value: boolean); |
| 403 |
|
| 404 |
{ Center of rotation and scale, relative to @link(Translation). |
| 405 |
In world coordinates. } |
| 406 |
function EffectiveCenterOfRotation: TVector3; |
| 407 |
|
| 408 |
function GetExamineVectors: TExamineVectors; |
| 409 |
procedure SetExamineVectors(const Value: TExamineVectors); |
| 410 |
property ExamineVectors: TExamineVectors read GetExamineVectors write SetExamineVectors; |
| 411 |
|
| 412 |
function GetMouseButtonRotate: TCastleMouseButton; |
| 413 |
procedure SetMouseButtonRotate(const Value: TCastleMouseButton); |
| 414 |
function GetMouseButtonMove: TCastleMouseButton; |
| 415 |
procedure SetMouseButtonMove(const Value: TCastleMouseButton); |
| 416 |
function GetMouseButtonZoom: TCastleMouseButton; |
| 417 |
procedure SetMouseButtonZoom(const Value: TCastleMouseButton); |
| 418 |
protected |
| 419 |
function Zoom(const Factor: Single): Boolean; override; |
| 420 |
public |
| 421 |
const |
| 422 |
DefaultRotationAccelerationSpeed = 5.0; |
| 423 |
DefaultRotationSpeed = 1.0; |
| 424 |
DefaultZoomSpeed = 1.0; |
| 425 |
|
| 426 |
constructor Create(AOwner: TComponent); override; |
| 427 |
destructor Destroy; override; |
| 428 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 429 |
procedure Update(const SecondsPassed: Single; |
| 430 |
var HandleInput: boolean); override; |
| 431 |
function AllowSuspendForInput: boolean; override; |
| 432 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 433 |
function Release(const Event: TInputPressRelease): boolean; override; |
| 434 |
function Motion(const Event: TInputMotion): boolean; override; |
| 435 |
|
| 436 |
function SensorTranslation(const X, Y, Z, Length: Double; const SecondsPassed: Single): boolean; override; |
| 437 |
function SensorRotation(const X, Y, Z, Angle: Double; const SecondsPassed: Single): boolean; override; |
| 438 |
|
| 439 |
{ Which input (like mouse button) should rotate the model. |
| 440 |
By default this is left mouse button. } |
| 441 |
property Input_Rotate: TInputShortcut read FInput_Rotate; |
| 442 |
|
| 443 |
{ Which input (like mouse button) should move the model. |
| 444 |
By default this is middle mouse button, or left mouse button while holding Shift. } |
| 445 |
property Input_Move: TInputShortcut read FInput_Move; |
| 446 |
|
| 447 |
{ Which input (like mouse button) should zoom (look closer / further at model). |
| 448 |
By default this is right mouse button, or left mouse button while holding Ctrl. } |
| 449 |
property Input_Zoom: TInputShortcut read FInput_Zoom; |
| 450 |
|
| 451 |
property MouseButtonRotate: TCastleMouseButton |
| 452 |
read GetMouseButtonRotate write SetMouseButtonRotate default buttonLeft; |
| 453 |
{$ifdef FPC}deprecated 'use Input_Rotate';{$endif} |
| 454 |
property MouseButtonMove: TCastleMouseButton |
| 455 |
read GetMouseButtonMove write SetMouseButtonMove default buttonMiddle; |
| 456 |
{$ifdef FPC}deprecated 'use Input_Move';{$endif} |
| 457 |
property MouseButtonZoom: TCastleMouseButton |
| 458 |
read GetMouseButtonZoom write SetMouseButtonZoom default buttonRight; |
| 459 |
{$ifdef FPC}deprecated 'use Input_Zoom';{$endif} |
| 460 |
|
| 461 |
{ Current rotation of the model. |
| 462 |
Rotation is done around ModelBox middle (with @link(Translation) added). } |
| 463 |
property Rotations: TQuaternion read GetRotations write SetRotations; |
| 464 |
|
| 465 |
{ Continuous rotation animation, applied each Update to Rotations. } |
| 466 |
property RotationsAnim: TVector3 read FRotationsAnim write SetRotationsAnim; |
| 467 |
|
| 468 |
{$ifdef FPC} |
| 469 |
property MoveAmount: TVector3 read GetTranslation write SetTranslation; |
| 470 |
deprecated 'use Translation'; |
| 471 |
{$endif} |
| 472 |
|
| 473 |
{ How much to move the model. By default, zero. } |
| 474 |
property Translation: TVector3 read GetTranslation write SetTranslation; |
| 475 |
|
| 476 |
{ Turntable rotates the scene around its Y axis instead of current camera axis. } |
| 477 |
property Turntable: boolean |
| 478 |
read FTurntable write FTurntable default false; |
| 479 |
|
| 480 |
property ScaleFactorMin: Single |
| 481 |
read FScaleFactorMin write FScaleFactorMin {$ifdef FPC}default 0.01{$endif}; |
| 482 |
{$ifdef FPC} deprecated 'this does nothing now; it was already only a limit in case of orthographic projection'; {$endif} |
| 483 |
property ScaleFactorMax: Single |
| 484 |
read FScaleFactorMax write FScaleFactorMax {$ifdef FPC}default 100.0{$endif}; |
| 485 |
{$ifdef FPC} deprecated 'this does nothing now; it was already only a limit in case of orthographic projection'; {$endif} |
| 486 |
|
| 487 |
{ Initialize most important properties of this class: |
| 488 |
sets ModelBox and goes to a nice view over the entire scene. |
| 489 |
|
| 490 |
In other words, this is just a shortcut to setting ModelBox, |
| 491 |
and setting suitable view by SetWorldView. } |
| 492 |
procedure Init(const AModelBox: TBox3D; const ARadius: Single); |
| 493 |
deprecated 'use Viewport.Camera.SetWorldView, and set GravityUp, ModelBox, Radius manually'; |
| 494 |
|
| 495 |
{ Methods performing navigation. |
| 496 |
Usually you want to just leave this for user to control. --------------- } |
| 497 |
|
| 498 |
{ Sets RotationsAnim to zero, stopping the rotation of the model. } |
| 499 |
function StopRotating: boolean; |
| 500 |
|
| 501 |
{ User inputs ------------------------------------------------------------ } |
| 502 |
|
| 503 |
{ Alternative ways to access Input_Move/Rotate(X|Y|Z)(Inc|Dec). |
| 504 |
Index the array (2nd index true means increase) instead of having |
| 505 |
to use the full identifier. |
| 506 |
@groupBegin } |
| 507 |
property Inputs_Move: T3BoolInputs read FInputs_Move; |
| 508 |
property Inputs_Rotate: T3BoolInputs read FInputs_Rotate; |
| 509 |
{ @groupEnd } |
| 510 |
|
| 511 |
{ TODO: Input_Xxx not published, although setting them in object inspector |
| 512 |
actually works Ok. They are not published, because they would be always |
| 513 |
stored in lfm (because each has different defaults, so they |
| 514 |
would be stored even if developer didn't touch them), |
| 515 |
and we may want to break compatibility here at some point |
| 516 |
(when implementing 3rd-person cameras). If they would be stored in lfm |
| 517 |
(always), breaking compatibility would be bad (causing errors |
| 518 |
when reading old lfm files about missing properties, |
| 519 |
*even if developer didn't customize any of these Input_Xxx properties*). |
| 520 |
Also, the defaults would be stored in lfm file. |
| 521 |
|
| 522 |
Until I am sure that this is how I want to presents inputs |
| 523 |
(see CastleInputs discussion about local vs global), |
| 524 |
better to keep it only in public. |
| 525 |
} |
| 526 |
{ } |
| 527 |
property Input_MoveXInc: TInputShortcut read GetInput_MoveXInc; |
| 528 |
property Input_MoveXDec: TInputShortcut read GetInput_MoveXDec; |
| 529 |
property Input_MoveYInc: TInputShortcut read GetInput_MoveYInc; |
| 530 |
property Input_MoveYDec: TInputShortcut read GetInput_MoveYDec; |
| 531 |
property Input_MoveZInc: TInputShortcut read GetInput_MoveZInc; |
| 532 |
property Input_MoveZDec: TInputShortcut read GetInput_MoveZDec; |
| 533 |
property Input_RotateXInc: TInputShortcut read GetInput_RotateXInc; |
| 534 |
property Input_RotateXDec: TInputShortcut read GetInput_RotateXDec; |
| 535 |
property Input_RotateYInc: TInputShortcut read GetInput_RotateYInc; |
| 536 |
property Input_RotateYDec: TInputShortcut read GetInput_RotateYDec; |
| 537 |
property Input_RotateZInc: TInputShortcut read GetInput_RotateZInc; |
| 538 |
property Input_RotateZDec: TInputShortcut read GetInput_RotateZDec; |
| 539 |
property Input_ScaleLarger: TInputShortcut read FInput_ScaleLarger; |
| 540 |
property Input_ScaleSmaller: TInputShortcut read FInput_ScaleSmaller; |
| 541 |
property Input_Home: TInputShortcut read FInput_Home; |
| 542 |
property Input_StopRotating: TInputShortcut read FInput_StopRotating; |
| 543 |
|
| 544 |
{$ifdef FPC} |
| 545 |
{ @Deprecated Include/exclude niMouseDragging from @link(Input) instead. } |
| 546 |
property MouseNavigation: boolean |
| 547 |
read GetMouseNavigation write SetMouseNavigation default true; deprecated; |
| 548 |
{$endif} |
| 549 |
|
| 550 |
{ 3D point around which we rotate, in world coordinates. |
| 551 |
This is used only when AutoCenterOfRotation = @false. } |
| 552 |
property CenterOfRotation: TVector3 read FCenterOfRotation write FCenterOfRotation; |
| 553 |
|
| 554 |
published |
| 555 |
{ Enable rotating the camera around the model by user input. |
| 556 |
When @false, no keys / mouse dragging / 3D mouse etc. can cause a rotation. |
| 557 |
|
| 558 |
Note that this doesn't prevent from rotating by code, e.g. by setting |
| 559 |
@link(Rotations) property or calling @link(TCastleTransform.SetWorldView Camera.SetWorldView). } |
| 560 |
property RotationEnabled: Boolean read FRotationEnabled write FRotationEnabled default true; |
| 561 |
|
| 562 |
{ Enable moving the camera by user input. |
| 563 |
When @false, no keys / mouse dragging / 3D mouse etc. can make a move. |
| 564 |
|
| 565 |
Note that this doesn't prevent from moving by code, e.g. by setting |
| 566 |
@link(Translation) property or calling @link(TCastleTransform.SetWorldView Camera.SetWorldView). } |
| 567 |
property MoveEnabled: Boolean read FMoveEnabled write FMoveEnabled default true; |
| 568 |
|
| 569 |
property ZoomEnabled default true; |
| 570 |
|
| 571 |
{ When @true, rotation keys make the rotation faster, and the model keeps |
| 572 |
rotating even when you don't hold any keys. When @false, you have to |
| 573 |
hold rotation keys to rotate. } |
| 574 |
property RotationAccelerate: boolean |
| 575 |
read FRotationAccelerate write SetRotationAccelerate default true; |
| 576 |
|
| 577 |
{ In orthographic projection with standard direction/up, |
| 578 |
move the camera exactly as many units as the mouse position change indicates. |
| 579 |
Makes the movemement in standard orthographic view most natural. } |
| 580 |
property ExactMovement: Boolean read FExactMovement write FExactMovement default true; |
| 581 |
|
| 582 |
{ Should we calculate center of rotation automatically (based on world bounding box) |
| 583 |
or use explicit @link(CenterOfRotation). } |
| 584 |
property AutoCenterOfRotation: Boolean read FAutoCenterOfRotation write FAutoCenterOfRotation default true; |
| 585 |
|
| 586 |
{ How fast user moves the scene by mouse/touch dragging. } |
| 587 |
property DragMoveSpeed: Single read FDragMoveSpeed write FDragMoveSpeed {$ifdef FPC}default 1.0{$endif}; |
| 588 |
|
| 589 |
{ How fast user moves the scene by pressing keys. } |
| 590 |
property KeysMoveSpeed: Single read FKeysMoveSpeed write FKeysMoveSpeed {$ifdef FPC}default 1.0{$endif}; |
| 591 |
|
| 592 |
{ Speed to change the rotation acceleration, |
| 593 |
used when RotationAccelerate = @true. } |
| 594 |
property RotationAccelerationSpeed: Single |
| 595 |
read FRotationAccelerationSpeed |
| 596 |
write FRotationAccelerationSpeed |
| 597 |
{$ifdef FPC}default DefaultRotationAccelerationSpeed{$endif}; |
| 598 |
|
| 599 |
{ Speed to change the rotation, used when RotationAccelerate = @false. } |
| 600 |
property RotationSpeed: Single |
| 601 |
read FRotationSpeed |
| 602 |
write FRotationSpeed |
| 603 |
{$ifdef FPC}default DefaultRotationSpeed{$endif}; |
| 604 |
|
| 605 |
{ Speed to change the Zoom, when ZoomEnabled = @true. } |
| 606 |
property ZoomSpeed: Single |
| 607 |
read FZoomSpeed |
| 608 |
write FZoomSpeed |
| 609 |
{$ifdef FPC}default DefaultZoomSpeed{$endif}; |
| 610 |
end; |
| 611 |
|
| 612 |
{ Navigation most suitable for 2D viewports |
| 613 |
(with orthographic projection and standard direction/up: -Z/+Y). } |
| 614 |
TCastle2DNavigation = class(TCastleExamineNavigation) |
| 615 |
public |
| 616 |
constructor Create(AOwner: TComponent); override; |
| 617 |
|
| 618 |
property MouseButtonMove default buttonLeft; |
| 619 |
property MouseButtonZoom default buttonMiddle; |
| 620 |
published |
| 621 |
property RotationEnabled default false; |
| 622 |
end; |
| 623 |
|
| 624 |
TCastleWalkNavigation = class; |
| 625 |
|
| 626 |
{ What mouse dragging does in TCastleWalkNavigation. } |
| 627 |
TMouseDragMode = ( |
| 628 |
{ Moves and rotates avatar depending on the direction of mouse drag. |
| 629 |
Default for @link(TCastleWalkNavigation.MouseDragMode). } |
| 630 |
mdWalkRotate, |
| 631 |
{ Rotates the head when mouse is moved. } |
| 632 |
mdRotate, |
| 633 |
{ Ignores the dragging. } |
| 634 |
mdNone |
| 635 |
); |
| 636 |
|
| 637 |
{ Abstract navigation class that can utilize @italic(mouse look), |
| 638 |
during which mouse cursor is hidden and we look at MouseLookDelta every frame. } |
| 639 |
TCastleMouseLookNavigation = class(TCastleNavigation) |
| 640 |
strict private |
| 641 |
FMouseLookHorizontalSensitivity: Single; |
| 642 |
FMouseLookVerticalSensitivity: Single; |
| 643 |
FInvertVerticalMouseLook: boolean; |
| 644 |
FMouseLook: boolean; |
| 645 |
procedure SetMouseLook(const Value: boolean); |
| 646 |
protected |
| 647 |
procedure ProcessMouseLookDelta(const Delta: TVector2); virtual; |
| 648 |
public |
| 649 |
const |
| 650 |
DefaultMouseLookHorizontalSensitivity = Pi * 0.1 / 180; |
| 651 |
DefaultMouseLookVerticalSensitivity = Pi * 0.1 / 180; |
| 652 |
|
| 653 |
constructor Create(AOwner: TComponent); override; |
| 654 |
procedure Update(const SecondsPassed: Single; |
| 655 |
var HandleInput: boolean); override; |
| 656 |
function Motion(const Event: TInputMotion): boolean; override; |
| 657 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 658 |
|
| 659 |
function InternalUsingMouseLook: Boolean; |
| 660 |
published |
| 661 |
{ Use mouse look to navigate (rotate the camera). |
| 662 |
|
| 663 |
This also makes mouse cursor of Container hidden, and forces |
| 664 |
mouse position to the middle of the window |
| 665 |
(to avoid the situation when mouse movement is blocked by screen borders). |
| 666 |
|
| 667 |
Setting this property at design-time (in CGE editor) does not activate |
| 668 |
the mouse look in CGE editor. |
| 669 |
It only controls the mouse look once the application is running. } |
| 670 |
property MouseLook: boolean read FMouseLook write SetMouseLook default false; |
| 671 |
|
| 672 |
{ Mouse look sensitivity, if @link(MouseLook) is working. |
| 673 |
These properties specify how much angle change is produced by moving mouse by 1 pixel. |
| 674 |
@groupBegin } |
| 675 |
property MouseLookHorizontalSensitivity: Single |
| 676 |
read FMouseLookHorizontalSensitivity write FMouseLookHorizontalSensitivity |
| 677 |
{$ifdef FPC}default DefaultMouseLookHorizontalSensitivity{$endif}; |
| 678 |
property MouseLookVerticalSensitivity: Single |
| 679 |
read FMouseLookVerticalSensitivity write FMouseLookVerticalSensitivity |
| 680 |
{$ifdef FPC}default DefaultMouseLookVerticalSensitivity{$endif}; |
| 681 |
{ @groupEnd } |
| 682 |
|
| 683 |
{ If this is @true and MouseLook works, then the meaning of vertical mouse |
| 684 |
movement is inverted: when user moves mouse up, he looks down. |
| 685 |
Some players are more comfortable with such configuration. } |
| 686 |
property InvertVerticalMouseLook: boolean |
| 687 |
read FInvertVerticalMouseLook write FInvertVerticalMouseLook |
| 688 |
default false; |
| 689 |
end; |
| 690 |
|
| 691 |
{ Navigation by walking or flying (classic first-person shooter navigation) |
| 692 |
in a 3D scene. |
| 693 |
User can rotate and move camera using various keys, like arrows or AWSD. |
| 694 |
Mouse dragging and mouse look are also supported. } |
| 695 |
TCastleWalkNavigation = class(TCastleMouseLookNavigation) |
| 696 |
strict private |
| 697 |
FRotationHorizontalSpeed, FRotationVerticalSpeed: Single; |
| 698 |
FRotationHorizontalPivot: Single; |
| 699 |
FPreferGravityUpForRotations: boolean; |
| 700 |
FPreferGravityUpForMoving: boolean; |
| 701 |
FIsAbove: boolean; |
| 702 |
FAboveHeight: Single; |
| 703 |
FAboveGround: PTriangle; |
| 704 |
FMouseDragMode: TMouseDragMode; |
| 705 |
FInput_Forward: TInputShortcut; |
| 706 |
FInput_Backward: TInputShortcut; |
| 707 |
FInput_RightRotate: TInputShortcut; |
| 708 |
FInput_LeftRotate: TInputShortcut; |
| 709 |
FInput_RightStrafe: TInputShortcut; |
| 710 |
FInput_LeftStrafe: TInputShortcut; |
| 711 |
FInput_UpRotate: TInputShortcut; |
| 712 |
FInput_DownRotate: TInputShortcut; |
| 713 |
FInput_IncreasePreferredHeight: TInputShortcut; |
| 714 |
FInput_DecreasePreferredHeight: TInputShortcut; |
| 715 |
FInput_GravityUp: TInputShortcut; |
| 716 |
FInput_MoveSpeedInc: TInputShortcut; |
| 717 |
FInput_MoveSpeedDec: TInputShortcut; |
| 718 |
FInput_Jump: TInputShortcut; |
| 719 |
FInput_Crouch: TInputShortcut; |
| 720 |
FInput_Run: TInputShortcut; |
| 721 |
|
| 722 |
FAllowSlowerRotations: boolean; |
| 723 |
FCheckModsDown: boolean; |
| 724 |
|
| 725 |
FMinAngleFromGravityUp: Single; |
| 726 |
|
| 727 |
{ This is initally false. It's used by MoveHorizontal while head bobbing, |
| 728 |
to avoid updating HeadBobbingPosition more than once in the same Update call. |
| 729 |
|
| 730 |
Updating it more than once is bad --- try e.g. holding Input_Forward |
| 731 |
with one of the strafe keys: you move and it's very noticeable |
| 732 |
that HeadBobbing seems faster. That's because |
| 733 |
when holding both Input_Forward and Input_StrafeRight, you shouldn't |
| 734 |
do HeadBobbing twice in one Update --- you should do it only Sqrt(2). |
| 735 |
When you will also hold Input_RotateRight at the same time --- situation |
| 736 |
gets a little complicated... |
| 737 |
|
| 738 |
The good solution seems to just do head bobbing only once. |
| 739 |
In some special cases this means that head bobbing will be done |
| 740 |
*less often* than it should be, but this doesn't hurt. } |
| 741 |
HeadBobbingAlreadyDone: boolean; |
| 742 |
|
| 743 |
{ MoveHorizontal call sets this to @true to indicate that some |
| 744 |
horizontal move was done. } |
| 745 |
MoveHorizontalDone: boolean; |
| 746 |
|
| 747 |
FMoveForward, FMoveBackward: boolean; |
| 748 |
FMoveHorizontalSpeed, FMoveVerticalSpeed, FMoveSpeed: Single; |
| 749 |
FMoveSpeedMin, FMoveSpeedMax: Single; |
| 750 |
FPreferredHeight: Single; |
| 751 |
FHeadBobbing: Single; |
| 752 |
FHeadBobbingTime: Single; |
| 753 |
FClimbHeight: Single; |
| 754 |
FCrouchHeight: Single; |
| 755 |
|
| 756 |
{ React to Input_MoveSpeedInc. } |
| 757 |
procedure MoveSpeedInc(const SecondsPassed: Single); |
| 758 |
{ React to Input_MoveSpeedDec. } |
| 759 |
procedure MoveSpeedDec(const SecondsPassed: Single); |
| 760 |
|
| 761 |
procedure RotateAroundGravityUp(const Angle: Single); |
| 762 |
procedure RotateAroundUp(const Angle: Single); |
| 763 |
procedure RotateHorizontal(const Angle: Single); |
| 764 |
procedure RotateVertical(AngleRad: Single); |
| 765 |
|
| 766 |
{ Move horizontally. |
| 767 |
Dir is in camera parent coordinates, like Camera.Direction. |
| 768 |
It will be automatically adjusted to be parallel to gravity plane, |
| 769 |
if PreferGravityUpForMoving. } |
| 770 |
procedure MoveHorizontal(Dir: TVector3; const SecondsPassed: Single); |
| 771 |
|
| 772 |
{ Up or down move, only when flying (ignored when @link(Gravity) is @true). } |
| 773 |
procedure MoveVertical(const SecondsPassed: Single; const Multiply: Integer); |
| 774 |
|
| 775 |
{ Call always after horizontal rotation change. |
| 776 |
This will return new Position, applying effect of RotationHorizontalPivot. |
| 777 |
The OldPosition, OldDirection, NewDirection must be in world coordinates, |
| 778 |
so is the result. } |
| 779 |
function AdjustPositionForRotationHorizontalPivot( |
| 780 |
const OldPosition: TVector3; |
| 781 |
const OldDirection, NewDirection: TVector3): TVector3; |
| 782 |
|
| 783 |
{ Jump. |
| 784 |
|
| 785 |
Returns if a jump was actually done. For example, you cannot |
| 786 |
jump when there's no gravity, or you're already in the middle |
| 787 |
of the jump. Can be useful to determine if key was handled and such. } |
| 788 |
function Jump: boolean; |
| 789 |
|
| 790 |
{ Camera.GravityUp expressed in camera parent coordinate system. } |
| 791 |
function GravityUpLocal: TVector3; |
| 792 |
|
| 793 |
{ Direction to strafe left. |
| 794 |
In camera parent coordinate space, just like Camera.Direction. |
| 795 |
This is not adjusted using PreferGravityUpForMoving/Rotations. } |
| 796 |
function DirectionLeft: TVector3; |
| 797 |
|
| 798 |
{ Direction to strafe right. |
| 799 |
In camera parent coordinate space, just like Camera.Direction. |
| 800 |
This is not adjusted using PreferGravityUpForMoving/Rotations. } |
| 801 |
function DirectionRight: TVector3; |
| 802 |
private |
| 803 |
{ Private things related to gravity ---------------------------- } |
| 804 |
|
| 805 |
FFalling: boolean; |
| 806 |
FFallingStartPosition: TVector3; |
| 807 |
FFallSpeedStart: Single; |
| 808 |
FFallSpeed: Single; |
| 809 |
FFallSpeedIncrease: Single; |
| 810 |
FGravity: boolean; |
| 811 |
FGrowSpeed: Single; |
| 812 |
{ This is used by FallingEffect to temporary modify Matrix result |
| 813 |
by rotating Up around Direction. In degress. } |
| 814 |
Fde_UpRotate: Single; |
| 815 |
{ This is used by FallingEffect to consistently rotate us. |
| 816 |
This is either -1, 0 or +1. } |
| 817 |
Fde_RotateHorizontal: Integer; |
| 818 |
FFallingEffect: boolean; |
| 819 |
|
| 820 |
FJumpMaxHeight: Single; |
| 821 |
FIsJumping: boolean; |
| 822 |
FJumpHeight: Single; |
| 823 |
FJumpTime: Single; |
| 824 |
FJumpHorizontalSpeedMultiply: Single; |
| 825 |
|
| 826 |
HeadBobbingPosition: Single; |
| 827 |
function UseHeadBobbing: boolean; |
| 828 |
class procedure CreateComponentFly(Sender: TObject); |
| 829 |
private |
| 830 |
FIsCrouching: boolean; |
| 831 |
|
| 832 |
FFallingOnTheGround: boolean; |
| 833 |
FFallingOnTheGroundAngleIncrease: boolean; |
| 834 |
|
| 835 |
FIsOnTheGround: boolean; |
| 836 |
FIsWalkingOnTheGround: boolean; |
| 837 |
|
| 838 |
FMouseDraggingHorizontalRotationSpeed, FMouseDraggingVerticalRotationSpeed: Single; |
| 839 |
FMouseDraggingMoveSpeed: Single; |
| 840 |
|
| 841 |
function RealPreferredHeightNoHeadBobbing: Single; |
| 842 |
function RealPreferredHeightMargin: Single; |
| 843 |
protected |
| 844 |
function ReallyEnableMouseDragging: boolean; override; |
| 845 |
procedure ProcessMouseLookDelta(const Delta: TVector2); override; |
| 846 |
function MoveAllowed( |
| 847 |
const OldPos: TVector3; ProposedNewPos: TVector3; out NewPos: TVector3; |
| 848 |
const BecauseOfGravity, CheckClimbHeight: Boolean): Boolean; override; |
| 849 |
public |
| 850 |
const |
| 851 |
DefaultFallSpeedStart = 0.5; |
| 852 |
DefaultGrowSpeed = 1.0; |
| 853 |
DefaultJumpMaxHeight = 1.0; |
| 854 |
DefaultMinAngleFromGravityUp = Pi * 10 / 180; |
| 855 |
DefaultRotationHorizontalSpeed = Pi * 150 / 180; |
| 856 |
DefaultRotationVerticalSpeed = Pi * 100 / 180; |
| 857 |
DefaultFallSpeedIncrease = 13/12; |
| 858 |
DefaultJumpHorizontalSpeedMultiply = 2.0; |
| 859 |
DefaultJumpTime = 1.0 / 8.0; |
| 860 |
DefaultMouseDraggingHorizontalRotationSpeed = Pi * 0.1 / 180; |
| 861 |
DefaultMouseDraggingVerticalRotationSpeed = Pi * 0.1 / 180; |
| 862 |
DefaultMouseDraggingMoveSpeed = 0.01; |
| 863 |
DefaultMoveSpeedMin = 0.01; |
| 864 |
DefaultMoveSpeedMax = 10000.0; |
| 865 |
|
| 866 |
constructor Create(AOwner: TComponent); override; |
| 867 |
destructor Destroy; override; |
| 868 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 869 |
procedure Update(const SecondsPassed: Single; |
| 870 |
var HandleInput: boolean); override; |
| 871 |
function AllowSuspendForInput: boolean; override; |
| 872 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 873 |
function SensorTranslation(const X, Y, Z, Length: Double; const SecondsPassed: Single): boolean; override; |
| 874 |
function SensorRotation(const X, Y, Z, Angle: Double; const SecondsPassed: Single): boolean; override; |
| 875 |
|
| 876 |
{ If PreferGravityUpForRotations or PreferGravityUpForMoving |
| 877 |
then various operations are done with respect |
| 878 |
to GravityUp, otherwise they are done with |
| 879 |
respect to current @link(TCastleTransform.Up Camera.Up). |
| 880 |
|
| 881 |
With PreferGravityUpForRotations, this affects rotations: |
| 882 |
horizontal rotations (Input_LeftRotate and Input_RightRotate) |
| 883 |
and rotations caused by MouseLook. |
| 884 |
Also vertical rotations are bounded by MinAngleFromGravityUp |
| 885 |
when PreferGravityUpForRotations. |
| 886 |
|
| 887 |
Note that you can change it freely at runtime, |
| 888 |
and when you set PreferGravityUpForRotations from @false to @true |
| 889 |
then in nearest Update |
| 890 |
the @link(TCastleTransform.Up Camera.Up) will be gradually fixed, |
| 891 |
so that @link(TCastleTransform.Direction Camera.Direction) and @link(TCastleTransform.Up Camera.Up) |
| 892 |
and GravityUp are on the same plane. Also @link(TCastleTransform.Direction Camera.Direction) may be adjusted |
| 893 |
to honour MinAngleFromGravityUp. |
| 894 |
|
| 895 |
With PreferGravityUpForMoving, this affects moving: |
| 896 |
horizontal moving (forward, backward, strafe), |
| 897 |
and vertical moving (Input_Jump and Input_Crouch when @link(Gravity) is @false). |
| 898 |
E.g. when PreferGravityUpForMoving then forward/backward keys are tied |
| 899 |
to horizontal plane defined by GravityUp. |
| 900 |
When not PreferGravityUpForMoving then forward/backward try to move |
| 901 |
you just in the @link(TCastleTransform.Direction Camera.Direction). Which is usually more handy when |
| 902 |
e.g. simulating flying. |
| 903 |
|
| 904 |
@unorderedList( |
| 905 |
@item( |
| 906 |
When there is no "natural" up-or-down feeling in the scene, |
| 907 |
e.g. outer space environment without any gravity, |
| 908 |
then you @bold(may) set |
| 909 |
PreferGravityUpForRotations as @false and you @bold(should) |
| 910 |
leave PreferGravityUpForMoving and @link(Gravity) to @false. |
| 911 |
) |
| 912 |
|
| 913 |
@item( |
| 914 |
With PreferGravityUpForRotations the "feeling" of GravityUp |
| 915 |
is stronger. Raising/bowing the head doesn't mess with "the general |
| 916 |
sense that there's some vertical axis independent of my movement, |
| 917 |
that doesn't change, and affects how I move". |
| 918 |
|
| 919 |
Without PreferGravityUpForRotations, we quickly start to do rotations |
| 920 |
in an awkward way --- once you do some vertical rotation, |
| 921 |
you changed @link(TCastleTransform.Up Camera.Up), and next horizontal rotation will be |
| 922 |
done versus new @link(TCastleTransform.Up Camera.Up). |
| 923 |
|
| 924 |
If your GravityUp is good, then you generally should |
| 925 |
leave PreferGravityUpForRotations to @true. Unless you really @bold(want) |
| 926 |
the player to feel movements as "awkward", e.g. when you |
| 927 |
want to simulate this "outer space without any gravity" feeling.) |
| 928 |
|
| 929 |
@item( |
| 930 |
If your GravityUp is good, then you generally should set |
| 931 |
PreferGravityUpForMoving just like Gravity. |
| 932 |
|
| 933 |
E.g. when the player is flying / swimming etc. he will probably prefer |
| 934 |
PreferGravityUpForMoving = @false, because this way he will not have to |
| 935 |
press Input_Jump and Input_Crouch. Simply pressing Input_Forward |
| 936 |
and Input_Backward and doing rotations will be enough to move |
| 937 |
freely in 3D space. |
| 938 |
|
| 939 |
When gravity works, PreferGravityUpForMoving = @true is better, |
| 940 |
otherwise player would unnecessarily try to jump when looking up.) |
| 941 |
) |
| 942 |
|
| 943 |
@groupBegin } |
| 944 |
property PreferGravityUpForRotations: boolean |
| 945 |
read FPreferGravityUpForRotations write FPreferGravityUpForRotations default true; |
| 946 |
|
| 947 |
property PreferGravityUpForMoving: boolean |
| 948 |
read FPreferGravityUpForMoving write FPreferGravityUpForMoving default true; |
| 949 |
{ @groupEnd } |
| 950 |
|
| 951 |
{ Set the most important properties of this navigation, in one call. |
| 952 |
Sets camera properties (Translation, Direction, Up). |
| 953 |
|
| 954 |
Given here AInitialDirection, AInitialUp, AGravityUp will be normalized, |
| 955 |
and AInitialUp will be adjusted to be orthogonal to AInitialDirection |
| 956 |
(see @link(TCastleTransform.SetWorldView Camera.SetWorldView)). |
| 957 |
|
| 958 |
Sets also PreferredHeight and Radius. |
| 959 |
PreferredHeight may be adjusted to be sensible |
| 960 |
(by calling CorrectPreferredHeight(ARadius)). |
| 961 |
You can pass ARadius = 0.0 if you really don't want this |
| 962 |
PreferredHeight adjustment. } |
| 963 |
procedure Init(const AInitialPosition, AInitialDirection, |
| 964 |
AInitialUp: TVector3; |
| 965 |
const AGravityUp: TVector3; |
| 966 |
const APreferredHeight: Single; |
| 967 |
const ARadius: Single); overload; |
| 968 |
deprecated 'use Viewport.Camera.SetWorldView, and set GravityUp, PreferredHeight, Radius and call CorrectPreferredHeight manually'; |
| 969 |
|
| 970 |
{ Alternative Init that sets camera properties such that |
| 971 |
an object inside Box is more or less "visible good". |
| 972 |
Sets InitialCameraXxx properties to make it look right, |
| 973 |
sets current CameraXxx properties to InitialCameraXxx. |
| 974 |
Sets GravityUp to the same thing as InitialUp. |
| 975 |
Sets also PreferredHeight to make it behave "sensibly". } |
| 976 |
procedure Init(const box: TBox3D; const ARadius: Single); overload; |
| 977 |
deprecated 'use Viewport.Camera.SetWorldView, and set GravityUp, PreferredHeight, Radius and call CorrectPreferredHeight manually'; |
| 978 |
|
| 979 |
{ This sets the minimal angle (in radians) between GravityUp |
| 980 |
and @link(TCastleTransform.Direction Camera.Direction), and also between -GravityUp and |
| 981 |
@link(TCastleTransform.Direction Camera.Direction). |
| 982 |
This way vertical rotations (like Input_UpRotate, |
| 983 |
Input_DownRotate) are "bounded" to not allow player to do something |
| 984 |
strange, i.e. bow your head too much and raise your head too much. |
| 985 |
|
| 986 |
This is used only when PreferGravityUpForRotations |
| 987 |
is @true and when it's <> 0.0. |
| 988 |
|
| 989 |
This must be always between 0 and Pi/2. Value of Pi/2 will effectively |
| 990 |
disallow vertical rotations (although you should rather do this in |
| 991 |
a "cleaner way" by calling MakeClear on Input_UpRotate and Input_DownRotate). } |
| 992 |
property MinAngleFromGravityUp: Single |
| 993 |
read FMinAngleFromGravityUp write FMinAngleFromGravityUp |
| 994 |
{$ifdef FPC}default DefaultMinAngleFromGravityUp{$endif}; |
| 995 |
|
| 996 |
function DirectionInGravityPlane: TVector3; deprecated 'avoid using it, as it inherently has difficult cases: it is in TCastleCamera local coordinate space, it cannot be correct when Direction is parallel to gravity'; |
| 997 |
|
| 998 |
function Motion(const Event: TInputMotion): boolean; override; |
| 999 |
|
| 1000 |
{ Initial speed of falling down. |
| 1001 |
Of course this is used only when @link(Gravity) is true. |
| 1002 |
|
| 1003 |
Note that while falling down, |
| 1004 |
the camera will actually fall with greater and greated speed |
| 1005 |
(this adds more realism to the gravity effect...). |
| 1006 |
Note that this is always relative to @link(TCastleTransform.Direction Camera.Direction) length. |
| 1007 |
@link(TCastleTransform.Direction Camera.Direction) determines moving speed --- and so it determines |
| 1008 |
also falling speed. The default DefaultFallSpeedStart |
| 1009 |
is chosen to be something sensible, to usually get nice effect |
| 1010 |
of falling. |
| 1011 |
|
| 1012 |
You can change it at any time, but note that if you change this |
| 1013 |
while Falling is @true, then you will not change the |
| 1014 |
"current falling down speed". You will change only the falling down |
| 1015 |
speed used the next time. } |
| 1016 |
property FallSpeedStart: Single |
| 1017 |
read FFallSpeedStart write FFallSpeedStart |
| 1018 |
{$ifdef FPC}default DefaultFallSpeedStart{$endif}; |
| 1019 |
|
| 1020 |
{ When falling down, the speed increases. |
| 1021 |
Set this to 1.0 to fall down with constant speed |
| 1022 |
(taken from FallSpeedStart). } |
| 1023 |
property FallSpeedIncrease: Single |
| 1024 |
read FFallSpeedIncrease write FFallSpeedIncrease |
| 1025 |
{$ifdef FPC}default DefaultFallSpeedIncrease{$endif}; |
| 1026 |
|
| 1027 |
{ Are we currently falling down because of gravity. } |
| 1028 |
property Falling: boolean read FFalling write FFalling; |
| 1029 |
|
| 1030 |
{ If Falling, then this will force Falling to false |
| 1031 |
@bold(without calling OnFallenDown). It's much like forcing |
| 1032 |
the opinion that "camera is not falling down right now". |
| 1033 |
|
| 1034 |
Note that if we will find out (e.g. in nearest @link(Update)) |
| 1035 |
that camera is still too high above the ground, |
| 1036 |
then we will start falling down again, setting @link(Falling) |
| 1037 |
back to true. (but then we will start falling down from the beginning |
| 1038 |
with initial falling down speed). |
| 1039 |
|
| 1040 |
This is useful to call if you just changed @link(TCastleTransform.Translation Camera.Translation) because |
| 1041 |
e.g. the player teleported somewhere (or e.g. game levels changed). |
| 1042 |
In this case you just want to forget the fact that camera |
| 1043 |
was falling down --- no consequences (like lowering player's |
| 1044 |
health, fadeout etc.). } |
| 1045 |
procedure CancelFalling; |
| 1046 |
|
| 1047 |
{ Make a nice dizzying camera effect when falling down. |
| 1048 |
This adds temporary camera rotations simulating that you |
| 1049 |
rotate randomly and helplessly when falling down. |
| 1050 |
|
| 1051 |
Of course this is meaningfull only when @link(Gravity) works. |
| 1052 |
|
| 1053 |
Note that changing it from @true to @false doesn't immediately |
| 1054 |
"cancel out" this effect if it's currently in progress. |
| 1055 |
It only prevents this effect from starting again. } |
| 1056 |
property FallingEffect: boolean |
| 1057 |
read FFallingEffect write FFallingEffect default true; |
| 1058 |
|
| 1059 |
{ When @link(Gravity) works and camera height above the ground |
| 1060 |
is less than PreferredHeight, then we try to "grow", |
| 1061 |
i.e. camera position increases along the GravityUp |
| 1062 |
so that camera height above the ground is closer to |
| 1063 |
PreferredHeight. This property (together with length of |
| 1064 |
@link(TCastleTransform.Direction Camera.Direction), that always determines every moving speed) |
| 1065 |
determines the speed of this growth. } |
| 1066 |
property GrowSpeed: Single |
| 1067 |
read FGrowSpeed write FGrowSpeed |
| 1068 |
{$ifdef FPC}default DefaultGrowSpeed{$endif}; |
| 1069 |
|
| 1070 |
{ How high can you jump ? |
| 1071 |
The max jump distance is calculated as |
| 1072 |
JumpMaxHeight * PreferredHeight, see MaxJumpDistance. } |
| 1073 |
property JumpMaxHeight: Single |
| 1074 |
read FJumpMaxHeight write FJumpMaxHeight |
| 1075 |
{$ifdef FPC}default DefaultJumpMaxHeight{$endif}; |
| 1076 |
|
| 1077 |
{ Returns just JumpMaxHeight * PreferredHeight, |
| 1078 |
see JumpMaxHeight for explanation. } |
| 1079 |
function MaxJumpDistance: Single; |
| 1080 |
|
| 1081 |
{ We are in the middle of a "jump" move right now. } |
| 1082 |
property IsJumping: boolean read FIsJumping; |
| 1083 |
|
| 1084 |
{ Scales the speed of horizontal moving during jump. } |
| 1085 |
property JumpHorizontalSpeedMultiply: Single |
| 1086 |
read FJumpHorizontalSpeedMultiply write FJumpHorizontalSpeedMultiply |
| 1087 |
{$ifdef FPC}default DefaultJumpHorizontalSpeedMultiply{$endif}; |
| 1088 |
|
| 1089 |
{ How fast do you jump up. This is the time, in seconds, in takes |
| 1090 |
to reach MaxJumpDistance height when jumping. } |
| 1091 |
property JumpTime: Single read FJumpTime write FJumpTime |
| 1092 |
{$ifdef FPC}default DefaultJumpTime{$endif}; |
| 1093 |
|
| 1094 |
{ Is player crouching right now. } |
| 1095 |
property IsCrouching: boolean read FIsCrouching; |
| 1096 |
|
| 1097 |
{ The PreferredHeight slightly modified by head bobbing |
| 1098 |
and crouch. It can be useful for collision detection |
| 1099 |
between camera and something. } |
| 1100 |
function RealPreferredHeight: Single; |
| 1101 |
|
| 1102 |
{ This makes a visual effect of camera falling down horizontally |
| 1103 |
on the ground. Nice to use when player died, and you want to show |
| 1104 |
that it's body falled on the ground. |
| 1105 |
This works by gradually changing @link(TCastleTransform.Up Camera.Up) such that |
| 1106 |
it gets orthogonal to GravityUp. } |
| 1107 |
procedure FallOnTheGround; |
| 1108 |
|
| 1109 |
{ @true when the effect caused by FallOnTheGround is stil in motion. } |
| 1110 |
property FallingOnTheGround: boolean read FFallingOnTheGround; |
| 1111 |
|
| 1112 |
{ This is @true when gravity works (that is @link(Gravity) is @true), |
| 1113 |
and player is standing stable on the ground. This is set in every Update. |
| 1114 |
|
| 1115 |
You can use this e.g. to make some effects when player is on some |
| 1116 |
special ground (standing or walking), e.g. hurt player when he's |
| 1117 |
standing on some toxical ground. |
| 1118 |
|
| 1119 |
@seealso IsWalkingOnTheGround } |
| 1120 |
property IsOnTheGround: boolean read FIsOnTheGround; |
| 1121 |
|
| 1122 |
{ This is @true when gravity works (that is @link(Gravity) is @true), |
| 1123 |
and player is standing stable on the ground, and player is moving |
| 1124 |
horizontally. In other words, this is like "IsOnTheGround and (s)he's |
| 1125 |
walking". This is set in every Update. |
| 1126 |
|
| 1127 |
The intention is that you can use this to make |
| 1128 |
some "footsteps" sound for the player. } |
| 1129 |
property IsWalkingOnTheGround: boolean read FIsWalkingOnTheGround; |
| 1130 |
|
| 1131 |
{ Change up vector, keeping the direction unchanged. |
| 1132 |
If necessary, the up vector provided here will be fixed to be orthogonal |
| 1133 |
to direction. |
| 1134 |
See TCastleTransform.UpPrefer for detailed documentation what this does. } |
| 1135 |
procedure UpPrefer(const AUp: TVector3); deprecated 'use Viewport.Camera.UpPrefer'; |
| 1136 |
|
| 1137 |
{ Last known information about whether camera is over the ground. |
| 1138 |
|
| 1139 |
These are updated continuously only when @link(Gravity) is @true. |
| 1140 |
|
| 1141 |
We do not (and, currently, cannot) track here if |
| 1142 |
AboveGround pointer will be eventually released (which may happen |
| 1143 |
if you release your 3D scene, or rebuild scene causing octree rebuild). |
| 1144 |
This is not a problem for navigation class, since we do not use this |
| 1145 |
pointer for anything. But if you use this pointer, |
| 1146 |
then you may want to take care to eventually set it to @nil when |
| 1147 |
your octree or such is released. |
| 1148 |
|
| 1149 |
AboveHeight is in world coordinates (not camera coordinates). |
| 1150 |
|
| 1151 |
@groupBegin } |
| 1152 |
property IsAbove: boolean read FIsAbove; |
| 1153 |
property AboveHeight: Single read FAboveHeight; |
| 1154 |
property AboveGround: PTriangle read FAboveGround write FAboveGround; |
| 1155 |
{ @groupEnd } |
| 1156 |
|
| 1157 |
{ TODO: Input_Xxx not published. See TCastleExamineNavigation Input_Xxx notes |
| 1158 |
for reasoning. } |
| 1159 |
{ } |
| 1160 |
property Input_Forward: TInputShortcut read FInput_Forward; |
| 1161 |
property Input_Backward: TInputShortcut read FInput_Backward; |
| 1162 |
property Input_LeftRotate: TInputShortcut read FInput_LeftRotate; |
| 1163 |
property Input_RightRotate: TInputShortcut read FInput_RightRotate; |
| 1164 |
{$ifdef FPC} |
| 1165 |
property Input_LeftRot: TInputShortcut read FInput_LeftRotate; deprecated 'use Input_LeftRotate'; |
| 1166 |
property Input_RightRot: TInputShortcut read FInput_RightRotate; deprecated 'use Input_RightRotate'; |
| 1167 |
{$endif} |
| 1168 |
property Input_LeftStrafe: TInputShortcut read FInput_LeftStrafe; |
| 1169 |
property Input_RightStrafe: TInputShortcut read FInput_RightStrafe; |
| 1170 |
property Input_UpRotate: TInputShortcut read FInput_UpRotate; |
| 1171 |
property Input_DownRotate: TInputShortcut read FInput_DownRotate; |
| 1172 |
property Input_IncreasePreferredHeight: TInputShortcut read FInput_IncreasePreferredHeight; |
| 1173 |
property Input_DecreasePreferredHeight: TInputShortcut read FInput_DecreasePreferredHeight; |
| 1174 |
property Input_GravityUp: TInputShortcut read FInput_GravityUp; |
| 1175 |
property Input_Run: TInputShortcut read FInput_Run; |
| 1176 |
|
| 1177 |
{ Change the MoveSpeed. |
| 1178 |
@groupBegin } |
| 1179 |
property Input_MoveSpeedInc: TInputShortcut read FInput_MoveSpeedInc; |
| 1180 |
property Input_MoveSpeedDec: TInputShortcut read FInput_MoveSpeedDec; |
| 1181 |
{ @groupEnd } |
| 1182 |
|
| 1183 |
{ Jumping and crouching (when @link(Gravity) = @true) or flying up / down |
| 1184 |
(when @link(Gravity) = @false). |
| 1185 |
@groupBegin } |
| 1186 |
property Input_Jump: TInputShortcut read FInput_Jump; |
| 1187 |
property Input_Crouch: TInputShortcut read FInput_Crouch; |
| 1188 |
{ @groupEnd } |
| 1189 |
|
| 1190 |
{ Move forward, just like Input_Forward would be pressed. } |
| 1191 |
property MoveForward: boolean read FMoveForward write FMoveForward; |
| 1192 |
{ Move backward, just like Input_Backward would be pressed. } |
| 1193 |
property MoveBackward: boolean read FMoveBackward write FMoveBackward; |
| 1194 |
|
| 1195 |
{ If @true then all rotation keys |
| 1196 |
(Input_RightRotate, Input_LeftRotate, Input_UpRotate, Input_DownRotate) |
| 1197 |
will work 10x slower when Ctrl modified is pressed. } |
| 1198 |
property AllowSlowerRotations: boolean |
| 1199 |
read FAllowSlowerRotations write FAllowSlowerRotations |
| 1200 |
default true; |
| 1201 |
|
| 1202 |
{ @abstract(Do we check what key modifiers are pressed and do something |
| 1203 |
differently based on it?) |
| 1204 |
|
| 1205 |
If @true then all keys work only when no modifiers or only shift are |
| 1206 |
pressed. Additionally when Ctrl is pressed (and AllowSlowerRotations) then |
| 1207 |
rotation keys work 10x slower. Also Increase/DecreasePreferredHeight |
| 1208 |
work only when Ctrl pressed. |
| 1209 |
Other keys with other modifiers |
| 1210 |
don't work. We allow shift, because to press character "+" on non-numpad |
| 1211 |
keyboard (useful on laptops, where numpad is difficult) you |
| 1212 |
probably need to press shift. |
| 1213 |
|
| 1214 |
If @false then all keys work as usual, no matter what |
| 1215 |
modifiers are pressed. And rotation keys never work 10x slower |
| 1216 |
(AllowSlowerRotations is ignored), |
| 1217 |
also Increase/DecreasePreferredHeight are ignored. } |
| 1218 |
property CheckModsDown: boolean |
| 1219 |
read FCheckModsDown write FCheckModsDown |
| 1220 |
default true; |
| 1221 |
|
| 1222 |
{ Horizontal rotation can rotate around a vector that is RotationHorizontalPivot units |
| 1223 |
forward before the camera. This is a poor-mans way to implement some 3rd camera game. |
| 1224 |
Note that when non-zero this may (for now) move the camera without actually checking |
| 1225 |
OnMoveAllowed. } |
| 1226 |
property RotationHorizontalPivot: Single |
| 1227 |
read FRotationHorizontalPivot write FRotationHorizontalPivot |
| 1228 |
{$ifdef FPC}default 0{$endif}; |
| 1229 |
{$ifdef FPC}deprecated 'use TCastleThirdPersonNavigation for real 3rd-person navigation';{$endif} |
| 1230 |
|
| 1231 |
{ Correct PreferredHeight based on @link(Radius) |
| 1232 |
and on current @link(HeadBobbing). |
| 1233 |
|
| 1234 |
Exactly what and why is done: if you do any kind of collision |
| 1235 |
detection with some Radius, then |
| 1236 |
you should make sure that RealPreferredHeight is always >= of your |
| 1237 |
Radius, otherwise strange effects may happen when crouching |
| 1238 |
or when head bobbing forces camera to go down. |
| 1239 |
|
| 1240 |
Exactly, the required equation is |
| 1241 |
|
| 1242 |
@preformatted( |
| 1243 |
MinimumRealPreferredHeight := |
| 1244 |
PreferredHeight * CrouchHeight * (1 - HeadBobbing); |
| 1245 |
) |
| 1246 |
|
| 1247 |
and always must be |
| 1248 |
|
| 1249 |
@preformatted( |
| 1250 |
MinimumRealPreferredHeight >= RealPreferredHeight |
| 1251 |
) |
| 1252 |
|
| 1253 |
Reasoning: otherwise this class would "want camera to fall down" |
| 1254 |
(because we will always be higher than RealPreferredHeight) |
| 1255 |
but your OnMoveAllowed would not allow it (because Radius |
| 1256 |
would not allow it). |
| 1257 |
|
| 1258 |
This method will make sure that this condition |
| 1259 |
holds by eventually adjusting (making larger) PreferredHeight. |
| 1260 |
Note that for Radius = 0.0 this will always leave |
| 1261 |
PreferredHeight as it is. } |
| 1262 |
procedure CorrectPreferredHeight; |
| 1263 |
|
| 1264 |
{ We may make a "head bobbing" effect, |
| 1265 |
by moving the camera a bit up and down. |
| 1266 |
|
| 1267 |
This property mutiplied by PreferredHeight |
| 1268 |
says how much head bobbing can move you along GravityUp. |
| 1269 |
Set this to 0 to disable head bobbing. |
| 1270 |
This must always be < 1.0. For sensible effects, this should |
| 1271 |
be rather close to 0.0, for example 0.02. |
| 1272 |
|
| 1273 |
This is meaningfull only when @link(TCastleWalkNavigation.Gravity) works. } |
| 1274 |
property HeadBobbing: Single |
| 1275 |
read FHeadBobbing write FHeadBobbing {$ifdef FPC}default DefaultHeadBobbing{$endif}; |
| 1276 |
|
| 1277 |
{ Controls head bobbing frequency. In the time of HeadBobbingTime seconds, |
| 1278 |
we do full head bobbing sequence (camera swing up, then down again). |
| 1279 |
|
| 1280 |
Note that if you do a footsteps sound in your game (see |
| 1281 |
stPlayerFootstepsDefault or TMaterialProperty.FootstepsSound) |
| 1282 |
then you will want this property to match your footsteps sound length, |
| 1283 |
things feel and sound natural then. |
| 1284 |
Also, often it sounds better to record two footsteps inside |
| 1285 |
a single sound file, in which case the footstep sound length should be twice |
| 1286 |
as long as this property. For example, record 2 steps inside a 1-second long |
| 1287 |
footstep sound, and set this property to 0.5 a second (which is a default |
| 1288 |
in fact). } |
| 1289 |
property HeadBobbingTime: Single |
| 1290 |
read FHeadBobbingTime write FHeadBobbingTime |
| 1291 |
{$ifdef FPC}default DefaultHeadBobbingTime{$endif}; |
| 1292 |
|
| 1293 |
{ The tallest height that you can climb only used |
| 1294 |
when @link(TCastleWalkNavigation.Gravity) is @true. |
| 1295 |
This is checked in each single horizontal move when @link(TCastleWalkNavigation.Gravity) works. |
| 1296 |
Must be >= 0. Value 0 means there is no limit (and makes a small speedup). |
| 1297 |
|
| 1298 |
This is reliable to prevent user from climbing stairs and such, |
| 1299 |
when vertical walls are really vertical (not just steep-almost-vertical). |
| 1300 |
|
| 1301 |
It's not 100% reliable to prevent player from climbing steep hills. |
| 1302 |
That's because, depending on how often an event processing occurs, |
| 1303 |
you actually climb using less or more steps. |
| 1304 |
So even a very steep hill can be always |
| 1305 |
climbed on a computer with very fast speed, because with large FPS you |
| 1306 |
effectively climb it using a lot of very small steps (assuming that |
| 1307 |
FPS limit is not enabled, that is CastleWindow.TCastleApplication.LimitFPS |
| 1308 |
or CastleControl.LimitFPS is zero). |
| 1309 |
|
| 1310 |
Remember that user can still try jumping to climb on high obstactes. |
| 1311 |
See @link(TCastleWalkNavigation.JumpMaxHeight) for a way to control jumping. |
| 1312 |
|
| 1313 |
For a 100% reliable way to prevent user from reaching some point, |
| 1314 |
that does not rely on specific navigation settings, |
| 1315 |
you should build actual walls in 3D (invisible walls |
| 1316 |
can be created by Collision.proxy in VRML/X3D). } |
| 1317 |
property ClimbHeight: Single read FClimbHeight write FClimbHeight; |
| 1318 |
published |
| 1319 |
property MouseLook; |
| 1320 |
property MouseLookHorizontalSensitivity; |
| 1321 |
property MouseLookVerticalSensitivity; |
| 1322 |
property InvertVerticalMouseLook; |
| 1323 |
|
| 1324 |
{ Rotation keys speed, in radians per second. |
| 1325 |
@groupBegin } |
| 1326 |
property RotationHorizontalSpeed: Single |
| 1327 |
read FRotationHorizontalSpeed write FRotationHorizontalSpeed |
| 1328 |
{$ifdef FPC}default DefaultRotationHorizontalSpeed{$endif}; |
| 1329 |
|
| 1330 |
property RotationVerticalSpeed: Single |
| 1331 |
read FRotationVerticalSpeed write FRotationVerticalSpeed |
| 1332 |
{$ifdef FPC}default DefaultRotationVerticalSpeed{$endif}; |
| 1333 |
{ @groupEnd } |
| 1334 |
|
| 1335 |
{ Moving speeds. |
| 1336 |
MoveHorizontalSpeed is only for horizontal movement, |
| 1337 |
MoveVerticalSpeed is only for vertical, and MoveSpeed simply affects |
| 1338 |
both types of movement. Effectively, we always scale the speed |
| 1339 |
of movement by either @code(MoveHorizontalSpeed * MoveSpeed) or |
| 1340 |
@code(MoveVerticalSpeed * MoveSpeed). |
| 1341 |
|
| 1342 |
We move by distance @code(MoveSpeed * MoveHorizontalSpeed (or MoveVerticalSpeed)) |
| 1343 |
during one second. |
| 1344 |
So if you leave MoveHorizontalSpeed = MoveVerticalSpeed = 1 (as default), |
| 1345 |
MoveSpeed expresses the speed in nice units / per second. |
| 1346 |
|
| 1347 |
Default values for all these speed properties is 1.0, |
| 1348 |
so you simply move by 1 unit per second. |
| 1349 |
|
| 1350 |
@groupBegin } |
| 1351 |
property MoveHorizontalSpeed: Single |
| 1352 |
read FMoveHorizontalSpeed write FMoveHorizontalSpeed {$ifdef FPC}default 1.0{$endif}; |
| 1353 |
property MoveVerticalSpeed: Single |
| 1354 |
read FMoveVerticalSpeed write FMoveVerticalSpeed {$ifdef FPC}default 1.0{$endif}; |
| 1355 |
property MoveSpeed: Single read FMoveSpeed write FMoveSpeed {$ifdef FPC}default 1.0{$endif}; |
| 1356 |
{ @groupEnd } |
| 1357 |
|
| 1358 |
{ Minimum and maximum values for possible @link(MoveSpeed) that user can make, |
| 1359 |
using Input_MoveSpeedInc, Input_MoveSpeedInc. |
| 1360 |
Note that code can still set @link(MoveSpeed) to any value, however small or large, |
| 1361 |
these limits only apply to user changing speed by Input_MoveSpeedInc, Input_MoveSpeedInc. |
| 1362 |
@groupBegin } |
| 1363 |
property MoveSpeedMin: Single read FMoveSpeedMin write FMoveSpeedMin {$ifdef FPC}default DefaultMoveSpeedMin{$endif}; |
| 1364 |
property MoveSpeedMax: Single read FMoveSpeedMax write FMoveSpeedMax {$ifdef FPC}default DefaultMoveSpeedMax{$endif}; |
| 1365 |
{ @groupEnd } |
| 1366 |
|
| 1367 |
{ Speed (radians per pixel delta) of rotations by mouse dragging. |
| 1368 |
Relevant only if niMouseDragging in @link(Input) and |
| 1369 |
MouseDragMode is mdRotate or mdWalkRotate. |
| 1370 |
Separate for horizontal and vertical, this way you can e.g. limit |
| 1371 |
(or disable) vertical rotations, useful for games where you mostly |
| 1372 |
look horizontally and accidentally looking up/down is more confusing |
| 1373 |
than useful. |
| 1374 |
@groupBegin } |
| 1375 |
property MouseDraggingHorizontalRotationSpeed: Single |
| 1376 |
read FMouseDraggingHorizontalRotationSpeed write FMouseDraggingHorizontalRotationSpeed |
| 1377 |
{$ifdef FPC}default DefaultMouseDraggingHorizontalRotationSpeed{$endif}; |
| 1378 |
property MouseDraggingVerticalRotationSpeed: Single |
| 1379 |
read FMouseDraggingVerticalRotationSpeed write FMouseDraggingVerticalRotationSpeed |
| 1380 |
{$ifdef FPC}default DefaultMouseDraggingVerticalRotationSpeed{$endif}; |
| 1381 |
{ @groupEnd } |
| 1382 |
|
| 1383 |
{ Moving speed when mouse dragging. |
| 1384 |
Relevant only when @code((MouseDragMode is mdWalkRotate) and |
| 1385 |
(niMouseDragging in UsingInput)). } |
| 1386 |
property MouseDraggingMoveSpeed: Single |
| 1387 |
read FMouseDraggingMoveSpeed write FMouseDraggingMoveSpeed |
| 1388 |
{$ifdef FPC}default DefaultMouseDraggingMoveSpeed{$endif}; |
| 1389 |
|
| 1390 |
{ What mouse dragging does. Used only when niMouseDragging in @link(Input). } |
| 1391 |
property MouseDragMode: TMouseDragMode |
| 1392 |
read FMouseDragMode write FMouseDragMode default mdWalkRotate; |
| 1393 |
|
| 1394 |
{ This unlocks a couple of features and automatic behaviors |
| 1395 |
related to gravity. Gravity always drags the camera down to |
| 1396 |
-GravityUp. |
| 1397 |
|
| 1398 |
Summary of things done by gravity: |
| 1399 |
@unorderedList( |
| 1400 |
@item(It performs collision detection with parent TCastleViewport to get camera height above the ground.) |
| 1401 |
@item(It allows player to jump. See Input_Jump, IsJumping, JumpMaxHeight, |
| 1402 |
JumpHorizontalSpeedMultiply.) |
| 1403 |
@item(It allows player to crouch. See Input_Crouch, CrouchHeight.) |
| 1404 |
@item(It tries to keep @link(TCastleTransform.Translation Camera.Translation) above the ground on |
| 1405 |
PreferredHeight height.) |
| 1406 |
@item(When current height is too small --- @link(TCastleTransform.Translation Camera.Translation) is moved up. |
| 1407 |
See GrowSpeed.) |
| 1408 |
@item(When current height is too large --- we're falling down. |
| 1409 |
See Falling, OnFall, FallSpeedStart, |
| 1410 |
FallSpeedIncrease, FallingEffect.) |
| 1411 |
@item(It does head bobbing. See HeadBobbing, HeadBobbingTime.) |
| 1412 |
) |
| 1413 |
|
| 1414 |
While there are many properties allowing you to control |
| 1415 |
gravity behavior, most of them have initial values that should be |
| 1416 |
sensible in all cases. The most important property you need to set yourself is PreferredHeight. |
| 1417 |
Everything else should basically work auto-magically. |
| 1418 |
|
| 1419 |
Note that Gravity setting is independent from |
| 1420 |
PreferGravityUpForRotations or PreferGravityUpForMoving settings --- |
| 1421 |
PreferGravityUpXxx say how the player controls work, |
| 1422 |
Gravity says what happens to player due to ... well, due to gravity. } |
| 1423 |
property Gravity: boolean |
| 1424 |
read FGravity write FGravity default true; |
| 1425 |
|
| 1426 |
{ Height above the ground, only used when @link(TCastleWalkNavigation.Gravity) is @true. |
| 1427 |
The @link(TCastleTransform.Translation Camera.Translation) tries to stay PreferredHeight above the ground. |
| 1428 |
Temporarily it may still be lower (e.g. player can |
| 1429 |
shortly "duck" when he falls from high). |
| 1430 |
|
| 1431 |
This must always be >= 0. |
| 1432 |
You should set this to something greater than zero to get sensible |
| 1433 |
behavior of some things related to @link(TCastleWalkNavigation.Gravity). |
| 1434 |
|
| 1435 |
See CorrectPreferredHeight for important property |
| 1436 |
of PreferredHeight that you should keep. } |
| 1437 |
property PreferredHeight: Single |
| 1438 |
read FPreferredHeight write FPreferredHeight {$ifdef FPC}default DefaultPreferredHeight{$endif}; |
| 1439 |
|
| 1440 |
{ Preferred height when crouching. |
| 1441 |
This is always mutiplied to PreferredHeight. |
| 1442 |
This should always be <= 1 (CrouchHeight = 1 effectively disables |
| 1443 |
crouching, although it's better to do this by calling MakeClear |
| 1444 |
on Input_Crouch). } |
| 1445 |
property CrouchHeight: Single |
| 1446 |
read FCrouchHeight write FCrouchHeight {$ifdef FPC}default DefaultCrouchHeight{$endif}; |
| 1447 |
|
| 1448 |
property Radius; |
| 1449 |
end; |
| 1450 |
|
| 1451 |
TUniversalCamera = TCastleNavigation deprecated 'complicated TUniversalCamera class is removed; use TCastleNavigation as base class, or TCastleWalkNavigation or TCastleExamineNavigation for particular type, and Viewport.NavigationType to switch type'; |
| 1452 |
|
| 1453 |
TCamera = TCastleNavigation deprecated 'use TCastleNavigation'; |
| 1454 |
TExamineCamera = TCastleExamineNavigation deprecated 'use TCastleExamineNavigation'; |
| 1455 |
TWalkCamera = TCastleWalkNavigation deprecated 'use TCastleWalkNavigation'; |
| 1456 |
|
| 1457 |
{ See TCastleWalkNavigation.CorrectPreferredHeight. |
| 1458 |
This is a global version, sometimes may be useful. } |
| 1459 |
procedure CorrectPreferredHeight(var PreferredHeight: Single; |
| 1460 |
const Radius: Single; const CrouchHeight, HeadBobbing: Single); |
| 1461 |
|
| 1462 |
const |
| 1463 |
ciNormal = niNormal deprecated 'use niNormal'; |
| 1464 |
ciMouseDragging = niMouseDragging deprecated 'use niMouseDragging'; |
| 1465 |
ciGesture = niGesture deprecated 'use niGesture'; |
| 1466 |
ci3dMouse = ni3dMouse deprecated 'use ni3dMouse'; |
| 1467 |
|
| 1468 |
implementation |
| 1469 |
|
| 1470 |
uses Math, |
| 1471 |
CastleStringUtils, CastleLog, CastleViewport, |
| 1472 |
CastleComponentSerialize; |
| 1473 |
|
| 1474 |
{ TCastleNavigation ------------------------------------------------------------ } |
| 1475 |
|
| 1476 |
constructor TCastleNavigation.Create(AOwner: TComponent); |
| 1477 |
begin |
| 1478 |
inherited; |
| 1479 |
FRadius := DefaultRadius; |
| 1480 |
FInput := DefaultInput; |
| 1481 |
FModelBox := TBox3D.Empty; |
| 1482 |
FCheckCollisions := true; |
| 1483 |
|
| 1484 |
// interaction state |
| 1485 |
MouseDraggingStarted := -1; |
| 1486 |
|
| 1487 |
FullSize := true; |
| 1488 |
EditorSelectOnHover := false; |
| 1489 |
|
| 1490 |
FInput_ZoomIn := TInputShortcut.Create(Self); |
| 1491 |
Input_ZoomIn.Assign(keyNone, keyNone, '', false, buttonLeft, mwUp); |
| 1492 |
Input_ZoomIn.SetSubComponent(true); |
| 1493 |
Input_ZoomIn.Name := 'Input_ZoomIn'; |
| 1494 |
|
| 1495 |
FInput_ZoomOut := TInputShortcut.Create(Self); |
| 1496 |
Input_ZoomOut.Assign(keyNone, keyNone, '', false, buttonLeft, mwDown); |
| 1497 |
Input_ZoomOut.SetSubComponent(true); |
| 1498 |
Input_ZoomOut.Name := 'Input_ZoomOut'; |
| 1499 |
end; |
| 1500 |
|
| 1501 |
function TCastleNavigation.MoveAllowed( |
| 1502 |
const OldPos: TVector3; ProposedNewPos: TVector3; out NewPos: TVector3; |
| 1503 |
const BecauseOfGravity, CheckClimbHeight: Boolean): Boolean; |
| 1504 |
begin |
| 1505 |
Result := true; |
| 1506 |
NewPos := ProposedNewPos; |
| 1507 |
|
| 1508 |
if Result and Valid and CheckCollisions then |
| 1509 |
begin |
| 1510 |
Result := (InternalViewport as TCastleViewport).InternalNavigationMoveAllowed( |
| 1511 |
Self, OldPos, ProposedNewPos, NewPos, Radius, BecauseOfGravity); |
| 1512 |
// update ProposedNewPos for OnMoveAllowed call |
| 1513 |
if Result then |
| 1514 |
ProposedNewPos := NewPos; |
| 1515 |
end; |
| 1516 |
if Result and Assigned(OnMoveAllowed) then |
| 1517 |
Result := OnMoveAllowed(Self, OldPos, ProposedNewPos, NewPos, Radius, BecauseOfGravity); |
| 1518 |
end; |
| 1519 |
|
| 1520 |
procedure TCastleNavigation.Height(const APosition: TVector3; |
| 1521 |
out AIsAbove: Boolean; |
| 1522 |
out AnAboveHeight: Single; out AnAboveGround: PTriangle); |
| 1523 |
begin |
| 1524 |
if Valid and CheckCollisions then |
| 1525 |
AIsAbove := (InternalViewport as TCastleViewport).InternalNavigationHeight(Self, APosition, AnAboveHeight, AnAboveGround) else |
| 1526 |
begin |
| 1527 |
AIsAbove := false; |
| 1528 |
AnAboveHeight := MaxSingle; |
| 1529 |
AnAboveGround := nil; |
| 1530 |
end; |
| 1531 |
end; |
| 1532 |
|
| 1533 |
function TCastleNavigation.Camera: TCastleCamera; |
| 1534 |
begin |
| 1535 |
if InternalViewport = nil then |
| 1536 |
raise EViewportNotAssigned.CreateFmt('Viewport not assigned, cannot get Camera properties (from %s %s)', [ |
| 1537 |
Name, |
| 1538 |
ClassName |
| 1539 |
]); |
| 1540 |
Result := (InternalViewport as TCastleViewport).InternalCamera; |
| 1541 |
end; |
| 1542 |
|
| 1543 |
procedure TCastleNavigation.AnimateTo(const APos, ADir, AUp: TVector3; const Time: TFloatTime); |
| 1544 |
begin |
| 1545 |
Camera.AnimateTo(APos, ADir, AUp, Time); |
| 1546 |
end; |
| 1547 |
|
| 1548 |
procedure TCastleNavigation.AnimateTo(const OtherNavigation: TCastleNavigation; const Time: TFloatTime); |
| 1549 |
var |
| 1550 |
APos, ADir, AUp: TVector3; |
| 1551 |
begin |
| 1552 |
OtherNavigation.Camera.GetWorldView(APos, ADir, AUp); |
| 1553 |
Camera.AnimateTo(APos, ADir, AUp, Time); |
| 1554 |
end; |
| 1555 |
|
| 1556 |
procedure TCastleNavigation.AnimateTo(const OtherCamera: TCastleCamera; const Time: TFloatTime); |
| 1557 |
begin |
| 1558 |
Camera.AnimateTo(OtherCamera, Time); |
| 1559 |
end; |
| 1560 |
|
| 1561 |
function TCastleNavigation.Animation: boolean; |
| 1562 |
begin |
| 1563 |
Result := Camera.Animation; |
| 1564 |
end; |
| 1565 |
|
| 1566 |
function TCastleNavigation.GetIgnoreAllInputs: boolean; |
| 1567 |
begin |
| 1568 |
Result := Input = []; |
| 1569 |
end; |
| 1570 |
|
| 1571 |
procedure TCastleNavigation.SetIgnoreAllInputs(const Value: boolean); |
| 1572 |
begin |
| 1573 |
if Value then |
| 1574 |
Input := [] |
| 1575 |
else |
| 1576 |
Input := DefaultInput; |
| 1577 |
end; |
| 1578 |
|
| 1579 |
function TCastleNavigation.ReallyEnableMouseDragging: boolean; |
| 1580 |
begin |
| 1581 |
Result := niMouseDragging in UsingInput; |
| 1582 |
|
| 1583 |
{ Is mouse dragging allowed by viewport. |
| 1584 |
This is an additional condition to enable mouse dragging, |
| 1585 |
above the existing niMouseDragging in UsingInput. |
| 1586 |
It is used to prevent camera navigation by |
| 1587 |
dragging when we already drag a 3D item (like X3D TouchSensor). } |
| 1588 |
if Result then |
| 1589 |
begin |
| 1590 |
Assert(InternalViewport <> nil); // UsingInput is [] otherwise |
| 1591 |
Result := not (InternalViewport as TCastleViewport).InternalPointingDeviceDragging; |
| 1592 |
end; |
| 1593 |
end; |
| 1594 |
|
| 1595 |
function TCastleNavigation.Press(const Event: TInputPressRelease): boolean; |
| 1596 |
begin |
| 1597 |
Result := inherited; |
| 1598 |
if Result then Exit; |
| 1599 |
|
| 1600 |
if (Event.EventType = itMouseButton) and |
| 1601 |
ReallyEnableMouseDragging then |
| 1602 |
begin |
| 1603 |
MouseDraggingStart := Container.MousePosition; |
| 1604 |
MouseDraggingStarted := Event.FingerIndex; |
| 1605 |
{ TODO: Not setting Result to true below is a hack, to allow TCastleViewport |
| 1606 |
to receive presses anyway. A cleaner solution would be to use |
| 1607 |
PreviewPress in TCastleViewport, but this causes other problems, |
| 1608 |
for unknown reason clicking on TouchSensor then still allows navigation like Walk |
| 1609 |
to receive mouse dragging. |
| 1610 |
Testcase: demo-models, touch_sensor_tests.x3dv } |
| 1611 |
// Exit(true); |
| 1612 |
end; |
| 1613 |
|
| 1614 |
if (Event.EventType = itMouseWheel) and |
| 1615 |
ZoomEnabled then |
| 1616 |
begin |
| 1617 |
if Zoom(Event.MouseWheelScroll) then |
| 1618 |
Exit(true); |
| 1619 |
end; |
| 1620 |
end; |
| 1621 |
|
| 1622 |
function TCastleNavigation.Release(const Event: TInputPressRelease): boolean; |
| 1623 |
begin |
| 1624 |
if Event.EventType = itMouseButton then |
| 1625 |
MouseDraggingStarted := -1; |
| 1626 |
Result := inherited; |
| 1627 |
end; |
| 1628 |
|
| 1629 |
procedure TCastleNavigation.Assign(Source: TPersistent); |
| 1630 |
var |
| 1631 |
SourceNav: TCastleNavigation; |
| 1632 |
begin |
| 1633 |
if Source is TCastleNavigation then |
| 1634 |
begin |
| 1635 |
SourceNav := TCastleNavigation(Source); |
| 1636 |
Radius := SourceNav.Radius ; |
| 1637 |
Input := SourceNav.Input ; |
| 1638 |
{ The Cursor should be synchronized with TCastleMouseLookNavigation.MouseLook, |
| 1639 |
do not blindly copy it from TCastleWalkNavigation to TCastleExamineNavigation. } |
| 1640 |
// Cursor := SourceNav.Cursor ; |
| 1641 |
ModelBox := SourceNav.ModelBox ; |
| 1642 |
|
| 1643 |
{ TODO: should move to TCastleWalkNavigation.Assign, |
| 1644 |
but actually we'll probably resign from maintaining Assign on navigation classes. } |
| 1645 |
// PreferredHeight := SourceNav.PreferredHeight ; |
| 1646 |
// MoveHorizontalSpeed := SourceNav.MoveHorizontalSpeed; |
| 1647 |
// MoveVerticalSpeed := SourceNav.MoveVerticalSpeed ; |
| 1648 |
// MoveSpeed := SourceNav.MoveSpeed ; |
| 1649 |
// HeadBobbing := SourceNav.HeadBobbing ; |
| 1650 |
// HeadBobbingTime := SourceNav.HeadBobbingTime ; |
| 1651 |
// ClimbHeight := SourceNav.ClimbHeight ; |
| 1652 |
// CrouchHeight := SourceNav.CrouchHeight ; |
| 1653 |
{ Always call CorrectPreferredHeight after changing Radius or PreferredHeight } |
| 1654 |
// CorrectPreferredHeight; |
| 1655 |
end else |
| 1656 |
{ Call inherited ONLY when you cannot handle Source class, |
| 1657 |
to raise EConvertError from TPersistent.Assign. } |
| 1658 |
inherited Assign(Source); |
| 1659 |
end; |
| 1660 |
|
| 1661 |
function TCastleNavigation.UsingInput: TNavigationInputs; |
| 1662 |
begin |
| 1663 |
if Valid then |
| 1664 |
Result := Input |
| 1665 |
else |
| 1666 |
Result := []; |
| 1667 |
end; |
| 1668 |
|
| 1669 |
function TCastleNavigation.InternalViewport: TCastleUserInterface; |
| 1670 |
begin |
| 1671 |
if Parent <> nil then |
| 1672 |
begin |
| 1673 |
if Parent is TCastleViewport then |
| 1674 |
Result := Parent |
| 1675 |
else |
| 1676 |
begin |
| 1677 |
Result := nil; |
| 1678 |
if not FWarningInvalidParentDone then |
| 1679 |
begin |
| 1680 |
FWarningInvalidParentDone := true; |
| 1681 |
raise Exception.Create('TCastleNavigation should be only added as an immediate child of TCastleViewport, otherwise it does not do anything'); |
| 1682 |
end; |
| 1683 |
end; |
| 1684 |
end else |
| 1685 |
Result := nil; |
| 1686 |
end; |
| 1687 |
|
| 1688 |
function TCastleNavigation.Valid: Boolean; |
| 1689 |
var |
| 1690 |
V: TCastleViewport; |
| 1691 |
begin |
| 1692 |
if InternalViewport = nil then |
| 1693 |
Exit(false); |
| 1694 |
|
| 1695 |
V := InternalViewport as TCastleViewport; |
| 1696 |
Result := |
| 1697 |
{ At design-time, honor only V.InternalDesignNavigation. } |
| 1698 |
((not V.InternalDesignManipulation) or (V.InternalDesignNavigation = Self)) and |
| 1699 |
{ Ignore input on a paused viewport at runtime (ignore Paused at design-time) } |
| 1700 |
(V.InternalDesignManipulation or (not V.Items.Paused)) and |
| 1701 |
{ As Viewport.Camera is assignable, be prepared to handle InternalCamera = nil situation } |
| 1702 |
(V.InternalCamera <> nil) and |
| 1703 |
{ During camera animation, all navigation is disabled. } |
| 1704 |
(not V.InternalCamera.Animation); |
| 1705 |
end; |
| 1706 |
|
| 1707 |
function TCastleNavigation.PropertySections( |
| 1708 |
const PropertyName: String): TPropertySections; |
| 1709 |
begin |
| 1710 |
if (PropertyName = 'CheckCollisions') or |
| 1711 |
(PropertyName = 'ZoomEnabled') then |
| 1712 |
Result := [psBasic] |
| 1713 |
else |
| 1714 |
Result := inherited PropertySections(PropertyName); |
| 1715 |
end; |
| 1716 |
|
| 1717 |
function TCastleNavigation.MoveTo(const LocalProposedNewPos: TVector3; |
| 1718 |
const BecauseOfGravity, CheckClimbHeight: boolean): boolean; |
| 1719 |
var |
| 1720 |
OldPos, NewPos: TVector3; |
| 1721 |
WorldProposedNewPos: TVector3; |
| 1722 |
begin |
| 1723 |
// most of calculations inside are in world coordinates |
| 1724 |
if (Camera.Parent <> nil) and |
| 1725 |
(Camera.World <> nil) then |
| 1726 |
begin |
| 1727 |
OldPos := Camera.Parent.LocalToWorld(Camera.Translation); |
| 1728 |
WorldProposedNewPos := Camera.Parent.LocalToWorld(LocalProposedNewPos); |
| 1729 |
end else |
| 1730 |
begin |
| 1731 |
OldPos := Camera.Translation; |
| 1732 |
WorldProposedNewPos := LocalProposedNewPos; |
| 1733 |
end; |
| 1734 |
|
| 1735 |
Result := MoveAllowed(OldPos, WorldProposedNewPos, NewPos, BecauseOfGravity, CheckClimbHeight); |
| 1736 |
|
| 1737 |
if Result then |
| 1738 |
begin |
| 1739 |
// convert back from world to local coordinates |
| 1740 |
if (Camera.Parent <> nil) and |
| 1741 |
(Camera.World <> nil) then |
| 1742 |
begin |
| 1743 |
NewPos := Camera.Parent.WorldToLocal(NewPos); |
| 1744 |
end; |
| 1745 |
|
| 1746 |
Camera.Translation := NewPos; |
| 1747 |
end; |
| 1748 |
end; |
| 1749 |
|
| 1750 |
function TCastleNavigation.Move(const MoveVector: TVector3; |
| 1751 |
const BecauseOfGravity, CheckClimbHeight: boolean): boolean; |
| 1752 |
begin |
| 1753 |
Result := MoveTo(Camera.Translation + MoveVector, BecauseOfGravity, CheckClimbHeight); |
| 1754 |
end; |
| 1755 |
|
| 1756 |
function TCastleNavigation.GoodModelBox: TBox3D; |
| 1757 |
begin |
| 1758 |
{ Try hard to return non-empty bounding box, otherwise examine navigation |
| 1759 |
doesn't work sensibly, as movement and zooming speed must depend on box |
| 1760 |
sizes. |
| 1761 |
|
| 1762 |
This is important in case you use TCastleExamineNavigation without |
| 1763 |
setting it's ModelBox explicitly, which happens e.g. when CGE editor |
| 1764 |
adds TCastleExamineNavigation. |
| 1765 |
|
| 1766 |
Note: We use Items.BoundingBox, not (private) ItemsBoundingBox |
| 1767 |
that avoids adding gizmos to bbox. |
| 1768 |
Right now it doesn't matter (as we don't need this box to be precise, |
| 1769 |
we dont' zoom to box center) so it's better to use Items.BoundingBox |
| 1770 |
and keep ItemsBoundingBox private. |
| 1771 |
} |
| 1772 |
if ModelBox.IsEmpty and |
| 1773 |
(InternalViewport <> nil) then |
| 1774 |
Result := (InternalViewport as TCastleViewport).Items.BoundingBox |
| 1775 |
else |
| 1776 |
Result := ModelBox; |
| 1777 |
end; |
| 1778 |
|
| 1779 |
function TCastleNavigation.Zoom(const Factor: Single): Boolean; |
| 1780 |
const |
| 1781 |
{ Multiplier for Factor. } |
| 1782 |
Speed = 1 / 30; |
| 1783 |
|
| 1784 |
function OrthographicProjection: Boolean; |
| 1785 |
begin |
| 1786 |
{ See how perspective (and more flexible frustum) projection matrices |
| 1787 |
look like in CastleProjection, they have always -1 in this field. } |
| 1788 |
Result := Camera.ProjectionMatrix.Data[2, 3] = 0; |
| 1789 |
end; |
| 1790 |
|
| 1791 |
var |
| 1792 |
Size: Single; |
| 1793 |
//MoveWorldDir, ToCenter, MoveDir, CamWorldPos, CamWorldDir, CamWorldUp: TVector3; |
| 1794 |
B: TBox3D; |
| 1795 |
SavedCheckCollisions: Boolean; |
| 1796 |
begin |
| 1797 |
Result := false; |
| 1798 |
|
| 1799 |
if Valid then |
| 1800 |
begin |
| 1801 |
if OrthographicProjection then |
| 1802 |
begin |
| 1803 |
{ In case of OrthographicProjection, changing Translation |
| 1804 |
would have no effect. So instead scale the projection size. } |
| 1805 |
if (Camera.Orthographic.Width = 0) and |
| 1806 |
(Camera.Orthographic.Height = 0) then |
| 1807 |
begin |
| 1808 |
{ Note: We had approach in CGE to make Orthographic.Scale, |
| 1809 |
to enable scaling orthographic view always. |
| 1810 |
But it was unnatural to not change Orthographic.Width/Height |
| 1811 |
in the most common case, which is that Orthographic.Width/Height |
| 1812 |
is non-zero. } |
| 1813 |
WritelnWarning('Scaling orthographic view is not possible without setting Camera.Orthographic.Width / Camera.Orthographic.Height to non-zero') |
| 1814 |
end else |
| 1815 |
begin |
| 1816 |
Camera.Orthographic.Width := Camera.Orthographic.Width * Exp(-Factor * Speed); |
| 1817 |
Camera.Orthographic.Height := Camera.Orthographic.Height * Exp(-Factor * Speed); |
| 1818 |
Result := true; |
| 1819 |
end; |
| 1820 |
end else |
| 1821 |
begin |
| 1822 |
{ In perspective projection, zoom by changing Translation } |
| 1823 |
B := GoodModelBox; |
| 1824 |
if B.IsEmptyOrZero then |
| 1825 |
Exit; |
| 1826 |
|
| 1827 |
Size := B.AverageSize; |
| 1828 |
|
| 1829 |
(* |
| 1830 |
// We used to have here a complicated logic that zooms into bbox center. |
| 1831 |
// In the end, this is not necessary and not intuitive. |
| 1832 |
// Let zoom now just move forward/backward. |
| 1833 |
|
| 1834 |
Camera.GetWorldView(CamWorldPos, CamWorldDir, CamWorldUp); |
| 1835 |
|
| 1836 |
ToCenter := B.Center - CamWorldPos; |
| 1837 |
if ToCenter.IsZero then |
| 1838 |
ToCenter := CamWorldDir |
| 1839 |
else |
| 1840 |
ToCenter := ToCenter.Normalize; |
| 1841 |
|
| 1842 |
if Factor > 0 then // zoom in, move along ToCenter |
| 1843 |
begin |
| 1844 |
{ Don't allow moving with "zoom in" if we're looking at the other side. } |
| 1845 |
if TVector3.DotProduct(ToCenter, CamWorldDir) < 0 then |
| 1846 |
Exit; |
| 1847 |
end else |
| 1848 |
begin |
| 1849 |
{ Always allow "zoom out", camera direction determines in which direction we move. |
| 1850 |
This allows to et out from the "maximum zoom in" view always, |
| 1851 |
even if camera effectively "crossed over" the box middle. } |
| 1852 |
if TVector3.DotProduct(ToCenter, CamWorldDir) < 0 then |
| 1853 |
ToCenter := -ToCenter; |
| 1854 |
end; |
| 1855 |
|
| 1856 |
MoveWorldDir := Factor * Size * ToCenter; |
| 1857 |
|
| 1858 |
if (Camera.Parent <> nil) and |
| 1859 |
(Camera.World <> nil) then |
| 1860 |
MoveDir := Camera.Parent.WorldToLocalDirection(MoveWorldDir) |
| 1861 |
else |
| 1862 |
Exit; |
| 1863 |
*) |
| 1864 |
|
| 1865 |
SavedCheckCollisions := CheckCollisions; |
| 1866 |
if Factor < 0 then |
| 1867 |
CheckCollisions := false; // never check collisions when zooming out |
| 1868 |
try |
| 1869 |
Result := Move(Camera.Direction * Size * Factor * Speed, false, false); |
| 1870 |
finally |
| 1871 |
CheckCollisions := SavedCheckCollisions; |
| 1872 |
end; |
| 1873 |
end; |
| 1874 |
end; |
| 1875 |
end; |
| 1876 |
|
| 1877 |
{ TCastleExamineNavigation ------------------------------------------------------------ } |
| 1878 |
|
| 1879 |
constructor TCastleExamineNavigation.Create(AOwner: TComponent); |
| 1880 |
type |
| 1881 |
T3BoolKeys = array [0..2, boolean] of TKey; |
| 1882 |
const |
| 1883 |
DefaultInputs_Move: T3BoolKeys = |
| 1884 |
((keyArrowLeft, keyArrowRight), (keyArrowDown, keyArrowUp), (keyNone, keyNone)); |
| 1885 |
DefaultInputs_Rotate: T3BoolKeys = |
| 1886 |
((keyArrowUp, keyArrowDown), (keyArrowLeft, keyArrowRight), (keyNone, keyNone)); |
| 1887 |
CoordToStr: array [0..2] of string = ('X', 'Y', 'Z'); |
| 1888 |
IncreaseToStr: array [boolean] of string = ('Dec', 'Inc'); |
| 1889 |
var |
| 1890 |
I: Integer; |
| 1891 |
B: boolean; |
| 1892 |
begin |
| 1893 |
inherited; |
| 1894 |
|
| 1895 |
FRotationEnabled := true; |
| 1896 |
FMoveEnabled := true; |
| 1897 |
ZoomEnabled := true; |
| 1898 |
FRotationsAnim := TVector3.Zero; |
| 1899 |
FDragMoveSpeed := 1; |
| 1900 |
FKeysMoveSpeed := 1; |
| 1901 |
FScaleFactorMin := 0.01; |
| 1902 |
FScaleFactorMax := 100.0; |
| 1903 |
FRotationAccelerate := true; |
| 1904 |
FRotationAccelerationSpeed := DefaultRotationAccelerationSpeed; |
| 1905 |
FRotationSpeed := DefaultRotationSpeed; |
| 1906 |
FPinchGestureRecognizer := TCastlePinchPanGestureRecognizer.Create; |
| 1907 |
FPinchGestureRecognizer.OnGestureChanged := {$ifdef FPC}@{$endif}OnGestureRecognized; |
| 1908 |
FExactMovement := true; |
| 1909 |
FAutoCenterOfRotation := true; |
| 1910 |
FZoomSpeed := DefaultZoomSpeed; |
| 1911 |
|
| 1912 |
for I := 0 to 2 do |
| 1913 |
for B := false to true do |
| 1914 |
begin |
| 1915 |
FInputs_Move[I, B] := TInputShortcut.Create(Self); |
| 1916 |
FInputs_Move[I, B].Name := 'Input_Move' + CoordToStr[I] + IncreaseToStr[B]; |
| 1917 |
FInputs_Move[I, B].SetSubComponent(true); |
| 1918 |
FInputs_Move[I, B].Assign(DefaultInputs_Move[I, B]); |
| 1919 |
|
| 1920 |
FInputs_Rotate[I, B] := TInputShortcut.Create(Self); |
| 1921 |
FInputs_Rotate[I, B].Name := 'Input_Rotate' + CoordToStr[I] + IncreaseToStr[B]; |
| 1922 |
FInputs_Rotate[I, B].SetSubComponent(true); |
| 1923 |
FInputs_Rotate[I, B].Assign(DefaultInputs_Rotate[I, B]); |
| 1924 |
end; |
| 1925 |
|
| 1926 |
{ For scale larger/smaller we use also character codes +/-, as numpad |
| 1927 |
may be hard to reach on some keyboards (e.g. on laptops). } |
| 1928 |
FInput_ScaleLarger := TInputShortcut.Create(Self); |
| 1929 |
Input_ScaleLarger.Name := 'Input_ScaleLarger'; |
| 1930 |
Input_ScaleLarger.SetSubComponent(true); |
| 1931 |
Input_ScaleLarger.Assign(keyNumpadPlus, keyNone, '+'); |
| 1932 |
|
| 1933 |
FInput_ScaleSmaller := TInputShortcut.Create(Self); |
| 1934 |
Input_ScaleSmaller.Name := 'Input_ScaleSmaller'; |
| 1935 |
Input_ScaleSmaller.SetSubComponent(true); |
| 1936 |
Input_ScaleSmaller.Assign(keyNumpadMinus, keyNone, '-'); |
| 1937 |
|
| 1938 |
FInput_Home := TInputShortcut.Create(Self); |
| 1939 |
Input_Home.Name := 'Input_Home'; |
| 1940 |
Input_Home.SetSubComponent(true); |
| 1941 |
Input_Home.Assign(keyNone); |
| 1942 |
|
| 1943 |
FInput_StopRotating := TInputShortcut.Create(Self); |
| 1944 |
Input_StopRotating.Name := 'Input_StopRotating'; |
| 1945 |
Input_StopRotating.SetSubComponent(true); |
| 1946 |
Input_StopRotating.Assign(keySpace, keyNone, '', true, buttonLeft); |
| 1947 |
|
| 1948 |
FInput_Rotate := TInputShortcut.Create(Self); |
| 1949 |
Input_Rotate.Name := 'Input_Rotate'; |
| 1950 |
Input_Rotate.SetSubComponent(true); |
| 1951 |
{ left mouse button, no modifiers } |
| 1952 |
Input_Rotate.Assign(keyNone, keyNone, '', true, buttonLeft); |
| 1953 |
Input_Rotate.MouseButtonCheckModifiers := [mkCtrl, mkShift]; |
| 1954 |
Input_Rotate.MouseButtonModifiers := []; |
| 1955 |
|
| 1956 |
FInput_Move := TInputShortcut.Create(Self); |
| 1957 |
Input_Move.Name := 'Input_Move'; |
| 1958 |
Input_Move.SetSubComponent(true); |
| 1959 |
{ middle mouse button, no modifiers } |
| 1960 |
Input_Move.Assign(keyNone, keyNone, '', true, buttonMiddle); |
| 1961 |
Input_Move.MouseButtonCheckModifiers := [mkCtrl, mkShift]; |
| 1962 |
Input_Move.MouseButtonModifiers := []; |
| 1963 |
{ left mouse button, with Shift } |
| 1964 |
Input_Move.MouseButton2Use := true; |
| 1965 |
Input_Move.MouseButton2 := buttonLeft; |
| 1966 |
Input_Move.MouseButton2CheckModifiers := [mkCtrl, mkShift]; |
| 1967 |
Input_Move.MouseButton2Modifiers := [mkShift]; |
| 1968 |
|
| 1969 |
FInput_Zoom := TInputShortcut.Create(Self); |
| 1970 |
Input_Zoom.Name := 'Input_Zoom'; |
| 1971 |
Input_Zoom.SetSubComponent(true); |
| 1972 |
{ right mouse button, no modifiers } |
| 1973 |
Input_Zoom.Assign(keyNone, keyNone, '', true, buttonRight); |
| 1974 |
Input_Zoom.MouseButtonCheckModifiers := [mkCtrl, mkShift]; |
| 1975 |
Input_Zoom.MouseButtonModifiers := []; |
| 1976 |
{ left mouse button, with Ctrl } |
| 1977 |
Input_Zoom.MouseButton2Use := true; |
| 1978 |
Input_Zoom.MouseButton2 := buttonLeft; |
| 1979 |
Input_Zoom.MouseButton2CheckModifiers := [mkCtrl, mkShift]; |
| 1980 |
Input_Zoom.MouseButton2Modifiers := [mkCtrl]; |
| 1981 |
end; |
| 1982 |
|
| 1983 |
destructor TCastleExamineNavigation.Destroy; |
| 1984 |
var |
| 1985 |
I: Integer; |
| 1986 |
B: boolean; |
| 1987 |
begin |
| 1988 |
for I := 0 to 2 do |
| 1989 |
for B := false to true do |
| 1990 |
begin |
| 1991 |
FreeAndNil(FInputs_Move[I, B]); |
| 1992 |
FreeAndNil(FInputs_Rotate[I, B]); |
| 1993 |
end; |
| 1994 |
FreeAndNil(FInput_ScaleLarger); |
| 1995 |
FreeAndNil(FInput_ScaleSmaller); |
| 1996 |
FreeAndNil(FInput_Home); |
| 1997 |
FreeAndNil(FInput_StopRotating); |
| 1998 |
FreeAndNil(FPinchGestureRecognizer); |
| 1999 |
inherited; |
| 2000 |
end; |
| 2001 |
|
| 2002 |
function TCastleExamineNavigation.GetMouseButtonRotate: TCastleMouseButton; |
| 2003 |
begin |
| 2004 |
Result := Input_Rotate.MouseButton; |
| 2005 |
end; |
| 2006 |
|
| 2007 |
procedure TCastleExamineNavigation.SetMouseButtonRotate(const Value: TCastleMouseButton); |
| 2008 |
begin |
| 2009 |
Input_Rotate.MouseButton := Value; |
| 2010 |
end; |
| 2011 |
|
| 2012 |
function TCastleExamineNavigation.GetMouseButtonMove: TCastleMouseButton; |
| 2013 |
begin |
| 2014 |
Result := Input_Move.MouseButton; |
| 2015 |
end; |
| 2016 |
|
| 2017 |
procedure TCastleExamineNavigation.SetMouseButtonMove(const Value: TCastleMouseButton); |
| 2018 |
begin |
| 2019 |
Input_Move.MouseButton := Value; |
| 2020 |
end; |
| 2021 |
|
| 2022 |
function TCastleExamineNavigation.GetMouseButtonZoom: TCastleMouseButton; |
| 2023 |
begin |
| 2024 |
Result := Input_Zoom.MouseButton; |
| 2025 |
end; |
| 2026 |
|
| 2027 |
procedure TCastleExamineNavigation.SetMouseButtonZoom(const Value: TCastleMouseButton); |
| 2028 |
begin |
| 2029 |
Input_Zoom.MouseButton := Value; |
| 2030 |
end; |
| 2031 |
|
| 2032 |
function TCastleExamineNavigation.GetExamineVectors: TExamineVectors; |
| 2033 |
var |
| 2034 |
APos, ADir, AUp: TVector3; |
| 2035 |
begin |
| 2036 |
Camera.GetWorldView(APos, ADir, AUp); |
| 2037 |
|
| 2038 |
Result.Translation := -APos; |
| 2039 |
|
| 2040 |
Result.Rotations := OrientationQuaternionFromDirectionUp(ADir, AUp).Conjugate; |
| 2041 |
|
| 2042 |
{ We have to fix our Translation, since our TCastleExamineNavigation.Matrix |
| 2043 |
applies our move *first* before applying rotation |
| 2044 |
(and this is good, as it allows rotating around object center, |
| 2045 |
not around camera). |
| 2046 |
|
| 2047 |
Alternative implementation of this would call QuatToRotationMatrix and |
| 2048 |
then simulate multiplying this rotation matrix * translation matrix |
| 2049 |
of Translation. But we can do this directly. |
| 2050 |
|
| 2051 |
We also note at this point that rotation is done around |
| 2052 |
(Translation + EffectiveCenterOfRotation). But EffectiveCenterOfRotation is not |
| 2053 |
included in Translation. } |
| 2054 |
Result.Translation := Result.Rotations.Rotate(Result.Translation + EffectiveCenterOfRotation) |
| 2055 |
- EffectiveCenterOfRotation; |
| 2056 |
end; |
| 2057 |
|
| 2058 |
procedure TCastleExamineNavigation.SetExamineVectors(const Value: TExamineVectors); |
| 2059 |
var |
| 2060 |
MInverse: TMatrix4; |
| 2061 |
begin |
| 2062 |
MInverse := |
| 2063 |
TranslationMatrix(EffectiveCenterOfRotation) * |
| 2064 |
Value.Rotations.Conjugate.ToRotationMatrix * |
| 2065 |
TranslationMatrix(-(Value.Translation + EffectiveCenterOfRotation)); |
| 2066 |
|
| 2067 |
{ These MultPoint/Direction should never fail with ETransformedResultInvalid. |
| 2068 |
That's because M is composed from translations, rotations, scaling, |
| 2069 |
which preserve points/directions (4th component in homogeneous coordinates) |
| 2070 |
nicely. } |
| 2071 |
Camera.SetWorldView( |
| 2072 |
MInverse.MultPoint(TVector3.Zero), |
| 2073 |
MInverse.MultDirection(DefaultCameraDirection), |
| 2074 |
MInverse.MultDirection(DefaultCameraUp) |
| 2075 |
); |
| 2076 |
end; |
| 2077 |
|
| 2078 |
function TCastleExamineNavigation.Zoom(const Factor: Single): Boolean; |
| 2079 |
begin |
| 2080 |
Result := inherited Zoom(Factor * ZoomSpeed); |
| 2081 |
end; |
| 2082 |
|
| 2083 |
procedure TCastleExamineNavigation.Update(const SecondsPassed: Single; |
| 2084 |
var HandleInput: boolean); |
| 2085 |
var |
| 2086 |
V: TExamineVectors; |
| 2087 |
|
| 2088 |
{ Increase speed of rotating, or just rotation angle |
| 2089 |
(depending on RotationAccelerate). Direction must be -1 or +1. } |
| 2090 |
procedure RotateSpeedOrAngle(const Coord: Integer; const Direction: Integer); |
| 2091 |
const |
| 2092 |
MaxRotationSpeed = 6.0; { this prevents rotations getting too wild speed } |
| 2093 |
begin |
| 2094 |
if not RotationEnabled then Exit; |
| 2095 |
|
| 2096 |
if RotationAccelerate then |
| 2097 |
FRotationsAnim.Data[coord] := |
| 2098 |
Clamped(FRotationsAnim.Data[coord] + |
| 2099 |
RotationAccelerationSpeed * SecondsPassed * Direction, |
| 2100 |
-MaxRotationSpeed, MaxRotationSpeed) |
| 2101 |
else |
| 2102 |
V.Rotations := QuatFromAxisAngle(TVector3.One[Coord], |
| 2103 |
RotationSpeed * SecondsPassed * Direction) * V.Rotations; |
| 2104 |
end; |
| 2105 |
|
| 2106 |
var |
| 2107 |
i: integer; |
| 2108 |
MoveChange: Single; |
| 2109 |
ModsDown: TModifierKeys; |
| 2110 |
RotChange: Single; |
| 2111 |
MoveChangeVector: TVector3; |
| 2112 |
const |
| 2113 |
KeyZoomSpeed = 30.0 * 10.0; |
| 2114 |
begin |
| 2115 |
inherited; |
| 2116 |
|
| 2117 |
if not Valid then Exit; |
| 2118 |
|
| 2119 |
V := ExamineVectors; |
| 2120 |
|
| 2121 |
if RotationEnabled and (not FRotationsAnim.IsPerfectlyZero) then |
| 2122 |
begin |
| 2123 |
RotChange := SecondsPassed * RotationSpeed; |
| 2124 |
|
| 2125 |
if FRotationsAnim[0] <> 0 then |
| 2126 |
V.Rotations := QuatFromAxisAngle(TVector3.One[0], |
| 2127 |
FRotationsAnim[0] * RotChange) * V.Rotations; |
| 2128 |
|
| 2129 |
if FRotationsAnim[1] <> 0 then |
| 2130 |
begin |
| 2131 |
if Turntable then |
| 2132 |
V.Rotations := V.Rotations * QuatFromAxisAngle(TVector3.One[1], |
| 2133 |
FRotationsAnim[1] * RotChange) else |
| 2134 |
V.Rotations := QuatFromAxisAngle(TVector3.One[1], |
| 2135 |
FRotationsAnim[1] * RotChange) * V.Rotations; |
| 2136 |
end; |
| 2137 |
|
| 2138 |
if FRotationsAnim[2] <> 0 then |
| 2139 |
V.Rotations := QuatFromAxisAngle(TVector3.One[2], |
| 2140 |
FRotationsAnim[2] * RotChange) * V.Rotations; |
| 2141 |
|
| 2142 |
V.Rotations.LazyNormalizeMe; |
| 2143 |
end; |
| 2144 |
|
| 2145 |
if HandleInput and (niNormal in UsingInput) then |
| 2146 |
begin |
| 2147 |
if GoodModelBox.IsEmptyOrZero then |
| 2148 |
MoveChange := KeysMoveSpeed * SecondsPassed |
| 2149 |
else |
| 2150 |
MoveChange := KeysMoveSpeed * GoodModelBox.AverageSize * SecondsPassed; |
| 2151 |
|
| 2152 |
ModsDown := ModifiersDown(Container.Pressed); |
| 2153 |
|
| 2154 |
if MoveEnabled and (ModsDown = [mkCtrl]) then |
| 2155 |
begin |
| 2156 |
for i := 0 to 2 do |
| 2157 |
begin |
| 2158 |
if Inputs_Move[i, true ].IsPressed(Container) then |
| 2159 |
begin |
| 2160 |
MoveChangeVector := TVector3.Zero; |
| 2161 |
MoveChangeVector.Data[I] := MoveChange; |
| 2162 |
V.Translation := V.Translation + MoveChangeVector; |
| 2163 |
|
| 2164 |
HandleInput := false; |
| 2165 |
end; |
| 2166 |
if Inputs_Move[i, false].IsPressed(Container) then |
| 2167 |
begin |
| 2168 |
MoveChangeVector := TVector3.Zero; |
| 2169 |
MoveChangeVector.Data[I] := -MoveChange; |
| 2170 |
V.Translation := V.Translation + MoveChangeVector; |
| 2171 |
|
| 2172 |
HandleInput := false; |
| 2173 |
end; |
| 2174 |
end; |
| 2175 |
end else |
| 2176 |
if RotationEnabled and (ModsDown = []) then |
| 2177 |
begin |
| 2178 |
for i := 0 to 2 do |
| 2179 |
begin |
| 2180 |
if Inputs_Rotate[i, true ].IsPressed(Container) then |
| 2181 |
begin |
| 2182 |
RotateSpeedOrAngle(i, +1); |
| 2183 |
HandleInput := false; |
| 2184 |
end; |
| 2185 |
if Inputs_Rotate[i, false].IsPressed(Container) then |
| 2186 |
begin |
| 2187 |
RotateSpeedOrAngle(i, -1); |
| 2188 |
HandleInput := false; |
| 2189 |
end; |
| 2190 |
end; |
| 2191 |
end; |
| 2192 |
end; |
| 2193 |
|
| 2194 |
ExamineVectors := V; |
| 2195 |
|
| 2196 |
{ process things that do not set ExamineVectors } |
| 2197 |
if HandleInput and (niNormal in UsingInput) then |
| 2198 |
begin |
| 2199 |
if Input_ScaleLarger.IsPressed(Container) then |
| 2200 |
begin |
| 2201 |
Zoom(KeyZoomSpeed * SecondsPassed); |
| 2202 |
HandleInput := false; |
| 2203 |
end; |
| 2204 |
if Input_ScaleSmaller.IsPressed(Container) then |
| 2205 |
begin |
| 2206 |
Zoom(-KeyZoomSpeed * SecondsPassed); |
| 2207 |
HandleInput := false; |
| 2208 |
end; |
| 2209 |
end; |
| 2210 |
end; |
| 2211 |
|
| 2212 |
function TCastleExamineNavigation.AllowSuspendForInput: boolean; |
| 2213 |
begin |
| 2214 |
Result := false; |
| 2215 |
end; |
| 2216 |
|
| 2217 |
procedure TCastleExamineNavigation.SetRotationAccelerate(const Value: boolean); |
| 2218 |
begin |
| 2219 |
if FRotationAccelerate <> Value then |
| 2220 |
begin |
| 2221 |
FRotationAccelerate := Value; |
| 2222 |
FRotationsAnim := TVector3.Zero; |
| 2223 |
end; |
| 2224 |
end; |
| 2225 |
|
| 2226 |
function TCastleExamineNavigation.StopRotating: boolean; |
| 2227 |
begin |
| 2228 |
Result := not FRotationsAnim.IsPerfectlyZero; |
| 2229 |
if Result then |
| 2230 |
FRotationsAnim := TVector3.Zero; |
| 2231 |
end; |
| 2232 |
|
| 2233 |
function TCastleExamineNavigation.SensorTranslation(const X, Y, Z, Length: Double; |
| 2234 |
const SecondsPassed: Single): boolean; |
| 2235 |
var |
| 2236 |
Size: Single; |
| 2237 |
MoveSize: Double; |
| 2238 |
begin |
| 2239 |
if not (ni3dMouse in UsingInput) then Exit(false); |
| 2240 |
if not MoveEnabled then Exit(false); |
| 2241 |
if GoodModelBox.IsEmptyOrZero then Exit(false); |
| 2242 |
Result := true; |
| 2243 |
|
| 2244 |
Size := GoodModelBox.AverageSize; |
| 2245 |
MoveSize := Length * SecondsPassed / 5000; |
| 2246 |
|
| 2247 |
if Abs(X) > 5 then { left / right } |
| 2248 |
Translation := Translation + Vector3(Size * X * MoveSize, 0, 0); |
| 2249 |
|
| 2250 |
if Abs(Y) > 5 then { up / down } |
| 2251 |
Translation := Translation + Vector3(0, Size * Y * MoveSize, 0); |
| 2252 |
|
| 2253 |
if Abs(Z) > 5 then { backward / forward } |
| 2254 |
Zoom(Z * MoveSize * 30 / 2); |
| 2255 |
end; |
| 2256 |
|
| 2257 |
function TCastleExamineNavigation.SensorRotation(const X, Y, Z, Angle: Double; |
| 2258 |
const SecondsPassed: Single): boolean; |
| 2259 |
var |
| 2260 |
Moved: boolean; |
| 2261 |
RotationSize: Double; |
| 2262 |
V: TExamineVectors; |
| 2263 |
begin |
| 2264 |
if not (ni3dMouse in UsingInput) then Exit(false); |
| 2265 |
if not RotationEnabled then Exit(false); |
| 2266 |
Result := true; |
| 2267 |
|
| 2268 |
Moved := false; |
| 2269 |
RotationSize := SecondsPassed * Angle * RotationSpeed; |
| 2270 |
V := ExamineVectors; |
| 2271 |
|
| 2272 |
if Abs(X) > 0.4 then { tilt forward / backward} |
| 2273 |
begin |
| 2274 |
V.Rotations := QuatFromAxisAngle(Vector3(1, 0, 0), X * RotationSize) * V.Rotations; |
| 2275 |
Moved := true; |
| 2276 |
end; |
| 2277 |
|
| 2278 |
if Abs(Y) > 0.4 then { rotate } |
| 2279 |
begin |
| 2280 |
if Turntable then |
| 2281 |
V.Rotations := V.Rotations * |
| 2282 |
QuatFromAxisAngle(Vector3(0, 1, 0), Y * RotationSize) else |
| 2283 |
V.Rotations := QuatFromAxisAngle(Vector3(0, 1, 0), Y * RotationSize) * |
| 2284 |
V.Rotations; |
| 2285 |
Moved := true; |
| 2286 |
end; |
| 2287 |
|
| 2288 |
if (Abs(Z) > 0.4) and (not Turntable) then { tilt sidewards } |
| 2289 |
begin |
| 2290 |
V.Rotations := QuatFromAxisAngle(Vector3(0, 0, 1), Z * RotationSize) * V.Rotations; |
| 2291 |
Moved := true; |
| 2292 |
end; |
| 2293 |
|
| 2294 |
{ Assign ExamineVectors only if some change occurred } |
| 2295 |
if Moved then |
| 2296 |
ExamineVectors := V; |
| 2297 |
end; |
| 2298 |
|
| 2299 |
procedure TCastleExamineNavigation.Init(const AModelBox: TBox3D; const ARadius: Single); |
| 2300 |
var |
| 2301 |
APos, ADir, AUp, NewGravityUp: TVector3; |
| 2302 |
begin |
| 2303 |
ModelBox := AModelBox; // set using FModelBox, as there's no need to preserve view |
| 2304 |
Radius := ARadius; |
| 2305 |
|
| 2306 |
CameraViewpointForWholeScene(ModelBox, 2, 1, false, true, |
| 2307 |
APos, ADir, AUp, NewGravityUp); |
| 2308 |
|
| 2309 |
Camera.SetWorldView(APos, ADir, AUp); |
| 2310 |
Camera.GravityUp := NewGravityUp; |
| 2311 |
Camera.ProjectionNear := Radius * RadiusToProjectionNear; |
| 2312 |
end; |
| 2313 |
|
| 2314 |
{ TCastleExamineNavigation.Set* properties } |
| 2315 |
|
| 2316 |
procedure TCastleExamineNavigation.SetRotationsAnim(const Value: TVector3); |
| 2317 |
begin |
| 2318 |
FRotationsAnim := Value; |
| 2319 |
end; |
| 2320 |
|
| 2321 |
function TCastleExamineNavigation.GetRotations: TQuaternion; |
| 2322 |
begin |
| 2323 |
Result := ExamineVectors.Rotations; |
| 2324 |
end; |
| 2325 |
|
| 2326 |
procedure TCastleExamineNavigation.SetRotations(const Value: TQuaternion); |
| 2327 |
var |
| 2328 |
V: TExamineVectors; |
| 2329 |
begin |
| 2330 |
V := ExamineVectors; |
| 2331 |
V.Rotations := Value; |
| 2332 |
ExamineVectors := V; |
| 2333 |
end; |
| 2334 |
|
| 2335 |
function TCastleExamineNavigation.GetTranslation: TVector3; |
| 2336 |
begin |
| 2337 |
Result := ExamineVectors.Translation; |
| 2338 |
end; |
| 2339 |
|
| 2340 |
procedure TCastleExamineNavigation.SetTranslation(const Value: TVector3); |
| 2341 |
var |
| 2342 |
V: TExamineVectors; |
| 2343 |
begin |
| 2344 |
V := ExamineVectors; |
| 2345 |
V.Translation := Value; |
| 2346 |
ExamineVectors := V; |
| 2347 |
end; |
| 2348 |
|
| 2349 |
function TCastleExamineNavigation.EffectiveCenterOfRotation: TVector3; |
| 2350 |
var |
| 2351 |
B: TBox3D; |
| 2352 |
begin |
| 2353 |
if AutoCenterOfRotation then |
| 2354 |
begin |
| 2355 |
B := GoodModelBox; |
| 2356 |
if B.IsEmpty then |
| 2357 |
Result := Vector3(0, 0, 0) { any dummy value } |
| 2358 |
else |
| 2359 |
Result := B.Center; |
| 2360 |
end else |
| 2361 |
Result := CenterOfRotation; |
| 2362 |
end; |
| 2363 |
|
| 2364 |
function TCastleExamineNavigation.Press(const Event: TInputPressRelease): boolean; |
| 2365 |
|
| 2366 |
procedure CameraInitial; |
| 2367 |
var |
| 2368 |
APos, ADir, AUp, AGravityUp: TVector3; |
| 2369 |
begin |
| 2370 |
CameraViewpointForWholeScene(ModelBox, 2, 1, false, true, |
| 2371 |
APos, ADir, AUp, AGravityUp); |
| 2372 |
Camera.SetWorldView(APos, ADir, AUp); |
| 2373 |
Camera.GravityUp := AGravityUp; |
| 2374 |
end; |
| 2375 |
|
| 2376 |
begin |
| 2377 |
Result := inherited; |
| 2378 |
if Result or |
| 2379 |
(not Valid) or |
| 2380 |
(ModifiersDown(Container.Pressed) <> []) then |
| 2381 |
Exit; |
| 2382 |
|
| 2383 |
if (niGesture in UsingInput) and FPinchGestureRecognizer.Press(Event) then |
| 2384 |
Exit(true); |
| 2385 |
|
| 2386 |
if not (niNormal in UsingInput) then Exit; |
| 2387 |
|
| 2388 |
if Input_StopRotating.IsEvent(Event) then |
| 2389 |
begin |
| 2390 |
{ If StopRotating was useless, do not mark the event as "handled". |
| 2391 |
This is necessary to avoid having mouse clicks "stolen" by the TCastleExamineNavigation |
| 2392 |
when an empty TCastleViewport is being used |
| 2393 |
(and thus, mouse clicks could instead be used by other control). |
| 2394 |
It was necessary with deprecated TCastleControl/TCastleWindow: |
| 2395 |
on empty window, mouse clicks would be "mysteriously" intercepted, |
| 2396 |
since the default scene manager creates |
| 2397 |
examine camera, and it captures left mouse click as Input_StopRotating. } |
| 2398 |
if StopRotating then |
| 2399 |
Result := true; |
| 2400 |
end else |
| 2401 |
if Input_Home.IsEvent(Event) then |
| 2402 |
begin |
| 2403 |
CameraInitial; |
| 2404 |
Result := true; |
| 2405 |
end else |
| 2406 |
Result := false; |
| 2407 |
end; |
| 2408 |
|
| 2409 |
function TCastleExamineNavigation.Release(const Event: TInputPressRelease): boolean; |
| 2410 |
begin |
| 2411 |
Result := inherited; |
| 2412 |
if Result then Exit; |
| 2413 |
|
| 2414 |
if (niGesture in UsingInput) and FPinchGestureRecognizer.Release(Event) then |
| 2415 |
Exit(true); |
| 2416 |
end; |
| 2417 |
|
| 2418 |
function TCastleExamineNavigation.Motion(const Event: TInputMotion): boolean; |
| 2419 |
var |
| 2420 |
MoveDivConst: Single; |
| 2421 |
Dpi: Single; |
| 2422 |
|
| 2423 |
procedure DragRotation; |
| 2424 |
var |
| 2425 |
V: TExamineVectors; |
| 2426 |
|
| 2427 |
{ Returns new rotation } |
| 2428 |
function XYRotation(const Scale: Single): TQuaternion; |
| 2429 |
begin |
| 2430 |
if Turntable then |
| 2431 |
Result := |
| 2432 |
QuatFromAxisAngle(Vector3(1, 0, 0), Scale * (Event.OldPosition[1] - Event.Position[1]) / MoveDivConst) * |
| 2433 |
V.Rotations * |
| 2434 |
QuatFromAxisAngle(Vector3(0, 1, 0), Scale * (Event.Position[0] - Event.OldPosition[0]) / MoveDivConst) |
| 2435 |
else |
| 2436 |
Result := |
| 2437 |
QuatFromAxisAngle(Vector3(1, 0, 0), Scale * (Event.OldPosition[1] - Event.Position[1]) / MoveDivConst) * |
| 2438 |
QuatFromAxisAngle(Vector3(0, 1, 0), Scale * (Event.Position[0] - Event.OldPosition[0]) / MoveDivConst) * |
| 2439 |
V.Rotations; |
| 2440 |
end; |
| 2441 |
|
| 2442 |
var |
| 2443 |
W2, H2, AvgX, AvgY, ZRotAngle, ZRotRatio: Single; |
| 2444 |
begin |
| 2445 |
V := ExamineVectors; |
| 2446 |
|
| 2447 |
{$warnings off} // using deprecated ContainerSizeKnown |
| 2448 |
if (not ContainerSizeKnown) then |
| 2449 |
{$warnings on} |
| 2450 |
begin |
| 2451 |
V.Rotations := XYRotation(RotationSpeed); |
| 2452 |
end else |
| 2453 |
if Turntable then |
| 2454 |
begin |
| 2455 |
//Result := XYRotation(0.5); // this matches the rotation speed of ntExamine |
| 2456 |
{ Do one turn around Y axis by dragging from one viewport side to another |
| 2457 |
(so it does not depend on viewport size) } |
| 2458 |
V.Rotations := XYRotation(2 * Pi * MoveDivConst / Container.PixelsWidth * RotationSpeed); |
| 2459 |
end else |
| 2460 |
begin |
| 2461 |
{ When the cursor is close to the window edge, make rotation around Z axis. |
| 2462 |
This is called "virtual trackball" on |
| 2463 |
http://audilab.bme.mcgill.ca/~funnell/graphics/graphics3dview.html . } |
| 2464 |
{ clamp, since mouse positions may be wild } |
| 2465 |
AvgX := (Event.Position[0] + Event.OldPosition[0]) / 2; |
| 2466 |
AvgY := (Event.Position[1] + Event.OldPosition[1]) / 2; |
| 2467 |
{ let physical size affect scaling speed } |
| 2468 |
W2 := Container.PixelsWidth / 2; |
| 2469 |
H2 := Container.PixelsHeight / 2; |
| 2470 |
{ calculate rotation around Z } |
| 2471 |
ZRotAngle := |
| 2472 |
ArcTan2((Event.OldPosition[1] - H2) / H2, (Event.OldPosition[0] - W2) / W2) - |
| 2473 |
ArcTan2((Event. Position[1] - H2) / H2, (Event. Position[0] - W2) / W2); |
| 2474 |
{ ArcTan2 is in [-pi,pi]. When the mouse passes the border |
| 2475 |
of this range, we have to be secure. } |
| 2476 |
if ZRotAngle > Pi then |
| 2477 |
ZRotAngle := 2 * Pi - ZRotAngle else |
| 2478 |
if ZRotAngle < -Pi then |
| 2479 |
ZRotAngle := 2 * Pi + ZRotAngle; |
| 2480 |
{ how much do we want Z rotation, i.e. how far are we from window middle, |
| 2481 |
in 0..1 } |
| 2482 |
ZRotRatio := Min(1.0, Sqrt(Sqr((AvgX - W2) / W2) + Sqr((AvgY - H2) / H2))); |
| 2483 |
V.Rotations := |
| 2484 |
QuatFromAxisAngle(Vector3(0, 0, -1), ZRotRatio * ZRotAngle) * |
| 2485 |
XYRotation((1 - ZRotRatio) * RotationSpeed); |
| 2486 |
end; |
| 2487 |
|
| 2488 |
ExamineVectors := V; |
| 2489 |
end; |
| 2490 |
|
| 2491 |
procedure MoveNonExact; |
| 2492 |
var |
| 2493 |
Size: Single; |
| 2494 |
begin |
| 2495 |
Size := GoodModelBox.AverageSize(false, 1.0); |
| 2496 |
Translation := Translation - Vector3( |
| 2497 |
DragMoveSpeed * Size * (Event.OldPosition[0] - Event.Position[0]) |
| 2498 |
/ (2 * MoveDivConst), |
| 2499 |
DragMoveSpeed * Size * (Event.OldPosition[1] - Event.Position[1]) |
| 2500 |
/ (2 * MoveDivConst), |
| 2501 |
0); |
| 2502 |
end; |
| 2503 |
|
| 2504 |
procedure MoveExact; |
| 2505 |
var |
| 2506 |
V: TCastleViewport; |
| 2507 |
//RayOrigin, RayDirection, NewHitPoint, OldHitPoint: TVector3; |
| 2508 |
//HitDistance: Single; |
| 2509 |
begin |
| 2510 |
V := InternalViewport as TCastleViewport; |
| 2511 |
if (Camera.ProjectionType = ptOrthographic) and |
| 2512 |
TVector3.Equals(Camera.Direction, DefaultCameraDirection) and |
| 2513 |
TVector3.Equals(Camera.Up, DefaultCameraUp) then |
| 2514 |
begin |
| 2515 |
Translation := Translation + Vector3( |
| 2516 |
V.PositionTo2DWorld(Event.Position, true) - |
| 2517 |
V.PositionTo2DWorld(Event.OldPosition, true), |
| 2518 |
0); |
| 2519 |
end else |
| 2520 |
begin |
| 2521 |
{ general solution to ExactMovement, that works with any camera direction, |
| 2522 |
with both orthogonal and perspective. } |
| 2523 |
|
| 2524 |
// TODO: not ready yet, just fallback to non-ExactMovement implementation |
| 2525 |
MoveNonExact; |
| 2526 |
|
| 2527 |
(* |
| 2528 |
{ calculate HitDistance: distance to thing in viewport under the mouse. } |
| 2529 |
V.PositionToRay(Event.OldPosition, true, RayOrigin, RayDirection); |
| 2530 |
if V.Items.WorldRayCast(RayOrigin, RayDirection, HitDistance) = nil then |
| 2531 |
begin |
| 2532 |
{ if nothing under the mouse, assume the distance to the 3D plane |
| 2533 |
- that passes through GoodModelBox.Center |
| 2534 |
- that is parallel to camera plane |
| 2535 |
So just project vector (GoodModelBox.Center - Camera.Translation) |
| 2536 |
on Camera.Direction |
| 2537 |
( https://en.wikipedia.org/wiki/Dot_product ). } |
| 2538 |
HitDistance := TVector3.DotProduct( |
| 2539 |
GoodModelBox.Center - Camera.Translation, |
| 2540 |
Camera.Direction); |
| 2541 |
end; |
| 2542 |
OldHitPoint := RayOrigin + RayDirection * HitDistance; |
| 2543 |
|
| 2544 |
if not V.PositionToCameraPlane(Event.Position, true, HitDistance, NewHitPoint) then |
| 2545 |
begin |
| 2546 |
WritelnWarning('PositionToCameraPlane returned false in MoveExact, this indicates extreme camera fov'); |
| 2547 |
Exit; |
| 2548 |
end; |
| 2549 |
|
| 2550 |
Translation := Translation + NewHitPoint - OldHitPoint; |
| 2551 |
*) |
| 2552 |
end; |
| 2553 |
end; |
| 2554 |
|
| 2555 |
begin |
| 2556 |
Result := inherited; |
| 2557 |
if Result then Exit; |
| 2558 |
|
| 2559 |
if Container <> nil then |
| 2560 |
Dpi := Container.Dpi |
| 2561 |
else |
| 2562 |
Dpi := DefaultDpi; |
| 2563 |
|
| 2564 |
if (niGesture in UsingInput) and FPinchGestureRecognizer.Motion(Event, Dpi) then |
| 2565 |
Exit(true); |
| 2566 |
|
| 2567 |
MoveDivConst := Dpi; |
| 2568 |
|
| 2569 |
{ When dragging should be ignored, or (it's an optimization to check it |
| 2570 |
here early, Motion occurs very often) when nothing pressed, do nothing. } |
| 2571 |
if (Container.MousePressed = []) or |
| 2572 |
(not ReallyEnableMouseDragging) or |
| 2573 |
(MouseDraggingStarted <> Event.FingerIndex) or |
| 2574 |
(not Valid) then |
| 2575 |
Exit; |
| 2576 |
|
| 2577 |
if RotationEnabled and Input_Rotate.IsPressed(Container.Pressed, Container.MousePressed) then |
| 2578 |
begin |
| 2579 |
DragRotation; |
| 2580 |
Result := true; |
| 2581 |
end; |
| 2582 |
|
| 2583 |
if ZoomEnabled and Input_Zoom.IsPressed(Container.Pressed, Container.MousePressed) then |
| 2584 |
begin |
| 2585 |
if Zoom((Event.OldPosition[1] - Event.Position[1]) * 30 / (2 * MoveDivConst)) then |
| 2586 |
Result := true; |
| 2587 |
end; |
| 2588 |
|
| 2589 |
if MoveEnabled and Input_Move.IsPressed(Container.Pressed, Container.MousePressed) then |
| 2590 |
begin |
| 2591 |
if ExactMovement and (InternalViewport <> nil) and (not GoodModelBox.IsEmpty) then |
| 2592 |
begin |
| 2593 |
MoveExact; |
| 2594 |
end else |
| 2595 |
begin |
| 2596 |
MoveNonExact; |
| 2597 |
end; |
| 2598 |
Result := true; |
| 2599 |
end; |
| 2600 |
end; |
| 2601 |
|
| 2602 |
procedure TCastleExamineNavigation.OnGestureRecognized(Sender: TObject); |
| 2603 |
const |
| 2604 |
PinchZoomSpeed = 20.0; |
| 2605 |
var |
| 2606 |
Recognizer: TCastlePinchPanGestureRecognizer; |
| 2607 |
Factor, Size, MoveDivConst, ZoomScale: Single; |
| 2608 |
begin |
| 2609 |
Recognizer := Sender as TCastlePinchPanGestureRecognizer; |
| 2610 |
if Recognizer = nil then Exit; |
| 2611 |
|
| 2612 |
if Container <> nil then |
| 2613 |
MoveDivConst := Container.Dpi else |
| 2614 |
MoveDivConst := 100; |
| 2615 |
|
| 2616 |
if ZoomEnabled and (Recognizer.Gesture = gtPinch) then |
| 2617 |
begin |
| 2618 |
if Recognizer.PinchScaleFactor > 1.0 then |
| 2619 |
Factor := Recognizer.PinchScaleFactor - 1.0 |
| 2620 |
else |
| 2621 |
Factor := - (1.0 / Recognizer.PinchScaleFactor - 1.0); |
| 2622 |
if Turntable then |
| 2623 |
ZoomScale := 1 |
| 2624 |
else |
| 2625 |
ZoomScale := 3; |
| 2626 |
Zoom(PinchZoomSpeed * Factor * ZoomScale); |
| 2627 |
end; |
| 2628 |
|
| 2629 |
if MoveEnabled and (not GoodModelBox.IsEmpty) and (Recognizer.Gesture = gtPan) then |
| 2630 |
begin |
| 2631 |
Size := GoodModelBox.AverageSize; |
| 2632 |
Translation := Translation - Vector3( |
| 2633 |
DragMoveSpeed * Size * Recognizer.PanMove.X / (2 * MoveDivConst), |
| 2634 |
DragMoveSpeed * Size * Recognizer.PanMove.Y / (2 * MoveDivConst), |
| 2635 |
0); |
| 2636 |
end; |
| 2637 |
end; |
| 2638 |
|
| 2639 |
function TCastleExamineNavigation.GetInput_MoveXInc: TInputShortcut; begin Result := Inputs_Move[0, true ] end; |
| 2640 |
function TCastleExamineNavigation.GetInput_MoveXDec: TInputShortcut; begin Result := Inputs_Move[0, false] end; |
| 2641 |
function TCastleExamineNavigation.GetInput_MoveYInc: TInputShortcut; begin Result := Inputs_Move[1, true ] end; |
| 2642 |
function TCastleExamineNavigation.GetInput_MoveYDec: TInputShortcut; begin Result := Inputs_Move[1, false] end; |
| 2643 |
function TCastleExamineNavigation.GetInput_MoveZInc: TInputShortcut; begin Result := Inputs_Move[2, true ] end; |
| 2644 |
function TCastleExamineNavigation.GetInput_MoveZDec: TInputShortcut; begin Result := Inputs_Move[2, false] end; |
| 2645 |
function TCastleExamineNavigation.GetInput_RotateXInc: TInputShortcut; begin Result := Inputs_Rotate[0, true ] end; |
| 2646 |
function TCastleExamineNavigation.GetInput_RotateXDec: TInputShortcut; begin Result := Inputs_Rotate[0, false] end; |
| 2647 |
function TCastleExamineNavigation.GetInput_RotateYInc: TInputShortcut; begin Result := Inputs_Rotate[1, true ] end; |
| 2648 |
function TCastleExamineNavigation.GetInput_RotateYDec: TInputShortcut; begin Result := Inputs_Rotate[1, false] end; |
| 2649 |
function TCastleExamineNavigation.GetInput_RotateZInc: TInputShortcut; begin Result := Inputs_Rotate[2, true ] end; |
| 2650 |
function TCastleExamineNavigation.GetInput_RotateZDec: TInputShortcut; begin Result := Inputs_Rotate[2, false] end; |
| 2651 |
|
| 2652 |
function TCastleExamineNavigation.GetMouseNavigation: boolean; |
| 2653 |
begin |
| 2654 |
Result := niMouseDragging in UsingInput; |
| 2655 |
end; |
| 2656 |
|
| 2657 |
procedure TCastleExamineNavigation.SetMouseNavigation(const Value: boolean); |
| 2658 |
begin |
| 2659 |
if Value then |
| 2660 |
Input := Input + [niMouseDragging] |
| 2661 |
else |
| 2662 |
Input := Input - [niMouseDragging]; |
| 2663 |
end; |
| 2664 |
|
| 2665 |
function TCastleExamineNavigation.PropertySections( |
| 2666 |
const PropertyName: String): TPropertySections; |
| 2667 |
begin |
| 2668 |
if (PropertyName = 'MoveEnabled') or |
| 2669 |
(PropertyName = 'RotationEnabled') or |
| 2670 |
(PropertyName = 'RotationAccelerate') or |
| 2671 |
(PropertyName = 'ExactMovement') then |
| 2672 |
Result := [psBasic] |
| 2673 |
else |
| 2674 |
Result := inherited PropertySections(PropertyName); |
| 2675 |
end; |
| 2676 |
|
| 2677 |
{ TCastle2DNavigation -------------------------------------------------------- } |
| 2678 |
|
| 2679 |
constructor TCastle2DNavigation.Create(AOwner: TComponent); |
| 2680 |
begin |
| 2681 |
inherited Create(AOwner); |
| 2682 |
|
| 2683 |
RotationEnabled := false; |
| 2684 |
|
| 2685 |
{ move with left mouse button, no modifiers } |
| 2686 |
Input_Move.MouseButton := buttonLeft; |
| 2687 |
Input_Move.MouseButtonCheckModifiers := [mkShift, mkCtrl]; |
| 2688 |
Input_Move.MouseButtonModifiers := []; |
| 2689 |
Input_Move.MouseButton2Use := false; |
| 2690 |
{ no mouse dragging for zoom (but still you can do zoom with mouse wheel) } |
| 2691 |
Input_Zoom.MakeClear; |
| 2692 |
end; |
| 2693 |
|
| 2694 |
{ TCastleMouseLookNavigation ------------------------------------------------- } |
| 2695 |
|
| 2696 |
constructor TCastleMouseLookNavigation.Create(AOwner: TComponent); |
| 2697 |
begin |
| 2698 |
inherited; |
| 2699 |
FMouseLookHorizontalSensitivity := DefaultMouseLookHorizontalSensitivity; |
| 2700 |
FMouseLookVerticalSensitivity := DefaultMouseLookVerticalSensitivity; |
| 2701 |
FInvertVerticalMouseLook := false; |
| 2702 |
end; |
| 2703 |
|
| 2704 |
procedure TCastleMouseLookNavigation.Update(const SecondsPassed: Single; |
| 2705 |
var HandleInput: boolean); |
| 2706 |
|
| 2707 |
procedure MouseLookUpdate; |
| 2708 |
begin |
| 2709 |
if InternalUsingMouseLook and (Container <> nil) then |
| 2710 |
Container.MouseLookUpdate; |
| 2711 |
end; |
| 2712 |
|
| 2713 |
begin |
| 2714 |
inherited; |
| 2715 |
MouseLookUpdate; |
| 2716 |
end; |
| 2717 |
|
| 2718 |
procedure TCastleMouseLookNavigation.SetMouseLook(const Value: boolean); |
| 2719 |
begin |
| 2720 |
if FMouseLook <> Value then |
| 2721 |
begin |
| 2722 |
FMouseLook := Value; |
| 2723 |
if InternalUsingMouseLook then |
| 2724 |
begin |
| 2725 |
Cursor := mcForceNone; |
| 2726 |
if Container <> nil then |
| 2727 |
Container.MouseLookPress; |
| 2728 |
end else |
| 2729 |
Cursor := mcDefault; |
| 2730 |
end; |
| 2731 |
end; |
| 2732 |
|
| 2733 |
procedure TCastleMouseLookNavigation.ProcessMouseLookDelta(const Delta: TVector2); |
| 2734 |
begin |
| 2735 |
// nothing in this class |
| 2736 |
end; |
| 2737 |
|
| 2738 |
function TCastleMouseLookNavigation.Motion(const Event: TInputMotion): boolean; |
| 2739 |
|
| 2740 |
procedure HandleMouseLook; |
| 2741 |
var |
| 2742 |
MouseChange: TVector2; |
| 2743 |
begin |
| 2744 |
MouseChange := Container.MouseLookDelta(Event, RenderRect); |
| 2745 |
|
| 2746 |
if not MouseChange.IsPerfectlyZero then |
| 2747 |
begin |
| 2748 |
if InvertVerticalMouseLook then |
| 2749 |
MouseChange.Y := -MouseChange.Y; |
| 2750 |
MouseChange.X := MouseChange.X * MouseLookHorizontalSensitivity; |
| 2751 |
MouseChange.Y := MouseChange.Y * MouseLookVerticalSensitivity; |
| 2752 |
ProcessMouseLookDelta(MouseChange); |
| 2753 |
Result := true; |
| 2754 |
end; |
| 2755 |
end; |
| 2756 |
|
| 2757 |
begin |
| 2758 |
Result := inherited; |
| 2759 |
if Result or (Event.FingerIndex <> 0) then Exit; |
| 2760 |
|
| 2761 |
{$warnings off} // using deprecated ContainerSizeKnown |
| 2762 |
if InternalUsingMouseLook and |
| 2763 |
Container.Focused and |
| 2764 |
ContainerSizeKnown and |
| 2765 |
Valid then |
| 2766 |
{$warnings on} |
| 2767 |
begin |
| 2768 |
HandleMouseLook; |
| 2769 |
Exit; |
| 2770 |
end; |
| 2771 |
end; |
| 2772 |
|
| 2773 |
function TCastleMouseLookNavigation.InternalUsingMouseLook: Boolean; |
| 2774 |
begin |
| 2775 |
Result := MouseLook and (niNormal in UsingInput); |
| 2776 |
|
| 2777 |
{ Note: we used to have here condition "and (not CastleDesignMode)" |
| 2778 |
as escaping from MouseLook was impossible, if you enable it in Object Inspector. |
| 2779 |
But it is OK now: our TCastleWalkNavigationDesign makes mouse look intuitive to use. } |
| 2780 |
end; |
| 2781 |
|
| 2782 |
function TCastleMouseLookNavigation.PropertySections(const PropertyName: String): TPropertySections; |
| 2783 |
begin |
| 2784 |
if ArrayContainsString(PropertyName, [ |
| 2785 |
'MouseLook', 'MouseLookHorizontalSensitivity', 'MouseLookVerticalSensitivity', |
| 2786 |
'InvertVerticalMouseLook' |
| 2787 |
]) then |
| 2788 |
Result := [psBasic] |
| 2789 |
else |
| 2790 |
Result := inherited PropertySections(PropertyName); |
| 2791 |
|
| 2792 |
end; |
| 2793 |
|
| 2794 |
{ TCastleWalkNavigation ---------------------------------------------------------------- } |
| 2795 |
|
| 2796 |
constructor TCastleWalkNavigation.Create(AOwner: TComponent); |
| 2797 |
begin |
| 2798 |
inherited; |
| 2799 |
|
| 2800 |
FRotationHorizontalSpeed := DefaultRotationHorizontalSpeed; |
| 2801 |
FRotationVerticalSpeed := DefaultRotationVerticalSpeed; |
| 2802 |
FFallSpeedStart := DefaultFallSpeedStart; |
| 2803 |
FFallSpeedIncrease := DefaultFallSpeedIncrease; |
| 2804 |
FPreferGravityUpForRotations := true; |
| 2805 |
FPreferGravityUpForMoving := true; |
| 2806 |
FGravity := true; |
| 2807 |
FGrowSpeed := DefaultGrowSpeed; |
| 2808 |
FFallingEffect := true; |
| 2809 |
FIsJumping := false; |
| 2810 |
FJumpMaxHeight := DefaultJumpMaxHeight; |
| 2811 |
FMinAngleFromGravityUp := DefaultMinAngleFromGravityUp; |
| 2812 |
FAllowSlowerRotations := true; |
| 2813 |
FCheckModsDown := true; |
| 2814 |
FJumpHorizontalSpeedMultiply := DefaultJumpHorizontalSpeedMultiply; |
| 2815 |
FJumpTime := DefaultJumpTime; |
| 2816 |
FMouseDraggingHorizontalRotationSpeed := DefaultMouseDraggingHorizontalRotationSpeed; |
| 2817 |
FMouseDraggingVerticalRotationSpeed := DefaultMouseDraggingVerticalRotationSpeed; |
| 2818 |
FMouseDraggingMoveSpeed := DefaultMouseDraggingMoveSpeed; |
| 2819 |
FMoveHorizontalSpeed := 1; |
| 2820 |
FMoveVerticalSpeed := 1; |
| 2821 |
FMoveSpeed := 1; |
| 2822 |
FMoveSpeedMin := DefaultMoveSpeedMin; |
| 2823 |
FMoveSpeedMax := DefaultMoveSpeedMax; |
| 2824 |
FPreferredHeight := DefaultPreferredHeight; |
| 2825 |
FHeadBobbing := DefaultHeadBobbing; |
| 2826 |
FHeadBobbingTime := DefaultHeadBobbingTime; |
| 2827 |
FCrouchHeight := DefaultCrouchHeight; |
| 2828 |
|
| 2829 |
FInput_Forward := TInputShortcut.Create(Self); |
| 2830 |
FInput_Backward := TInputShortcut.Create(Self); |
| 2831 |
FInput_LeftRotate := TInputShortcut.Create(Self); |
| 2832 |
FInput_RightRotate := TInputShortcut.Create(Self); |
| 2833 |
FInput_LeftStrafe := TInputShortcut.Create(Self); |
| 2834 |
FInput_RightStrafe := TInputShortcut.Create(Self); |
| 2835 |
FInput_UpRotate := TInputShortcut.Create(Self); |
| 2836 |
FInput_DownRotate := TInputShortcut.Create(Self); |
| 2837 |
FInput_IncreasePreferredHeight := TInputShortcut.Create(Self); |
| 2838 |
FInput_DecreasePreferredHeight := TInputShortcut.Create(Self); |
| 2839 |
FInput_GravityUp := TInputShortcut.Create(Self); |
| 2840 |
FInput_MoveSpeedInc := TInputShortcut.Create(Self); |
| 2841 |
FInput_MoveSpeedDec := TInputShortcut.Create(Self); |
| 2842 |
FInput_Jump := TInputShortcut.Create(Self); |
| 2843 |
FInput_Crouch := TInputShortcut.Create(Self); |
| 2844 |
FInput_Run := TInputShortcut.Create(Self); |
| 2845 |
|
| 2846 |
Input_Forward .Assign(keyW, keyArrowUp); |
| 2847 |
Input_Backward .Assign(keyS, keyArrowDown); |
| 2848 |
Input_LeftRotate .Assign(keyArrowLeft); |
| 2849 |
Input_RightRotate .Assign(keyArrowRight); |
| 2850 |
Input_LeftStrafe .Assign(keyA); |
| 2851 |
Input_RightStrafe .Assign(keyD); |
| 2852 |
Input_UpRotate .Assign(keyNone); |
| 2853 |
Input_DownRotate .Assign(keyNone); |
| 2854 |
Input_IncreasePreferredHeight .Assign(keyNone); |
| 2855 |
Input_DecreasePreferredHeight .Assign(keyNone); |
| 2856 |
Input_GravityUp .Assign(keyNone); |
| 2857 |
{ For move speed we use also character codes +/-, as numpad |
| 2858 |
may be hard to reach on some keyboards (e.g. on laptops). } |
| 2859 |
Input_MoveSpeedInc .Assign(keyNumpadPlus , keyNone, '+'); |
| 2860 |
Input_MoveSpeedDec .Assign(keyNumpadMinus, keyNone, '-'); |
| 2861 |
Input_Jump .Assign(keySpace); |
| 2862 |
Input_Crouch .Assign(keyC); |
| 2863 |
Input_Run .Assign(keyShift); |
| 2864 |
|
| 2865 |
Input_Forward .SetSubComponent(true); |
| 2866 |
Input_Backward .SetSubComponent(true); |
| 2867 |
Input_LeftRotate .SetSubComponent(true); |
| 2868 |
Input_RightRotate .SetSubComponent(true); |
| 2869 |
Input_LeftStrafe .SetSubComponent(true); |
| 2870 |
Input_RightStrafe .SetSubComponent(true); |
| 2871 |
Input_UpRotate .SetSubComponent(true); |
| 2872 |
Input_DownRotate .SetSubComponent(true); |
| 2873 |
Input_IncreasePreferredHeight.SetSubComponent(true); |
| 2874 |
Input_DecreasePreferredHeight.SetSubComponent(true); |
| 2875 |
Input_GravityUp .SetSubComponent(true); |
| 2876 |
Input_MoveSpeedInc .SetSubComponent(true); |
| 2877 |
Input_MoveSpeedDec .SetSubComponent(true); |
| 2878 |
Input_Jump .SetSubComponent(true); |
| 2879 |
Input_Crouch .SetSubComponent(true); |
| 2880 |
Input_Run .SetSubComponent(true); |
| 2881 |
|
| 2882 |
Input_Forward .Name := 'Input_Forward'; |
| 2883 |
Input_Backward .Name := 'Input_Backward'; |
| 2884 |
Input_LeftRotate .Name := 'Input_LeftRotate'; |
| 2885 |
Input_RightRotate .Name := 'Input_RightRotate'; |
| 2886 |
Input_LeftStrafe .Name := 'Input_LeftStrafe'; |
| 2887 |
Input_RightStrafe .Name := 'Input_RightStrafe'; |
| 2888 |
Input_UpRotate .Name := 'Input_UpRotate'; |
| 2889 |
Input_DownRotate .Name := 'Input_DownRotate'; |
| 2890 |
Input_IncreasePreferredHeight.Name := 'Input_IncreasePreferredHeight'; |
| 2891 |
Input_DecreasePreferredHeight.Name := 'Input_DecreasePreferredHeight'; |
| 2892 |
Input_GravityUp .Name := 'Input_GravityUp'; |
| 2893 |
Input_MoveSpeedInc .Name := 'Input_MoveSpeedInc'; |
| 2894 |
Input_MoveSpeedDec .Name := 'Input_MoveSpeedDec'; |
| 2895 |
Input_Jump .Name := 'Input_Jump'; |
| 2896 |
Input_Crouch .Name := 'Input_Crouch'; |
| 2897 |
Input_Run .Name := 'Input_Run'; |
| 2898 |
end; |
| 2899 |
|
| 2900 |
destructor TCastleWalkNavigation.Destroy; |
| 2901 |
begin |
| 2902 |
inherited; |
| 2903 |
end; |
| 2904 |
|
| 2905 |
function TCastleWalkNavigation.MoveAllowed( |
| 2906 |
const OldPos: TVector3; ProposedNewPos: TVector3; out NewPos: TVector3; |
| 2907 |
const BecauseOfGravity, CheckClimbHeight: Boolean): Boolean; |
| 2908 |
var |
| 2909 |
NewIsAbove: boolean; |
| 2910 |
NewAboveHeight, OldAbsoluteHeight, NewAbsoluteHeight: Single; |
| 2911 |
NewAboveGround: PTriangle; |
| 2912 |
begin |
| 2913 |
Result := inherited; |
| 2914 |
|
| 2915 |
if Result and Gravity and CheckClimbHeight and (ClimbHeight <> 0) and IsAbove and |
| 2916 |
{ if we're already below ClimbHeight then do not check if new position |
| 2917 |
satisfies ClimbHeight requirement. This may prevent camera blocking |
| 2918 |
in weird situations, e.g. if were forcefully pushed into some position |
| 2919 |
(e.g. because player is hit by a missile with a knockback, or teleported |
| 2920 |
or such). } |
| 2921 |
(AboveHeight > ClimbHeight) then |
| 2922 |
begin |
| 2923 |
Height(NewPos, NewIsAbove, NewAboveHeight, NewAboveGround); |
| 2924 |
if NewIsAbove then |
| 2925 |
begin |
| 2926 |
OldAbsoluteHeight := TVector3.DotProduct(Camera.GravityUp, OldPos); |
| 2927 |
NewAbsoluteHeight := TVector3.DotProduct(Camera.GravityUp, NewPos); |
| 2928 |
Result := not ( |
| 2929 |
AboveHeight - NewAboveHeight - (OldAbsoluteHeight - NewAbsoluteHeight) > |
| 2930 |
ClimbHeight ); |
| 2931 |
// useful log to test ClimbHeight, but too spammy to be enabled by default |
| 2932 |
// if Log and not Result then |
| 2933 |
// WritelnLog('Camera', 'Blocked move because of ClimbHeight (%f).', [ClimbHeight]); |
| 2934 |
end; |
| 2935 |
end; |
| 2936 |
end; |
| 2937 |
|
| 2938 |
procedure TCastleWalkNavigation.CorrectPreferredHeight; |
| 2939 |
begin |
| 2940 |
CastleCameras.CorrectPreferredHeight( |
| 2941 |
FPreferredHeight, Radius, CrouchHeight, HeadBobbing); |
| 2942 |
end; |
| 2943 |
|
| 2944 |
function TCastleWalkNavigation.UseHeadBobbing: boolean; |
| 2945 |
begin |
| 2946 |
Result := Gravity and (HeadBobbing <> 0.0); |
| 2947 |
end; |
| 2948 |
|
| 2949 |
function TCastleWalkNavigation.RealPreferredHeightNoHeadBobbing: Single; |
| 2950 |
begin |
| 2951 |
Result := PreferredHeight; |
| 2952 |
|
| 2953 |
if IsCrouching then |
| 2954 |
Result := Result * CrouchHeight; |
| 2955 |
end; |
| 2956 |
|
| 2957 |
function TCastleWalkNavigation.RealPreferredHeight: Single; |
| 2958 |
var |
| 2959 |
BobbingModifier: Single; |
| 2960 |
begin |
| 2961 |
Result := RealPreferredHeightNoHeadBobbing; |
| 2962 |
|
| 2963 |
if UseHeadBobbing then |
| 2964 |
begin |
| 2965 |
{ HeadBobbingPosition = 0 means that head is at lowest position. |
| 2966 |
HeadBobbingPosition = 0.5 means that head is at highest position. |
| 2967 |
HeadBobbingPosition = 1.0 means that head is at lowest position again. |
| 2968 |
|
| 2969 |
Larger HeadBobbingPosition work like Frac(HeadBobbingPosition) |
| 2970 |
(i.e. function HeadBobbingPosition -> BobbingModifier |
| 2971 |
is periodic with period = 1.0). } |
| 2972 |
|
| 2973 |
BobbingModifier := Frac(HeadBobbingPosition); |
| 2974 |
|
| 2975 |
if BobbingModifier <= 0.5 then |
| 2976 |
BobbingModifier := MapRange(BobbingModifier, 0.0, 0.5, -1, +1) else |
| 2977 |
BobbingModifier := MapRange(BobbingModifier, 0.5, 1.0, +1, -1); |
| 2978 |
|
| 2979 |
{ Most game tutorials and codes advice that head bobbing be done with sinus, |
| 2980 |
as below. But actually I found that the visual difference between |
| 2981 |
sin-based head bobbing and linear-based (like above) head bobbing |
| 2982 |
is not noticeable, so I'm using linear-based right now (as it's |
| 2983 |
a little faster --- no trig calculation needed, although this |
| 2984 |
could be avoided with sinus lookup table). |
| 2985 |
|
| 2986 |
If however you prefer sin-based head bobbing, uncomment line below |
| 2987 |
and comment out 3 lines "if BobbingModifier <= 0.5 then ...." above. |
| 2988 |
|
| 2989 |
BobbingModifier := Sin(BobbingModifier * 2 * Pi); |
| 2990 |
} |
| 2991 |
|
| 2992 |
BobbingModifier := BobbingModifier * (Result * HeadBobbing); |
| 2993 |
Result := Result + BobbingModifier; |
| 2994 |
end; |
| 2995 |
end; |
| 2996 |
|
| 2997 |
function TCastleWalkNavigation.RealPreferredHeightMargin: Single; |
| 2998 |
begin |
| 2999 |
{ I tried using here something smaller like |
| 3000 |
SingleEpsilon, but this was not good. } |
| 3001 |
Result := RealPreferredHeight * 0.01; |
| 3002 |
end; |
| 3003 |
|
| 3004 |
function TCastleWalkNavigation.AdjustPositionForRotationHorizontalPivot( |
| 3005 |
const OldPosition: TVector3; |
| 3006 |
const OldDirection, NewDirection: TVector3): TVector3; |
| 3007 |
var |
| 3008 |
Pivot, OldDirectionInGravityPlane, NewDirectionInGravityPlane: TVector3; |
| 3009 |
begin |
| 3010 |
Result := OldPosition; |
| 3011 |
{$warnings off} // using deprecated RotationHorizontalPivot to keep it working |
| 3012 |
if RotationHorizontalPivot <> 0 then |
| 3013 |
begin |
| 3014 |
if PreferGravityUpForRotations then |
| 3015 |
begin |
| 3016 |
Pivot := OldPosition + OldDirection * RotationHorizontalPivot; |
| 3017 |
Result := Pivot - NewDirection * RotationHorizontalPivot; |
| 3018 |
end else |
| 3019 |
begin |
| 3020 |
OldDirectionInGravityPlane := OldDirection; |
| 3021 |
if not VectorsParallel(OldDirectionInGravityPlane, Camera.GravityUp) then |
| 3022 |
MakeVectorsOrthoOnTheirPlane(OldDirectionInGravityPlane, Camera.GravityUp); |
| 3023 |
NewDirectionInGravityPlane := NewDirection; |
| 3024 |
if not VectorsParallel(NewDirectionInGravityPlane, Camera.GravityUp) then |
| 3025 |
MakeVectorsOrthoOnTheirPlane(NewDirectionInGravityPlane, Camera.GravityUp); |
| 3026 |
Pivot := OldPosition + OldDirectionInGravityPlane * RotationHorizontalPivot; |
| 3027 |
Result := Pivot - NewDirectionInGravityPlane * RotationHorizontalPivot; |
| 3028 |
end; |
| 3029 |
end; |
| 3030 |
{$warnings on} |
| 3031 |
end; |
| 3032 |
|
| 3033 |
procedure TCastleWalkNavigation.RotateAroundGravityUp(const Angle: Single); |
| 3034 |
var |
| 3035 |
GravityAxis, |
| 3036 |
OldPosition, OldDirection, OldUp, |
| 3037 |
NewPosition, NewDirection, NewUp: TVector3; |
| 3038 |
begin |
| 3039 |
Camera.GetWorldView(OldPosition, OldDirection, OldUp); |
| 3040 |
|
| 3041 |
{ We need to sometimes use GravityUp, sometimes -GravityUp, |
| 3042 |
to intuitively keep rotations to left -> going left even |
| 3043 |
when looking upside-down. } |
| 3044 |
if AngleRadBetweenVectors(OldUp, Camera.GravityUp) > Pi/2 then |
| 3045 |
GravityAxis := -Camera.GravityUp |
| 3046 |
else |
| 3047 |
GravityAxis := Camera.GravityUp; |
| 3048 |
|
| 3049 |
NewUp := RotatePointAroundAxisRad(Angle, OldUp, GravityAxis); |
| 3050 |
NewDirection := RotatePointAroundAxisRad(Angle, OldDirection, GravityAxis); |
| 3051 |
|
| 3052 |
NewPosition := AdjustPositionForRotationHorizontalPivot(OldPosition, OldDirection, NewDirection); |
| 3053 |
|
| 3054 |
Camera.SetWorldView(NewPosition, NewDirection, NewUp); |
| 3055 |
end; |
| 3056 |
|
| 3057 |
procedure TCastleWalkNavigation.RotateAroundUp(const Angle: Single); |
| 3058 |
var |
| 3059 |
OldPosition, OldDirection, OldUp, NewPosition, NewDirection: TVector3; |
| 3060 |
begin |
| 3061 |
Camera.GetWorldView(OldPosition, OldDirection, OldUp); |
| 3062 |
|
| 3063 |
{ We know that RotatePointAroundAxisRad below doesn't change the length |
| 3064 |
of the NewDirection (so it will remain normalized, like OldDirection) |
| 3065 |
and it will keep NewDirection and OldUp vectors orthogonal. } |
| 3066 |
NewDirection := RotatePointAroundAxisRad(Angle, OldDirection, OldUp); |
| 3067 |
|
| 3068 |
NewPosition := AdjustPositionForRotationHorizontalPivot(OldPosition, OldDirection, NewDirection); |
| 3069 |
|
| 3070 |
Camera.SetWorldView(NewPosition, NewDirection, OldUp); |
| 3071 |
end; |
| 3072 |
|
| 3073 |
procedure TCastleWalkNavigation.RotateHorizontal(const Angle: Single); |
| 3074 |
begin |
| 3075 |
if PreferGravityUpForRotations then |
| 3076 |
RotateAroundGravityUp(Angle) |
| 3077 |
else |
| 3078 |
RotateAroundUp(Angle); |
| 3079 |
end; |
| 3080 |
|
| 3081 |
procedure TCastleWalkNavigation.RotateVertical(AngleRad: Single); |
| 3082 |
var |
| 3083 |
Side: TVector3; |
| 3084 |
GravityAxis, OldPosition, OldDirection, OldUp, NewDirection, NewUp: TVector3; |
| 3085 |
AngleRadBetween: Single; |
| 3086 |
begin |
| 3087 |
Camera.GetWorldView(OldPosition, OldDirection, OldUp); |
| 3088 |
|
| 3089 |
{ We need to sometimes use GravityUp, sometimes -GravityUp, |
| 3090 |
to intuitively keep rotations to left -> going left even |
| 3091 |
when looking upside-down. } |
| 3092 |
if AngleRadBetweenVectors(OldUp, Camera.GravityUp) > Pi/2 then |
| 3093 |
GravityAxis := -Camera.GravityUp |
| 3094 |
else |
| 3095 |
GravityAxis := Camera.GravityUp; |
| 3096 |
|
| 3097 |
if PreferGravityUpForRotations then |
| 3098 |
begin |
| 3099 |
Side := TVector3.CrossProduct(OldDirection, GravityAxis); |
| 3100 |
if Side.IsZero then |
| 3101 |
begin |
| 3102 |
{ if OldDirection is parallel to GravityAxis, |
| 3103 |
then realizing PreferGravityUpForRotations is not really possible. |
| 3104 |
Allow rotation as if PreferGravityUpForRotations = false. |
| 3105 |
This is important to do right, to allow in CGE editor to use mouse look |
| 3106 |
right after pressing Top (7). } |
| 3107 |
Side := TVector3.CrossProduct(OldDirection, OldUp); |
| 3108 |
end else |
| 3109 |
if MinAngleFromGravityUp <> 0 then |
| 3110 |
begin |
| 3111 |
{ Calculate AngleRadBetween, and possibly adjust AngleRad. } |
| 3112 |
AngleRadBetween := AngleRadBetweenVectors(OldDirection, GravityAxis); |
| 3113 |
|
| 3114 |
if AngleRadBetween - AngleRad < MinAngleFromGravityUp then |
| 3115 |
AngleRad := AngleRadBetween - MinAngleFromGravityUp |
| 3116 |
else |
| 3117 |
if AngleRadBetween - AngleRad > Pi - MinAngleFromGravityUp then |
| 3118 |
AngleRad := AngleRadBetween - (Pi - MinAngleFromGravityUp); |
| 3119 |
end; |
| 3120 |
end else |
| 3121 |
begin |
| 3122 |
Side := TVector3.CrossProduct(OldDirection, OldUp); |
| 3123 |
end; |
| 3124 |
|
| 3125 |
{ Rotate NewUp around Side } |
| 3126 |
NewUp := RotatePointAroundAxisRad(AngleRad, OldUp , Side); |
| 3127 |
{ Rotate NewDirection around Side } |
| 3128 |
NewDirection := RotatePointAroundAxisRad(AngleRad, OldDirection, Side); |
| 3129 |
|
| 3130 |
Camera.SetWorldView(OldPosition, NewDirection, NewUp); |
| 3131 |
end; |
| 3132 |
|
| 3133 |
procedure TCastleWalkNavigation.MoveHorizontal(Dir: TVector3; |
| 3134 |
const SecondsPassed: Single); |
| 3135 |
var |
| 3136 |
Multiplier: Single; |
| 3137 |
Grav: TVector3; |
| 3138 |
begin |
| 3139 |
Multiplier := MoveSpeed * MoveHorizontalSpeed * SecondsPassed; |
| 3140 |
if IsJumping then |
| 3141 |
Multiplier := Multiplier * JumpHorizontalSpeedMultiply; |
| 3142 |
if Input_Run.IsPressed(Container) then |
| 3143 |
Multiplier := Multiplier * 2; |
| 3144 |
|
| 3145 |
{ Update HeadBobbingPosition } |
| 3146 |
if (not IsJumping) and UseHeadBobbing and (not HeadBobbingAlreadyDone) then |
| 3147 |
begin |
| 3148 |
HeadBobbingPosition := HeadBobbingPosition + (SecondsPassed / HeadBobbingTime); |
| 3149 |
HeadBobbingAlreadyDone := true; |
| 3150 |
end; |
| 3151 |
|
| 3152 |
if PreferGravityUpForMoving then |
| 3153 |
begin |
| 3154 |
Grav := GravityUpLocal; |
| 3155 |
if not VectorsParallel(Dir, Grav) then |
| 3156 |
MakeVectorsOrthoOnTheirPlane(Dir, Grav) |
| 3157 |
else |
| 3158 |
{ Do not move at all, if Dir and Grav parallel. |
| 3159 |
This avoids moving vertically in such case. } |
| 3160 |
EXit; |
| 3161 |
end; |
| 3162 |
|
| 3163 |
MoveHorizontalDone := true; |
| 3164 |
Move(Dir * Multiplier, false, true); |
| 3165 |
end; |
| 3166 |
|
| 3167 |
procedure TCastleWalkNavigation.MoveVertical(const SecondsPassed: Single; const Multiply: Integer); |
| 3168 |
|
| 3169 |
{ Provided PreferredUpVector must be already normalized. } |
| 3170 |
procedure MoveVerticalCore(const PreferredUpVector: TVector3); |
| 3171 |
var |
| 3172 |
Multiplier: Single; |
| 3173 |
begin |
| 3174 |
Multiplier := MoveSpeed * MoveVerticalSpeed * SecondsPassed * Multiply; |
| 3175 |
if Input_Run.IsPressed(Container) then |
| 3176 |
Multiplier := Multiplier * 2; |
| 3177 |
Move(PreferredUpVector * Multiplier, false, false); |
| 3178 |
end; |
| 3179 |
|
| 3180 |
begin |
| 3181 |
if not Gravity then |
| 3182 |
begin |
| 3183 |
if PreferGravityUpForMoving then |
| 3184 |
MoveVerticalCore(GravityUpLocal) |
| 3185 |
else |
| 3186 |
MoveVerticalCore(Camera.Up); |
| 3187 |
end; |
| 3188 |
end; |
| 3189 |
|
| 3190 |
function TCastleWalkNavigation.ReallyEnableMouseDragging: boolean; |
| 3191 |
begin |
| 3192 |
Result := (inherited ReallyEnableMouseDragging) and not InternalUsingMouseLook; |
| 3193 |
end; |
| 3194 |
|
| 3195 |
procedure TCastleWalkNavigation.Update(const SecondsPassed: Single; |
| 3196 |
var HandleInput: boolean); |
| 3197 |
|
| 3198 |
{ Check are keys for left/right/down/up rotations are pressed, and handle them. |
| 3199 |
SpeedScale = 1 indicates a normal rotation speed, you can use it to scale |
| 3200 |
the rotation speed to specific purposes. } |
| 3201 |
procedure CheckRotates(SpeedScale: Single); |
| 3202 |
begin |
| 3203 |
if Input_RightRotate.IsPressed(Container) then |
| 3204 |
RotateHorizontal(-RotationHorizontalSpeed * SecondsPassed * SpeedScale); |
| 3205 |
if Input_LeftRotate.IsPressed(Container) then |
| 3206 |
RotateHorizontal(+RotationHorizontalSpeed * SecondsPassed * SpeedScale); |
| 3207 |
if Input_UpRotate.IsPressed(Container) then |
| 3208 |
RotateVertical(+RotationVerticalSpeed * SecondsPassed * SpeedScale); |
| 3209 |
if Input_DownRotate.IsPressed(Container) then |
| 3210 |
RotateVertical(-RotationVerticalSpeed * SecondsPassed * SpeedScale); |
| 3211 |
end; |
| 3212 |
|
| 3213 |
{ Things related to gravity --- jumping, taking into account |
| 3214 |
falling down and keeping RealPreferredHeight above the ground. } |
| 3215 |
procedure GravityUpdate; |
| 3216 |
|
| 3217 |
function TryJump: boolean; |
| 3218 |
var |
| 3219 |
ThisJumpHeight: Single; |
| 3220 |
begin |
| 3221 |
Result := IsJumping; |
| 3222 |
|
| 3223 |
if Result then |
| 3224 |
begin |
| 3225 |
{ jump. This means: |
| 3226 |
1. update FJumpHeight and move Position |
| 3227 |
2. or set FIsJumping to false when jump ends } |
| 3228 |
ThisJumpHeight := MaxJumpDistance * SecondsPassed / FJumpTime; |
| 3229 |
FJumpHeight := FJumpHeight + ThisJumpHeight; |
| 3230 |
|
| 3231 |
if FJumpHeight > MaxJumpDistance then |
| 3232 |
FIsJumping := false else |
| 3233 |
{ do jumping } |
| 3234 |
Move(GravityUpLocal * ThisJumpHeight, false, false); |
| 3235 |
end; |
| 3236 |
end; |
| 3237 |
|
| 3238 |
function TryFde_Stabilize: boolean; forward; |
| 3239 |
|
| 3240 |
{ If our height above the ground is < RealPreferredHeight |
| 3241 |
then we try to "grow". |
| 3242 |
|
| 3243 |
(this may happen because of many things --- e.g. user code |
| 3244 |
just changed PreferredHeight to something larger |
| 3245 |
(because e.g. "duck mode" ended), or we just ended falling dowm |
| 3246 |
from high). } |
| 3247 |
function TryGrow: boolean; |
| 3248 |
var |
| 3249 |
GrowingVectorLength: Single; |
| 3250 |
begin |
| 3251 |
Result := AboveHeight < RealPreferredHeight - RealPreferredHeightMargin; |
| 3252 |
|
| 3253 |
if Result then |
| 3254 |
begin |
| 3255 |
{ calculate GrowingVectorLength } |
| 3256 |
GrowingVectorLength := Min( |
| 3257 |
MoveSpeed * MoveVerticalSpeed * GrowSpeed * SecondsPassed, |
| 3258 |
RealPreferredHeight - AboveHeight); |
| 3259 |
|
| 3260 |
Move(GravityUpLocal * GrowingVectorLength, true, false); |
| 3261 |
|
| 3262 |
{ When growing, TryFde_Stabilize also must be done. |
| 3263 |
Otherwise when player walks horizontally on the flat surface |
| 3264 |
for some time then "Falling down effect" activates --- because |
| 3265 |
player is always in TryGrow or TryFalling. So one of them |
| 3266 |
(TryGrow or TryFalling) *must* allow "Falling down effect" |
| 3267 |
to stabilize itself. Obviously TryFalling can't (this would |
| 3268 |
be against the idea of this effect) so TryGrow does it... } |
| 3269 |
TryFde_Stabilize; |
| 3270 |
end; |
| 3271 |
end; |
| 3272 |
|
| 3273 |
function TryFalling: boolean; |
| 3274 |
const |
| 3275 |
Fde_VerticalRotateDeviation = 50.0; |
| 3276 |
Fde_HorizontalRotateDeviation = 15.0; |
| 3277 |
var |
| 3278 |
PositionBefore: TVector3; |
| 3279 |
FallingVectorLength: Single; |
| 3280 |
begin |
| 3281 |
Result := false; |
| 3282 |
|
| 3283 |
{ Note that if we got here, then TryGrow returned false, |
| 3284 |
which means that (assuming OnInternalHeight is correctly assigned) |
| 3285 |
we are not above the ground, or |
| 3286 |
AboveHeight >= |
| 3287 |
RealPreferredHeight - RealPreferredHeightMargin |
| 3288 |
However we require something stronger to continue: |
| 3289 |
AboveHeight > |
| 3290 |
RealPreferredHeight + RealPreferredHeightMargin |
| 3291 |
|
| 3292 |
This is important, because this way we avoid the unpleasant |
| 3293 |
"bouncing" effect when in one Update we decide that camera |
| 3294 |
is falling down, in next Update we decide that it's growing, |
| 3295 |
in next Update it falls down again etc. In TryGrow we try |
| 3296 |
to precisely set our Position, so that it hits exactly |
| 3297 |
at RealPreferredHeight -- which means that after TryGrow, |
| 3298 |
in next Update TryGrow should not cause growing and TryFalling |
| 3299 |
should not cause falling down. } |
| 3300 |
if AboveHeight <= |
| 3301 |
RealPreferredHeight + RealPreferredHeightMargin then |
| 3302 |
begin |
| 3303 |
FFalling := false; |
| 3304 |
Exit; |
| 3305 |
end; |
| 3306 |
|
| 3307 |
{ Make sure that FallSpeed is initialized. |
| 3308 |
When Falling, we know it's initialized (because setting |
| 3309 |
"FFalling := true;" is done only in the piece of code below...), |
| 3310 |
otherwise we make sure it's set to it's starting value. } |
| 3311 |
if not FFalling then |
| 3312 |
FFallSpeed := FallSpeedStart; |
| 3313 |
|
| 3314 |
{ try to fall down } |
| 3315 |
PositionBefore := Camera.Translation; |
| 3316 |
|
| 3317 |
{ calculate FallingVectorLength. |
| 3318 |
|
| 3319 |
Note that we make sure that FallingVectorLength is no longer |
| 3320 |
than AboveHeight --- this way we avoid the problem |
| 3321 |
that when FFallSpeed would get very big, |
| 3322 |
we couldn't fall down any more (while in fact we should then fall down |
| 3323 |
very quickly). |
| 3324 |
|
| 3325 |
Actually, we even do more. We make sure that |
| 3326 |
FallingVectorLength is no longer than |
| 3327 |
(AboveHeight - RealPreferredHeight). |
| 3328 |
Initially I wanted to do here |
| 3329 |
MinVar(FallingVectorLength, AboveHeight); |
| 3330 |
i.e. to allow camera to fall below RealPreferredHeight. |
| 3331 |
|
| 3332 |
But this didn't work like it should. Why ? |
| 3333 |
See above for the trick that I have to do with |
| 3334 |
RealPreferredHeightMargin above (to not cause |
| 3335 |
"unpleasant bouncing" when swapping Falling and TryGrow). |
| 3336 |
If I could fall down here below RealPreferredHeight then |
| 3337 |
|
| 3338 |
1. It *will not* cause the desired "nice" effect (of automatically |
| 3339 |
"ducking" when falling down from high), because of comparison |
| 3340 |
(the one with RealPreferredHeightMargin) above. |
| 3341 |
|
| 3342 |
2. It *will* cause the undesired unpleasant swapping between |
| 3343 |
Falling and TryGrow. |
| 3344 |
|
| 3345 |
So it's totally bad thing to do. |
| 3346 |
|
| 3347 |
This means that I should limit myself to not fall down |
| 3348 |
below RealPreferredHeight. And that's what I'm doing. } |
| 3349 |
FallingVectorLength := |
| 3350 |
MoveSpeed * MoveVerticalSpeed * FFallSpeed * SecondsPassed; |
| 3351 |
MinVar(FallingVectorLength, AboveHeight - RealPreferredHeight); |
| 3352 |
|
| 3353 |
if Move(GravityUpLocal * (- FallingVectorLength), true, false) and |
| 3354 |
(not TVector3.PerfectlyEquals(Camera.Translation, PositionBefore)) then |
| 3355 |
begin |
| 3356 |
if not Falling then |
| 3357 |
begin |
| 3358 |
FFallingStartPosition := PositionBefore; |
| 3359 |
|
| 3360 |
{ Why do I init here FFallSpeed ? A few lines above I did |
| 3361 |
if not FFalling then |
| 3362 |
FFallSpeed := FallSpeedStart; |
| 3363 |
to init FFallSpeed (I had to do it to calculate |
| 3364 |
FallingVectorLength). So why initing it again here ? |
| 3365 |
|
| 3366 |
Answer: Because Move above called MoveTo, that set Position |
| 3367 |
that actually called VisibleChange that possibly |
| 3368 |
called OnVisibleChange. |
| 3369 |
And OnVisibleChange is user callback and user could do there |
| 3370 |
things like |
| 3371 |
- Changing FallSpeedStart (but still it's unspecified |
| 3372 |
whether we have to apply this change, right ?) |
| 3373 |
- Calling CancelFalling and *then* changing FallSpeedStart. |
| 3374 |
And in this case, we *must* honour it, because here user |
| 3375 |
expects that we will use FallSpeedStart if we want |
| 3376 |
to fall down. (of course, one call to "Move" with old |
| 3377 |
"FallSpeedStart" was already done, that's unavoidable...). |
| 3378 |
|
| 3379 |
TODO: Is the above reasoning still valid? Now only TCastleCamera |
| 3380 |
calls VisibleChange. |
| 3381 |
} |
| 3382 |
FFallSpeed := FallSpeedStart; |
| 3383 |
|
| 3384 |
FFalling := true; |
| 3385 |
end; |
| 3386 |
|
| 3387 |
Result := true; |
| 3388 |
|
| 3389 |
if AboveHeight < RealPreferredHeight * 1.1 then |
| 3390 |
begin |
| 3391 |
{ This check is needed, otherwise when you're walking down even from |
| 3392 |
the most slight hill then you get |
| 3393 |
|
| 3394 |
1. FallingEffect |
| 3395 |
2. OnFall is called seldom and with large heights. |
| 3396 |
|
| 3397 |
Why ? Because MoveHorizontal calls are done between GravityUpdate |
| 3398 |
calls, and the move can be quite fast. So even though the player is |
| 3399 |
actually quite closely following the terrain, we would constantly |
| 3400 |
have Falling := true. Consider a large hill that is almost |
| 3401 |
flat --- when walking down the hill, we would get Falling |
| 3402 |
:= true, FallSpeed and FallingEffect would raise, |
| 3403 |
and at the end OnFall would be called with parameters |
| 3404 |
like player fell down from the top of the hill to the ground |
| 3405 |
(which can cause e.g. player losing life). |
| 3406 |
|
| 3407 |
The check for RealPreferredHeight * 1.1 above and |
| 3408 |
setting FFalling cure the situation. OnFall will |
| 3409 |
be called more often indicating very small fallen down heights, |
| 3410 |
and FallSpeed and FallingEffect will not be able |
| 3411 |
to raise high as long as player follows terrain closely. |
| 3412 |
|
| 3413 |
Of course we're setting here FFalling := false even though |
| 3414 |
the player is not exactly on the terrain --- but he's very close. |
| 3415 |
In the next GravityUpdate call we will again bring him a little |
| 3416 |
down, set FFalling to @true, and then set it back to @false |
| 3417 |
by line below. } |
| 3418 |
FFalling := false; |
| 3419 |
end else |
| 3420 |
begin |
| 3421 |
{ This is where we do FallingEffect. |
| 3422 |
|
| 3423 |
Note that I do FallingEffect *before* increasing |
| 3424 |
FFallSpeed below. |
| 3425 |
|
| 3426 |
1. reason (ideological, not really that important...) is that |
| 3427 |
FallingEffect is a penalty equivalent to FFallSpeed that |
| 3428 |
was already used --- not to the future FFallSpeed. |
| 3429 |
|
| 3430 |
2. reason (practical, and real :) is that when the program |
| 3431 |
was in some non-3d drawing state (e.g. displaying menu, or |
| 3432 |
displaying progress bar because the VRML model was just loaded) |
| 3433 |
then SecondsPassed indicates (truly) that a lot of time elapsed |
| 3434 |
since last Update. This means that it's common that at the same moment |
| 3435 |
when Falling changed suddenly to @true, SecondsPassed may be large |
| 3436 |
and we're better not using this too much... A practical bug demo: |
| 3437 |
open in castle-model-viewer (it does progress bar in OpenGL, so will cause |
| 3438 |
large SecondsPassed) any model with gravity on and camera slightly |
| 3439 |
higher then PreferredHeight (we want to trigger Falling |
| 3440 |
right when the model is loaded). E.g. run |
| 3441 |
"castle-model-viewer demo_models/navigation/speed_2.wrl". |
| 3442 |
If FallSpeedIncrease will be done before FallingEffect, |
| 3443 |
then you'll see that at the very first frame FFallSpeed |
| 3444 |
was increased so much (because SecondsPassed was large) that it triggered |
| 3445 |
FallingEffect. Even though the falling down distance was really small... |
| 3446 |
|
| 3447 |
Maybe in the future I'll workaround it differently. |
| 3448 |
One idea is that FFallSpeed should be made smaller if the |
| 3449 |
falled down distance is small. Or just don't call GravityUpdate after the first |
| 3450 |
model load, to avoid using large SecondsPassed ? |
| 3451 |
|
| 3452 |
LATER NOTE: note that the (2.) problem above may be non-existing |
| 3453 |
now, since we use SecondsPassed and we have ZeroNextSecondsPassed to |
| 3454 |
set SecondsPassed to zero in such cases. } |
| 3455 |
if FallingEffect and |
| 3456 |
(FFallSpeed > FallSpeedStart * 3) then |
| 3457 |
begin |
| 3458 |
if FFallSpeed > FallSpeedStart * 5 then |
| 3459 |
begin |
| 3460 |
if Fde_RotateHorizontal = 0 then |
| 3461 |
Fde_RotateHorizontal := RandomPlusMinus; |
| 3462 |
RotateAroundGravityUp(DegToRad(Fde_RotateHorizontal * |
| 3463 |
Fde_HorizontalRotateDeviation * SecondsPassed)); |
| 3464 |
end; |
| 3465 |
|
| 3466 |
if Fde_UpRotate < 0 then |
| 3467 |
Fde_UpRotate := Fde_UpRotate - (Fde_VerticalRotateDeviation * SecondsPassed) else |
| 3468 |
if Fde_UpRotate > 0 then |
| 3469 |
Fde_UpRotate := Fde_UpRotate + (Fde_VerticalRotateDeviation * SecondsPassed) else |
| 3470 |
Fde_UpRotate := RandomPlusMinus * |
| 3471 |
Fde_VerticalRotateDeviation * SecondsPassed; |
| 3472 |
end; |
| 3473 |
|
| 3474 |
{ Note that when changing FFallSpeed below I'm using SecondsPassed * 50. |
| 3475 |
And also above when using FFallSpeed, I multipled |
| 3476 |
FFallSpeed * SecondsPassed * 50. This is correct: |
| 3477 |
- changing position based on FallSpeed is a "velocity" |
| 3478 |
- changing FallSpeed below is "acceleration" |
| 3479 |
And both acceleration and velocity must be time-based. } |
| 3480 |
if FallSpeedIncrease <> 1.0 then |
| 3481 |
FFallSpeed := FFallSpeed * (Power(FallSpeedIncrease, SecondsPassed * 50)); |
| 3482 |
end; |
| 3483 |
end else |
| 3484 |
FFalling := false; |
| 3485 |
end; |
| 3486 |
|
| 3487 |
function TryFde_Stabilize: boolean; |
| 3488 |
const |
| 3489 |
Fde_VerticalRotateNormalization = 7 * 50; |
| 3490 |
var |
| 3491 |
Change: Single; |
| 3492 |
begin |
| 3493 |
Result := (Fde_RotateHorizontal <> 0) or (Fde_UpRotate <> 0); |
| 3494 |
|
| 3495 |
{ Bring Fde_Xxx vars back to normal (zero) values. } |
| 3496 |
|
| 3497 |
Fde_RotateHorizontal := 0; |
| 3498 |
|
| 3499 |
if Fde_UpRotate <> 0.0 then |
| 3500 |
begin |
| 3501 |
{ Note that we try to immediately bring UpRotate to |
| 3502 |
range (-360, 360) here. E.g. no need to gradually bring back |
| 3503 |
UpRotate from 360.0 to 0.0 --- this doesn't cause |
| 3504 |
any interesting visual effect (and the only reason for |
| 3505 |
UpRotate is a visual effect)... } |
| 3506 |
Change := Trunc(Abs(Fde_UpRotate) / 360.0) * 360.0 + |
| 3507 |
Fde_VerticalRotateNormalization * SecondsPassed; |
| 3508 |
|
| 3509 |
if Fde_UpRotate < 0 then |
| 3510 |
Fde_UpRotate := Min(Fde_UpRotate + Change, 0.0) |
| 3511 |
else |
| 3512 |
Fde_UpRotate := Max(Fde_UpRotate - Change, 0.0); |
| 3513 |
end; |
| 3514 |
end; |
| 3515 |
|
| 3516 |
function TryFallingOnTheGround: boolean; |
| 3517 |
var |
| 3518 |
Grav: TVector3; |
| 3519 |
|
| 3520 |
{ Return @link(TCastleTransform.Direction Camera.Direction) vector rotated such that it is |
| 3521 |
orthogonal to GravityUp. This way it returns |
| 3522 |
@link(TCastleTransform.Direction Camera.Direction) projected |
| 3523 |
on the gravity horizontal plane, which neutralizes such things |
| 3524 |
like raising / bowing your head. |
| 3525 |
|
| 3526 |
Result is always normalized (length 1). |
| 3527 |
|
| 3528 |
Note that when @link(TCastleTransform.Direction Camera.Direction) and GravityUp are parallel, |
| 3529 |
this just returns current @link(TCastleTransform.Direction Camera.Direction) --- because in such case |
| 3530 |
we can't project @link(TCastleTransform.Direction Camera.Direction) on the horizontal plane. |
| 3531 |
|
| 3532 |
Note that the result is in TCastleCamera parent coordinate space, just like Direction. |
| 3533 |
We automatically account for the fact that GravityUp is specified in world coordinate space. } |
| 3534 |
function DirInGravityPlane: TVector3; |
| 3535 |
begin |
| 3536 |
Result := Camera.Direction; |
| 3537 |
MakeVectorsOrthoOnTheirPlane(Result, Grav); |
| 3538 |
end; |
| 3539 |
|
| 3540 |
var |
| 3541 |
Angle, AngleRotate: Single; |
| 3542 |
begin |
| 3543 |
Grav := GravityUpLocal; |
| 3544 |
|
| 3545 |
Result := FFallingOnTheGround; |
| 3546 |
if not Result then |
| 3547 |
Exit; |
| 3548 |
|
| 3549 |
Angle := AngleRadBetweenVectors(Camera.Up, Grav); |
| 3550 |
if SameValue(Angle, HalfPi, 0.01) then |
| 3551 |
begin |
| 3552 |
{ FallingOnTheGround effect stops here. } |
| 3553 |
FFallingOnTheGround := false; |
| 3554 |
Exit; |
| 3555 |
end; |
| 3556 |
|
| 3557 |
{ Our DirInGravityPlane doesn't work when Camera.Direction and Grav are parallel } |
| 3558 |
if VectorsParallel(Camera.Direction, Grav) then |
| 3559 |
Exit; |
| 3560 |
|
| 3561 |
AngleRotate := SecondsPassed * 5; |
| 3562 |
MinVar(AngleRotate, Abs(Angle - HalfPi)); |
| 3563 |
if not FFallingOnTheGroundAngleIncrease then |
| 3564 |
AngleRotate := -AngleRotate; |
| 3565 |
|
| 3566 |
Camera.Up := RotatePointAroundAxisRad(AngleRotate, Camera.Up, DirInGravityPlane); |
| 3567 |
end; |
| 3568 |
|
| 3569 |
procedure DoFall; |
| 3570 |
var |
| 3571 |
BeginPos, EndPos, FallVector: TVector3; |
| 3572 |
begin |
| 3573 |
if Assigned(OnFall) then |
| 3574 |
begin |
| 3575 |
{ Project Position and FFallingStartPosition |
| 3576 |
onto GravityUp vector to calculate fall height. } |
| 3577 |
BeginPos := PointOnLineClosestToPoint(TVector3.Zero, GravityUpLocal, FFallingStartPosition); |
| 3578 |
EndPos := PointOnLineClosestToPoint(TVector3.Zero, GravityUpLocal, Camera.Translation); |
| 3579 |
FallVector := BeginPos - EndPos; |
| 3580 |
|
| 3581 |
{ Because of various growing and jumping effects (imagine you jump up |
| 3582 |
onto a taller pillar) it may turn out that we're higher at the end |
| 3583 |
at the end of fall. Do not report it to OnFall event in this case. } |
| 3584 |
if TVector3.DotProduct(GravityUpLocal, FallVector.Normalize) <= 0 then |
| 3585 |
Exit; |
| 3586 |
|
| 3587 |
OnFall(Self, FallVector.Length); |
| 3588 |
end; |
| 3589 |
end; |
| 3590 |
|
| 3591 |
procedure HeadBobbingGoesDown; |
| 3592 |
const |
| 3593 |
HeadBobbingGoingDownSpeed = 5; |
| 3594 |
var |
| 3595 |
FracHeadBobbingPosition: Single; |
| 3596 |
begin |
| 3597 |
if UseHeadBobbing and (not HeadBobbingAlreadyDone) then |
| 3598 |
begin |
| 3599 |
{ If head bobbing is active, but player did not move during |
| 3600 |
this Update call, and no gravity effect is in work |
| 3601 |
then player is standing still on the ground. |
| 3602 |
|
| 3603 |
This means that his head bobbing should go down as far as |
| 3604 |
possible. This means that HeadBobbingPosition should |
| 3605 |
go to nearest integer value. |
| 3606 |
|
| 3607 |
Note that we avoid changing HeadBobbingPosition by less |
| 3608 |
than SingleEpsilon, just to be on the safe side |
| 3609 |
and avoid any "corner cases", when HeadBobbingPosition |
| 3610 |
would switch between going up and down repeatedly. } |
| 3611 |
FracHeadBobbingPosition := Frac(HeadBobbingPosition); |
| 3612 |
if FracHeadBobbingPosition > 0.5 then |
| 3613 |
begin |
| 3614 |
if 1 - FracHeadBobbingPosition > SingleEpsilon then |
| 3615 |
HeadBobbingPosition := HeadBobbingPosition + |
| 3616 |
Min(HeadBobbingGoingDownSpeed * SecondsPassed, |
| 3617 |
1 - FracHeadBobbingPosition); |
| 3618 |
end else |
| 3619 |
begin |
| 3620 |
if FracHeadBobbingPosition > SingleEpsilon then |
| 3621 |
HeadBobbingPosition := HeadBobbingPosition - |
| 3622 |
Min(HeadBobbingGoingDownSpeed * SecondsPassed, |
| 3623 |
FracHeadBobbingPosition); |
| 3624 |
end; |
| 3625 |
end; |
| 3626 |
end; |
| 3627 |
|
| 3628 |
function GetIsOnTheGround: boolean; |
| 3629 |
var |
| 3630 |
MinAboveHeight, MaxAboveHeight, H: Single; |
| 3631 |
begin |
| 3632 |
H := RealPreferredHeightNoHeadBobbing; |
| 3633 |
MinAboveHeight := (H - H * HeadBobbing) * 0.99; |
| 3634 |
MaxAboveHeight := (H + H * HeadBobbing) * 1.01; |
| 3635 |
Result := IsAbove and |
| 3636 |
(MinAboveHeight <= AboveHeight) and |
| 3637 |
(AboveHeight <= MaxAboveHeight); |
| 3638 |
end; |
| 3639 |
|
| 3640 |
var |
| 3641 |
OldFalling: boolean; |
| 3642 |
begin |
| 3643 |
OldFalling := Falling; |
| 3644 |
|
| 3645 |
if Gravity then |
| 3646 |
begin |
| 3647 |
{ update IsAbove, AboveHeight } |
| 3648 |
Height(Camera.WorldTranslation, FIsAbove, FAboveHeight, FAboveGround); |
| 3649 |
|
| 3650 |
FIsOnTheGround := GetIsOnTheGround; |
| 3651 |
FIsWalkingOnTheGround := MoveHorizontalDone and FIsOnTheGround; |
| 3652 |
|
| 3653 |
if not TryJump then |
| 3654 |
if not TryGrow then |
| 3655 |
if not TryFalling then |
| 3656 |
if not TryFde_Stabilize then |
| 3657 |
{ Note that we don't do FallingOnTheGround effect until all |
| 3658 |
other effects (jumping, growing, falling on the ground |
| 3659 |
and stabilizing after falling on the ground) will finish |
| 3660 |
their work. } |
| 3661 |
if not TryFallingOnTheGround then |
| 3662 |
HeadBobbingGoesDown; |
| 3663 |
end else |
| 3664 |
begin |
| 3665 |
FFalling := false; |
| 3666 |
TryFde_Stabilize; |
| 3667 |
end; |
| 3668 |
|
| 3669 |
if OldFalling and (not Falling) then |
| 3670 |
DoFall; |
| 3671 |
end; |
| 3672 |
|
| 3673 |
procedure PreferGravityUpForRotationsUpdate; |
| 3674 |
(* This is a good piece of work and seemed to work OK, |
| 3675 |
but it's too much untested right now to let it work. |
| 3676 |
|
| 3677 |
It's needed only when you'll start to change |
| 3678 |
PreferGravityUpForRotations from false to true in runtime, |
| 3679 |
to avoid making player feel "awkward" rotations. |
| 3680 |
|
| 3681 |
Temporary I don't need it. |
| 3682 |
|
| 3683 |
var |
| 3684 |
TargetPlane: TVector4; |
| 3685 |
TargetPlaneDir: TVector3 absolute TargetPlane; |
| 3686 |
TargetUp: TVector3; |
| 3687 |
AngleRadBetweenTargetAndGravity: Single; |
| 3688 |
AngleRadBetweenTarget, AngleRadBetweenTargetChange: Single; |
| 3689 |
NewUp: TVector3; |
| 3690 |
begin |
| 3691 |
if PreferGravityUp then |
| 3692 |
begin |
| 3693 |
{ TODO: Correcting MinAngleFromGravityUp } |
| 3694 |
|
| 3695 |
{ Correct Up such that GravityUp, Direction and Up |
| 3696 |
are on the same plane. |
| 3697 |
|
| 3698 |
Math: |
| 3699 |
TargetPlane := common plane of GravityUp and Direction, |
| 3700 |
given by (A, B, C) = TVector3.CrossProduct(GravityUp, Direction) |
| 3701 |
and D = 0 (because point (0, 0, 0) is part of this plane). |
| 3702 |
|
| 3703 |
We check whether Up is on this TargetPlane too. |
| 3704 |
|
| 3705 |
If not, we find TargetUp = nearest point to Up |
| 3706 |
lying on this TargetPlane. We want our Up be pointing |
| 3707 |
like GravityUp, not in the other way, so if the angle between |
| 3708 |
GravityUp and TargetUp is > 90 degress we negate |
| 3709 |
TargetUp. If the angle is exactly 90 degress then |
| 3710 |
TargetUp is simply equal to GravityUp. |
| 3711 |
|
| 3712 |
And then we make the angle between TargetUp and Up |
| 3713 |
smaller. } |
| 3714 |
|
| 3715 |
TargetPlaneDir := TVector3.CrossProduct(GravityUp, Direction); |
| 3716 |
if not Zero( |
| 3717 |
(TargetPlaneDir[0] * FUp[0]) + |
| 3718 |
(TargetPlaneDir[1] * FUp[1]) + |
| 3719 |
(TargetPlaneDir[2] * FUp[2])) then |
| 3720 |
begin |
| 3721 |
TargetPlane[3] := 0; |
| 3722 |
|
| 3723 |
Writeln('corrrecting'); |
| 3724 |
|
| 3725 |
{ calculate TargetUp } |
| 3726 |
TargetUp := PointOnPlaneClosestToPoint(TargetPlane, FUp); |
| 3727 |
AngleRadBetweenTargetAndGravity := |
| 3728 |
AngleRadBetweenVectors(TargetUp, GravityUp); |
| 3729 |
if SameValue(AngleRadBetweenTargetAndGravity, HalfPi) then |
| 3730 |
TargetUp := GravityUp else |
| 3731 |
if AngleRadBetweenTargetAndGravity > HalfPi then |
| 3732 |
TargetUp := -TargetUp; |
| 3733 |
|
| 3734 |
AngleRadBetweenTarget := AngleRadBetweenVectors(TargetUp, FUp); |
| 3735 |
AngleRadBetweenTargetChange := 0.5 * SecondsPassed; |
| 3736 |
if AngleRadBetweenTarget > AngleRadBetweenTargetChange then |
| 3737 |
begin |
| 3738 |
NewUp := FUp; |
| 3739 |
MakeVectorsAngleRadOnTheirPlane(NewUp, TargetUp, |
| 3740 |
AngleRadBetweenTarget - AngleRadBetweenTargetChange, NewUp); |
| 3741 |
Up := NewUp; |
| 3742 |
end else |
| 3743 |
Up := TargetUp; |
| 3744 |
end; |
| 3745 |
end; |
| 3746 |
*) |
| 3747 |
begin |
| 3748 |
end; |
| 3749 |
|
| 3750 |
procedure ChangePreferredHeight(const Increase: Integer); |
| 3751 |
begin |
| 3752 |
PreferredHeight := PreferredHeight + |
| 3753 |
{ It's best to scale PreferredHeight changes by MoveSpeed, |
| 3754 |
to make it faster/slower depending on scene size |
| 3755 |
(which usually corresponds to move speed). } |
| 3756 |
Increase * MoveSpeed * SecondsPassed * 0.2; |
| 3757 |
|
| 3758 |
CorrectPreferredHeight; |
| 3759 |
end; |
| 3760 |
|
| 3761 |
{ Implement MouseDragMode = mdWalkRotate case, |
| 3762 |
moving and rotating in response to mouse dragging by Delta. } |
| 3763 |
procedure MoveRotateViaMouseDragging(Delta: TVector2); |
| 3764 |
var |
| 3765 |
MoveSizeX, MoveSizeY: Single; |
| 3766 |
const |
| 3767 |
Tolerance = 5; { 5px tolerance for not-moving } |
| 3768 |
begin |
| 3769 |
MoveSizeX := 0; |
| 3770 |
MoveSizeY := 0; |
| 3771 |
|
| 3772 |
if Abs(Delta.X) < Tolerance then |
| 3773 |
Delta.X := 0 |
| 3774 |
else |
| 3775 |
MoveSizeX := (Abs(Delta.X) - Tolerance) * MouseDraggingMoveSpeed; |
| 3776 |
|
| 3777 |
if Abs(Delta.Y) < Tolerance then |
| 3778 |
Delta.Y := 0 |
| 3779 |
else |
| 3780 |
MoveSizeY := (Abs(Delta.Y) - Tolerance) * MouseDraggingMoveSpeed; |
| 3781 |
|
| 3782 |
if buttonLeft in Container.MousePressed then |
| 3783 |
begin |
| 3784 |
if Delta.Y < -Tolerance then |
| 3785 |
MoveHorizontal( Camera.Direction, -MoveSizeY * SecondsPassed); // forward |
| 3786 |
if Delta.Y > Tolerance then |
| 3787 |
MoveHorizontal(-Camera.Direction, -MoveSizeY * SecondsPassed); // backward |
| 3788 |
|
| 3789 |
if Abs(Delta.X) > Tolerance then |
| 3790 |
RotateHorizontal(-Delta.X * SecondsPassed * MouseDraggingHorizontalRotationSpeed); { rotate } |
| 3791 |
end else |
| 3792 |
if buttonRight in Container.MousePressed then |
| 3793 |
begin |
| 3794 |
if Delta.X < -Tolerance then |
| 3795 |
MoveHorizontal(DirectionLeft, MoveSizeX * SecondsPassed); |
| 3796 |
if Delta.X > Tolerance then |
| 3797 |
MoveHorizontal(DirectionRight, MoveSizeX * SecondsPassed); |
| 3798 |
|
| 3799 |
if Delta.Y < -5 then |
| 3800 |
MoveVertical(-MoveSizeY * SecondsPassed, 1); { fly up } |
| 3801 |
if Delta.Y > 5 then |
| 3802 |
MoveVertical(-MoveSizeY * SecondsPassed, -1); { fly down } |
| 3803 |
end; |
| 3804 |
end; |
| 3805 |
|
| 3806 |
var |
| 3807 |
ModsDown: TModifierKeys; |
| 3808 |
begin |
| 3809 |
inherited; |
| 3810 |
|
| 3811 |
{ update Cursor every frame, in case InternalViewport.Paused changed |
| 3812 |
(which changes UsingInput and InternalUsingMouseLook) } |
| 3813 |
if InternalUsingMouseLook then |
| 3814 |
Cursor := mcForceNone |
| 3815 |
else |
| 3816 |
Cursor := mcDefault; |
| 3817 |
|
| 3818 |
if (not Valid) then Exit; |
| 3819 |
|
| 3820 |
ModsDown := ModifiersDown(Container.Pressed); |
| 3821 |
|
| 3822 |
HeadBobbingAlreadyDone := false; |
| 3823 |
MoveHorizontalDone := false; |
| 3824 |
|
| 3825 |
if HandleInput then |
| 3826 |
begin |
| 3827 |
if niNormal in UsingInput then |
| 3828 |
begin |
| 3829 |
HandleInput := false; |
| 3830 |
FIsCrouching := Gravity and Input_Crouch.IsPressed(Container); |
| 3831 |
|
| 3832 |
if (not CheckModsDown) or |
| 3833 |
(ModsDown - Input_Run.Modifiers = []) then |
| 3834 |
begin |
| 3835 |
CheckRotates(1.0); |
| 3836 |
|
| 3837 |
if Input_Forward.IsPressed(Container) or MoveForward then |
| 3838 |
MoveHorizontal( Camera.Direction, SecondsPassed); |
| 3839 |
if Input_Backward.IsPressed(Container) or MoveBackward then |
| 3840 |
MoveHorizontal(-Camera.Direction, SecondsPassed); |
| 3841 |
if Input_RightStrafe.IsPressed(Container) then |
| 3842 |
MoveHorizontal(DirectionRight, SecondsPassed); |
| 3843 |
if Input_LeftStrafe.IsPressed(Container) then |
| 3844 |
MoveHorizontal(DirectionLeft , SecondsPassed); |
| 3845 |
|
| 3846 |
{ A simple implementation of Input_Jump was |
| 3847 |
RotateVertical(HalfPi); Move(MoveVerticalSpeed * MoveSpeed * SecondsPassed); RotateVertical(-HalfPi) |
| 3848 |
Similarly, simple implementation of Input_Crouch was |
| 3849 |
RotateVertical(-HalfPi); Move(MoveVerticalSpeed * MoveSpeed * SecondsPassed); RotateVertical(HalfPi) |
| 3850 |
But this is not good, because when PreferGravityUp, we want to move |
| 3851 |
along the GravityUp. (Also later note: RotateVertical is now bounded by |
| 3852 |
MinAngleFromGravityUp). } |
| 3853 |
|
| 3854 |
if Input_Jump.IsPressed(Container) then |
| 3855 |
MoveVertical(SecondsPassed, 1); |
| 3856 |
if Input_Crouch.IsPressed(Container) then |
| 3857 |
MoveVertical(SecondsPassed, -1); |
| 3858 |
if Input_MoveSpeedInc.IsPressed(Container) then |
| 3859 |
MoveSpeedInc(SecondsPassed); |
| 3860 |
if Input_MoveSpeedDec.IsPressed(Container) then |
| 3861 |
MoveSpeedDec(SecondsPassed); |
| 3862 |
if Input_IncreasePreferredHeight.IsPressed(Container) then |
| 3863 |
ChangePreferredHeight(+1); |
| 3864 |
if Input_DecreasePreferredHeight.IsPressed(Container) then |
| 3865 |
ChangePreferredHeight(-1); |
| 3866 |
end else |
| 3867 |
if ModsDown = [mkCtrl] then |
| 3868 |
begin |
| 3869 |
if AllowSlowerRotations then |
| 3870 |
CheckRotates(0.1); |
| 3871 |
end; |
| 3872 |
end; |
| 3873 |
|
| 3874 |
{ mouse dragging navigation } |
| 3875 |
if (MouseDraggingStarted <> -1) and |
| 3876 |
ReallyEnableMouseDragging and |
| 3877 |
(Container.MousePressed <> []) and |
| 3878 |
{ Enable dragging only when no modifiers (except Input_Run, |
| 3879 |
which must be allowed to enable running) are pressed. |
| 3880 |
This allows application to handle e.g. ctrl + dragging |
| 3881 |
in some custom ways (like castle-model-viewer selecting a triangle). } |
| 3882 |
(Container.Pressed.Modifiers - Input_Run.Modifiers = []) and |
| 3883 |
(MouseDragMode = mdWalkRotate) and |
| 3884 |
{ Hack to avoid processing when mouse is over TCastleTouchNavigation |
| 3885 |
gizmo, since it does it's own rotating/movement of camera. |
| 3886 |
This is a hack, ideally TCastleWalkNavigation should not have a specific |
| 3887 |
rule to interact with TCastleTouchNavigation... |
| 3888 |
|
| 3889 |
Note: We cannot move MoveRotateViaMouseDragging logic to Motion |
| 3890 |
(thus using "motion is handled" boolean) because |
| 3891 |
MoveRotateViaMouseDragging wants to move even if no mouse movement |
| 3892 |
happened in last frame. } |
| 3893 |
(not TCastleTouchNavigation.MouseOverTouchGizmo(Container)) then |
| 3894 |
begin |
| 3895 |
HandleInput := false; |
| 3896 |
MoveRotateViaMouseDragging(Container.MousePosition - MouseDraggingStart); |
| 3897 |
end; |
| 3898 |
end; |
| 3899 |
|
| 3900 |
PreferGravityUpForRotationsUpdate; |
| 3901 |
|
| 3902 |
{ These may be set to @true only inside GravityUpdate } |
| 3903 |
FIsWalkingOnTheGround := false; |
| 3904 |
FIsOnTheGround := false; |
| 3905 |
|
| 3906 |
{ Disable gravity in design mode (in the future we may add optional way to enable them) } |
| 3907 |
if not CastleDesignMode then |
| 3908 |
GravityUpdate; |
| 3909 |
end; |
| 3910 |
|
| 3911 |
function TCastleWalkNavigation.Jump: boolean; |
| 3912 |
begin |
| 3913 |
Result := false; |
| 3914 |
|
| 3915 |
if IsJumping or Falling or (not Gravity) then Exit; |
| 3916 |
|
| 3917 |
{ Merely checking for Falling is not enough, because Falling |
| 3918 |
may be triggered with some latency. E.g. consider user that holds |
| 3919 |
Input_Jump key down: whenever jump will end (in GravityUpdate), |
| 3920 |
Input_Jump.IsKey = true will cause another jump to be immediately |
| 3921 |
(before Falling will be set to true) initiated. |
| 3922 |
This is of course bad, because user holding Input_Jump key down |
| 3923 |
would be able to jump to any height. The only good thing to do |
| 3924 |
is to check whether player really has some ground beneath his feet |
| 3925 |
to be able to jump. } |
| 3926 |
|
| 3927 |
{ update IsAbove, AboveHeight } |
| 3928 |
Height(Camera.WorldTranslation, FIsAbove, FAboveHeight, FAboveGround); |
| 3929 |
|
| 3930 |
if AboveHeight > RealPreferredHeight + RealPreferredHeightMargin then |
| 3931 |
Exit; |
| 3932 |
|
| 3933 |
FIsJumping := true; |
| 3934 |
FJumpHeight := 0.0; |
| 3935 |
Result := true; |
| 3936 |
end; |
| 3937 |
|
| 3938 |
function TCastleWalkNavigation.AllowSuspendForInput: boolean; |
| 3939 |
begin |
| 3940 |
Result := false; |
| 3941 |
end; |
| 3942 |
|
| 3943 |
function TCastleWalkNavigation.Press(const Event: TInputPressRelease): boolean; |
| 3944 |
|
| 3945 |
procedure SetUpToGravityUp; |
| 3946 |
var |
| 3947 |
OldPosition, OldDirection, OldUp, NewDirection, NewUp: TVector3; |
| 3948 |
begin |
| 3949 |
Camera.GetWorldView(OldPosition, OldDirection, OldUp); |
| 3950 |
|
| 3951 |
if VectorsParallel(OldDirection, Camera.GravityUp) then |
| 3952 |
begin |
| 3953 |
{ We can't carelessly set NewUp to something parallel to GravityUp |
| 3954 |
in this case. |
| 3955 |
|
| 3956 |
Yes, this situation can happen: for example open a model with |
| 3957 |
no viewpoint in VRML in castle-model-viewer (so default viewpoint, |
| 3958 |
both gravity and Up = +Y is used). Then change GravityUp |
| 3959 |
by menu and press Home (Input_GravityUp). } |
| 3960 |
|
| 3961 |
NewUp := Camera.GravityUp; |
| 3962 |
NewDirection := AnyOrthogonalVector(NewUp); |
| 3963 |
end else |
| 3964 |
begin |
| 3965 |
NewUp := Camera.GravityUp; |
| 3966 |
NewDirection := OldDirection; // we use AdjustUp = false with SetWorldView, it will already adjust direction |
| 3967 |
end; |
| 3968 |
|
| 3969 |
Camera.SetWorldView(OldPosition, NewDirection, NewUp, false); |
| 3970 |
end; |
| 3971 |
|
| 3972 |
procedure HandleMouseWheelPress; |
| 3973 |
const |
| 3974 |
PretendSecondsPassed = 1 / 30; |
| 3975 |
var |
| 3976 |
RotationsSpeedScale: Single; |
| 3977 |
begin |
| 3978 |
{ Inputs below are handled also in TCastleWalkNavigation.Update. |
| 3979 |
But the mouse wheel is never in a pressed state, so it cannot be handled in Update. |
| 3980 |
So we handle pressing on mouse wheel in a special way here. |
| 3981 |
|
| 3982 |
TODO: A more generic mechanism for handling mouse wheel would be nice, so we don't |
| 3983 |
need to double handling of everything for mouse wheel? } |
| 3984 |
|
| 3985 |
if Input_MoveSpeedInc.IsEvent(Event) then |
| 3986 |
begin |
| 3987 |
MoveSpeedInc(PretendSecondsPassed); |
| 3988 |
Result := true; |
| 3989 |
end; |
| 3990 |
|
| 3991 |
if Input_MoveSpeedDec.IsEvent(Event) then |
| 3992 |
begin |
| 3993 |
MoveSpeedDec(PretendSecondsPassed); |
| 3994 |
Result := true; |
| 3995 |
end; |
| 3996 |
|
| 3997 |
if Container.Pressed.Modifiers = [mkCtrl] then |
| 3998 |
RotationsSpeedScale := 0.1 |
| 3999 |
else |
| 4000 |
RotationsSpeedScale := 1.0; |
| 4001 |
|
| 4002 |
if Input_RightRotate.IsEvent(Event) then |
| 4003 |
begin |
| 4004 |
RotateHorizontal(-RotationHorizontalSpeed * PretendSecondsPassed * RotationsSpeedScale); |
| 4005 |
Result := true; |
| 4006 |
end; |
| 4007 |
|
| 4008 |
if Input_LeftRotate.IsEvent(Event) then |
| 4009 |
begin |
| 4010 |
RotateHorizontal(+RotationHorizontalSpeed * PretendSecondsPassed * RotationsSpeedScale); |
| 4011 |
Result := true; |
| 4012 |
end; |
| 4013 |
|
| 4014 |
if Input_UpRotate.IsEvent(Event) then |
| 4015 |
begin |
| 4016 |
RotateVertical(+RotationVerticalSpeed * PretendSecondsPassed * RotationsSpeedScale); |
| 4017 |
Result := true; |
| 4018 |
end; |
| 4019 |
|
| 4020 |
if Input_DownRotate.IsEvent(Event) then |
| 4021 |
begin |
| 4022 |
RotateVertical(-RotationVerticalSpeed * PretendSecondsPassed * RotationsSpeedScale); |
| 4023 |
Result := true; |
| 4024 |
end; |
| 4025 |
end; |
| 4026 |
|
| 4027 |
begin |
| 4028 |
Result := inherited; |
| 4029 |
if Result then Exit; |
| 4030 |
|
| 4031 |
if (Event.EventType = itKey) and |
| 4032 |
CheckModsDown and |
| 4033 |
(ModifiersDown(Container.Pressed) - Input_Run.Modifiers <> []) then |
| 4034 |
Exit; |
| 4035 |
|
| 4036 |
if (Event.EventType = itMouseButton) and |
| 4037 |
ReallyEnableMouseDragging and |
| 4038 |
(MouseDragMode = mdNone) then |
| 4039 |
begin |
| 4040 |
MouseDraggingStarted := -1; |
| 4041 |
Result := false; |
| 4042 |
Exit; |
| 4043 |
end; |
| 4044 |
|
| 4045 |
if (not (niNormal in UsingInput)) or |
| 4046 |
(not Valid) then |
| 4047 |
Exit(false); |
| 4048 |
|
| 4049 |
if Input_GravityUp.IsEvent(Event) then |
| 4050 |
begin |
| 4051 |
SetUpToGravityUp; |
| 4052 |
Result := true; |
| 4053 |
end; |
| 4054 |
|
| 4055 |
if Input_Jump.IsEvent(Event) then |
| 4056 |
begin |
| 4057 |
if Jump then |
| 4058 |
Result := true; |
| 4059 |
end; |
| 4060 |
|
| 4061 |
if Event.EventType = itMouseWheel then |
| 4062 |
HandleMouseWheelPress; |
| 4063 |
end; |
| 4064 |
|
| 4065 |
procedure TCastleWalkNavigation.MoveSpeedInc(const SecondsPassed: Single); |
| 4066 |
begin |
| 4067 |
{ Time-based animation for multiplication: use SecondsPassed as an exponent. |
| 4068 |
|
| 4069 |
We want to do "MoveSpeed *= 10", intuitively. |
| 4070 |
|
| 4071 |
To do it correctly (accounting that each system has different FPS) |
| 4072 |
we want to apply SecondsPassed such that the effect is the same, regardless |
| 4073 |
if we apply it in 10 steps over 1 second, or 1 step over 1 second. |
| 4074 |
Using SecondsPassed as the exponent is the solution. |
| 4075 |
} |
| 4076 |
if MoveSpeed < MoveSpeedMax then |
| 4077 |
MoveSpeed := Min(MoveSpeedMax, MoveSpeed * Power(10, SecondsPassed)); |
| 4078 |
end; |
| 4079 |
|
| 4080 |
procedure TCastleWalkNavigation.MoveSpeedDec(const SecondsPassed: Single); |
| 4081 |
begin |
| 4082 |
if MoveSpeed > MoveSpeedMin then |
| 4083 |
MoveSpeed := Max(MoveSpeedMin, MoveSpeed / Power(10, SecondsPassed)); |
| 4084 |
end; |
| 4085 |
|
| 4086 |
function TCastleWalkNavigation.SensorTranslation(const X, Y, Z, Length: Double; |
| 4087 |
const SecondsPassed: Single): boolean; |
| 4088 |
var |
| 4089 |
MoveSize: Double; |
| 4090 |
begin |
| 4091 |
if not (ni3dMouse in UsingInput) then Exit(false); |
| 4092 |
Result := true; |
| 4093 |
|
| 4094 |
MoveSize := Length * SecondsPassed / 5000; |
| 4095 |
|
| 4096 |
if Z > 5 then |
| 4097 |
MoveHorizontal(-Camera.Direction, Z * MoveSize); { backward } |
| 4098 |
if Z < -5 then |
| 4099 |
MoveHorizontal( Camera.Direction, -Z * MoveSize); { forward } |
| 4100 |
|
| 4101 |
if X > 5 then |
| 4102 |
MoveHorizontal(DirectionRight, X * MoveSize); { right } |
| 4103 |
if X < -5 then |
| 4104 |
MoveHorizontal(DirectionLeft, -X * MoveSize); { left } |
| 4105 |
|
| 4106 |
if Y > 5 then |
| 4107 |
MoveVertical(Y * MoveSize, 1); { up } |
| 4108 |
if Y < -5 then |
| 4109 |
MoveVertical(-Y * MoveSize, -1); { down } |
| 4110 |
end; |
| 4111 |
|
| 4112 |
function TCastleWalkNavigation.SensorRotation(const X, Y, Z, Angle: Double; |
| 4113 |
const SecondsPassed: Single): boolean; |
| 4114 |
const |
| 4115 |
SpeedSensor = 2; |
| 4116 |
begin |
| 4117 |
if not (ni3dMouse in UsingInput) then Exit(false); |
| 4118 |
Result := true; |
| 4119 |
|
| 4120 |
if Abs(X) > 0.4 then { tilt forward / backward } |
| 4121 |
RotateVertical(X * Angle * SpeedSensor * SecondsPassed); |
| 4122 |
if Abs(Y) > 0.4 then { rotate } |
| 4123 |
RotateHorizontal(Y * Angle * SpeedSensor * SecondsPassed); |
| 4124 |
{if Abs(Z) > 0.4 then ?} { tilt sidewards } |
| 4125 |
end; |
| 4126 |
|
| 4127 |
procedure TCastleWalkNavigation.Init( |
| 4128 |
const AInitialPosition, AInitialDirection, AInitialUp: TVector3; |
| 4129 |
const AGravityUp: TVector3; |
| 4130 |
const APreferredHeight: Single; |
| 4131 |
const ARadius: Single); |
| 4132 |
begin |
| 4133 |
PreferredHeight := APreferredHeight; |
| 4134 |
Radius := ARadius; |
| 4135 |
CorrectPreferredHeight; |
| 4136 |
|
| 4137 |
Camera.SetWorldView(AInitialPosition, AInitialDirection, AInitialUp); |
| 4138 |
Camera.GravityUp := AGravityUp; |
| 4139 |
Camera.ProjectionNear := Radius * RadiusToProjectionNear; |
| 4140 |
end; |
| 4141 |
|
| 4142 |
procedure TCastleWalkNavigation.Init(const Box: TBox3D; const ARadius: Single); |
| 4143 |
var |
| 4144 |
Pos: TVector3; |
| 4145 |
AvgSize: Single; |
| 4146 |
begin |
| 4147 |
if Box.IsEmptyOrZero then |
| 4148 |
begin |
| 4149 |
Radius := ARadius; |
| 4150 |
PreferredHeight := Max(DefaultPreferredHeight, RadiusToPreferredHeightMin * ARadius); |
| 4151 |
CorrectPreferredHeight; |
| 4152 |
|
| 4153 |
Camera.ProjectionNear := Radius * RadiusToProjectionNear; |
| 4154 |
|
| 4155 |
Camera.SetWorldView(TVector3.Zero, |
| 4156 |
DefaultCameraDirection, |
| 4157 |
DefaultCameraUp); |
| 4158 |
Camera.GravityUp := DefaultCameraUp; |
| 4159 |
end else |
| 4160 |
begin |
| 4161 |
Radius := ARadius; |
| 4162 |
AvgSize := Box.AverageSize; |
| 4163 |
PreferredHeight := AvgSize * 5; |
| 4164 |
CorrectPreferredHeight; |
| 4165 |
|
| 4166 |
Camera.ProjectionNear := Radius * RadiusToProjectionNear; |
| 4167 |
|
| 4168 |
Pos := Vector3( |
| 4169 |
Box.Data[0].X - AvgSize, |
| 4170 |
(Box.Data[0].Y + Box.Data[1].Y) / 2, |
| 4171 |
(Box.Data[0].Z + Box.Data[1].Z) / 2 |
| 4172 |
); |
| 4173 |
Camera.SetWorldView(Pos, |
| 4174 |
DefaultCameraDirection, |
| 4175 |
DefaultCameraUp); |
| 4176 |
Camera.GravityUp := DefaultCameraUp; |
| 4177 |
end; |
| 4178 |
end; |
| 4179 |
|
| 4180 |
procedure TCastleWalkNavigation.UpPrefer(const AUp: TVector3); |
| 4181 |
begin |
| 4182 |
Camera.UpPrefer(AUp); |
| 4183 |
end; |
| 4184 |
|
| 4185 |
function TCastleWalkNavigation.MaxJumpDistance: Single; |
| 4186 |
begin |
| 4187 |
Result := JumpMaxHeight * PreferredHeight; |
| 4188 |
end; |
| 4189 |
|
| 4190 |
function TCastleWalkNavigation.GravityUpLocal: TVector3; |
| 4191 |
begin |
| 4192 |
if (Camera.Parent <> nil) and |
| 4193 |
(Camera.World <> nil) then |
| 4194 |
Result := Camera.Parent.WorldToLocalDirection(Camera.GravityUp).Normalize |
| 4195 |
else |
| 4196 |
Result := Camera.GravityUp; |
| 4197 |
end; |
| 4198 |
|
| 4199 |
function TCastleWalkNavigation.DirectionLeft: TVector3; |
| 4200 |
begin |
| 4201 |
Result := TVector3.CrossProduct(Camera.Up, Camera.Direction); |
| 4202 |
end; |
| 4203 |
|
| 4204 |
function TCastleWalkNavigation.DirectionRight: TVector3; |
| 4205 |
begin |
| 4206 |
Result := TVector3.CrossProduct(Camera.Direction, Camera.Up); |
| 4207 |
end; |
| 4208 |
|
| 4209 |
procedure TCastleWalkNavigation.FallOnTheGround; |
| 4210 |
begin |
| 4211 |
FFallingOnTheGround := true; |
| 4212 |
|
| 4213 |
{ Mathematically reasoning, this should be smarter. |
| 4214 |
I mean that we should randomize FFallingOnTheGroundAngleIncrease |
| 4215 |
*only* if Up is parallel to GravityUp ? |
| 4216 |
Otherwise Up could change through some strange path ? |
| 4217 |
|
| 4218 |
But current effect seems to behave good in all situations... |
| 4219 |
In any case, Up going through some strange path will only |
| 4220 |
be noticeable for a very short time, so I don't think that's a real |
| 4221 |
problem... unless I see some example when it looks bad. } |
| 4222 |
|
| 4223 |
FFallingOnTheGroundAngleIncrease := RandomBoolean; |
| 4224 |
end; |
| 4225 |
|
| 4226 |
procedure TCastleWalkNavigation.CancelFalling; |
| 4227 |
begin |
| 4228 |
{ Fortunately implementation of this is brutally simple right now. } |
| 4229 |
FFalling := false; |
| 4230 |
end; |
| 4231 |
|
| 4232 |
procedure TCastleWalkNavigation.ProcessMouseLookDelta(const Delta: TVector2); |
| 4233 |
begin |
| 4234 |
inherited; |
| 4235 |
RotateHorizontal(-Delta.X); |
| 4236 |
RotateVertical(Delta.Y); |
| 4237 |
end; |
| 4238 |
|
| 4239 |
function TCastleWalkNavigation.Motion(const Event: TInputMotion): boolean; |
| 4240 |
|
| 4241 |
procedure HandleMouseDrag; |
| 4242 |
var |
| 4243 |
MouseChange: TVector2; |
| 4244 |
begin |
| 4245 |
MouseChange := Event.Position - Container.MousePosition; |
| 4246 |
if MouseChange[0] <> 0 then |
| 4247 |
RotateHorizontal(-MouseChange[0] * MouseDraggingHorizontalRotationSpeed); |
| 4248 |
if MouseChange[1] <> 0 then |
| 4249 |
RotateVertical(MouseChange[1] * MouseDraggingVerticalRotationSpeed); |
| 4250 |
end; |
| 4251 |
|
| 4252 |
begin |
| 4253 |
Result := inherited; |
| 4254 |
if Result or (Event.FingerIndex <> 0) then Exit; |
| 4255 |
|
| 4256 |
{ It is possible to have MouseDraggingStarted = -1 even when all buttons |
| 4257 |
released: |
| 4258 |
|
| 4259 |
- When setting MouseDraggingStarted to >= 0 in Press, |
| 4260 |
we do not set Result:=true to avoid other issues |
| 4261 |
(see comments in TCastleNevigation.Press). |
| 4262 |
- But as a consequence, TCastleTouchNavigation.Press may also start dragging |
| 4263 |
at the same time. |
| 4264 |
- Then when user releases the mouse, |
| 4265 |
TCastleTouchNavigation.Release is called, |
| 4266 |
and TCastleNavigation.Release is not called. |
| 4267 |
This is actually regardless of whether TCastleTouchNavigation.Release |
| 4268 |
returns false or true. It's because TCastleContainer.EventRelease |
| 4269 |
calls only Capture.Release + Exit when Capture <> nil, |
| 4270 |
it doesn't call Release on others. |
| 4271 |
- In effect, since our TCastleNavigation.Release wasn't called, |
| 4272 |
MouseDraggingStarted is left at -1. |
| 4273 |
|
| 4274 |
Testcase: simple_3d_demo, click on touch navigation in the corner, |
| 4275 |
then release. When moving mouse, you should *not* rotate the camera now. } |
| 4276 |
if Event.Pressed = [] then |
| 4277 |
MouseDraggingStarted := -1; |
| 4278 |
|
| 4279 |
if (MouseDraggingStarted <> -1) and |
| 4280 |
// Not need to check here ReallyEnableMouseDragging, as MouseDraggingStarted is already <> -1 |
| 4281 |
// ReallyEnableMouseDragging and |
| 4282 |
(MouseDragMode = mdRotate) and |
| 4283 |
Valid and |
| 4284 |
(not InternalUsingMouseLook) then |
| 4285 |
begin |
| 4286 |
HandleMouseDrag; |
| 4287 |
Result := true; |
| 4288 |
end; |
| 4289 |
end; |
| 4290 |
|
| 4291 |
function TCastleWalkNavigation.PropertySections( |
| 4292 |
const PropertyName: String): TPropertySections; |
| 4293 |
begin |
| 4294 |
if ArrayContainsString(PropertyName, [ |
| 4295 |
'Gravity', 'MoveSpeed', 'Radius', 'CrouchHeight', 'PreferredHeight', |
| 4296 |
'MoveHorizontalSpeed', 'MoveVerticalSpeed', |
| 4297 |
'MouseDraggingHorizontalRotationSpeed', 'MouseDraggingVerticalRotationSpeed', |
| 4298 |
'MouseDraggingMoveSpeed', 'MouseDragMode', 'RotationHorizontalSpeed', |
| 4299 |
'RotationVerticalSpeed' |
| 4300 |
]) then |
| 4301 |
Result := [psBasic] |
| 4302 |
else |
| 4303 |
Result := inherited PropertySections(PropertyName); |
| 4304 |
end; |
| 4305 |
|
| 4306 |
class procedure TCastleWalkNavigation.CreateComponentFly(Sender: TObject); |
| 4307 |
begin |
| 4308 |
(Sender as TCastleWalkNavigation).Gravity := false; |
| 4309 |
end; |
| 4310 |
|
| 4311 |
function TCastleWalkNavigation.DirectionInGravityPlane: TVector3; |
| 4312 |
var |
| 4313 |
Grav: TVector3; |
| 4314 |
begin |
| 4315 |
Result := Camera.Direction; |
| 4316 |
|
| 4317 |
Grav := GravityUpLocal; |
| 4318 |
if not VectorsParallel(Result, Grav) then |
| 4319 |
MakeVectorsOrthoOnTheirPlane(Result, Grav); |
| 4320 |
end; |
| 4321 |
|
| 4322 |
{ global ------------------------------------------------------------ } |
| 4323 |
|
| 4324 |
procedure CorrectPreferredHeight(var PreferredHeight: Single; |
| 4325 |
const Radius: Single; const CrouchHeight, HeadBobbing: Single); |
| 4326 |
var |
| 4327 |
NewPreferredHeight: Single; |
| 4328 |
begin |
| 4329 |
{ We have requirement that |
| 4330 |
PreferredHeight * CrouchHeight * (1 - HeadBobbing) >= Radius |
| 4331 |
So |
| 4332 |
PreferredHeight >= Radius / (CrouchHeight * (1 - HeadBobbing)); |
| 4333 |
|
| 4334 |
I make it even a little larger (that's the reason for "* 1.01") to be |
| 4335 |
sure to avoid floating-point rounding errors. } |
| 4336 |
|
| 4337 |
NewPreferredHeight := 1.01 * Radius / |
| 4338 |
(CrouchHeight * (1 - HeadBobbing)); |
| 4339 |
|
| 4340 |
if PreferredHeight < NewPreferredHeight then |
| 4341 |
PreferredHeight := NewPreferredHeight; |
| 4342 |
end; |
| 4343 |
|
| 4344 |
var |
| 4345 |
R: TRegisteredComponent; |
| 4346 |
initialization |
| 4347 |
R := TRegisteredComponent.Create; |
| 4348 |
R.ComponentClass := TCastleWalkNavigation; |
| 4349 |
R.Caption := ['Navigation', 'Fly (Walk with Gravity=false)']; |
| 4350 |
R.OnCreate := {$ifdef FPC}@{$endif}TCastleWalkNavigation.CreateComponentFly; |
| 4351 |
RegisterSerializableComponent(R); |
| 4352 |
|
| 4353 |
RegisterSerializableComponent(TCastleWalkNavigation, ['Navigation', 'Walk']); |
| 4354 |
RegisterSerializableComponent(TCastleExamineNavigation, ['Navigation', 'Examine']); |
| 4355 |
RegisterSerializableComponent(TCastle2DNavigation, ['Navigation', '2D']); |
| 4356 |
end. |