Planeshift
pscamera.h
Go to the documentation of this file.
1 /*
2  * Author: Andrew Robberts
3  *
4  * Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #ifndef PS_CAMERA_HEADER
21 #define PS_CAMERA_HEADER
22 //=============================================================================
23 // Crystal Space Includes
24 //=============================================================================
25 #include <csutil/csstring.h>
26 #include <csutil/ref.h>
27 #include <iutil/virtclk.h>
28 
29 //=============================================================================
30 // Project Includes
31 //=============================================================================
32 #include "net/cmdbase.h"
33 
34 //=============================================================================
35 // Local Includes
36 //=============================================================================
37 #include "psoptions.h"
38 
39 #define NPC_MODE_DISTANCE 5.0f
40 
41 struct RaceDefinition;
42 struct iView;
43 struct iPerspectiveCamera;
44 struct iSector;
45 struct iCollideSystem;
46 struct iMeshWrapper;
47 
48 class GEMClientActor;
49 class GEMClientObject;
50 
55 class psCamera : public iOptionsClass, public psCmdBase
56 {
57 public:
58 
59  psCamera();
60  virtual ~psCamera();
61 
62  void LoadOptions();
63  void SaveOptions();
64 
65  // the different camera modes
67  {
73 
74  // a special hidden camera mode that contains the actual data to use (all other camera modes set this one accordingly)
75  CAMERA_ACTUAL_DATA, // this should be the first entry here after the real camera modes
76  CAMERA_LAST_ACTUAL, // keep reference to what the actual data was last frame
77 
78  // This is a camera mode that occurs in the transition phase in between normal camera modes.
80 
81  // camera mode for talking to an NPC
83 
84  // A camera mode that is used when a collision occurs.
85  // It allows for collisions to be rectified faster than normal
87  CAMERA_ERR, // this is the error between the actual camera pos and its ideal
88 
90  };
91 
92  struct DistanceCfg
93  {
94  bool adaptive; // is distance adapted automatically ?
95  int dist; // fixed distance (used when adaptive=false)
96  int minFPS; // acceptable FPS interval (used when adaptive=true)
97  int maxFPS;
98  int minDist; // minimum acceptable distance (used when adaptive=true)
99  };
100 
101  const char *HandleCommand(const char *cmd);
102  void HandleMessage(MsgEntry *msg);
103 
104 
109  bool InitializeView(GEMClientActor* entity);
110 
114  void SetActor(GEMClientActor* entity);
115 
124  void npcTargetReplaceIfEqual(GEMClientObject *currentEntity, GEMClientObject* replacementEntity);
125 
131  bool LoadFromFile(bool useDefault=false, bool overrideCurrent=true);
132 
136  bool SaveToFile();
137 
141  bool Draw();
142 
147  void SetPosition(const csVector3& pos, int mode = -1);
148 
153  csVector3 GetPosition(int mode = -1) const;
154 
161  void SetTarget(const csVector3& tar, int mode = -1);
162 
167  csVector3 GetTarget(int mode = -1) const;
168 
175  void SetUp(const csVector3& up, int mode = -1);
176 
183  csVector3 GetUp(int mode = -1) const;
184 
188  void LockCameraMode(bool state);
189 
193  void SetCameraMode(int mode);
194 
197  void NextCameraMode();
198 
202  int GetCameraMode() const;
203 
207  csString GetCameraModeVerbose() const;
208 
212  iPerspectiveCamera *GetICamera();
213 
217  iView *GetView();
218 
226  iMeshWrapper * Get3DPointFrom2D(int x, int y, csVector3 * worldCoord=0, csVector3 * untransfCoord=0);
227 
237  iMeshWrapper* FindMeshUnder2D(int x, int y, csVector3 * pos = NULL, int *poly = NULL);
238 
243  void SetPitch(float pitch, int mode = -1);
244 
249  void MovePitch(float deltaPitch, int mode = -1);
250 
255  float GetPitch(int mode = -1) const;
256 
260  void SetPitchVelocity(float pitchVel);
261 
265  float GetPitchVelocity() const;
266 
271  void SetYaw(float yaw, int mode = -1);
272 
277  void MoveYaw(float deltaYaw, int mode = -1);
278 
283  float GetYaw(int mode = -1) const;
284 
288  void SetYawVelocity(float yawVel);
289 
293  float GetYawVelocity() const;
294 
299  void SetDistance(float distance, int mode = -1);
300 
305  void MoveDistance(float deltaDistance, int mode = -1);
306 
311  float GetDistance(int mode = -1) const;
312 
315  void ResetActualCameraData();
316 
319  void ResetCameraPositioning();
320 
324  bool RotateCameraWithPlayer() const;
325 
330  csVector3 GetForwardVector(int mode = -1) const;
331 
336  csVector3 GetRightVector(int mode = -1) const;
337 
342  float GetMinDistance(int mode = -1) const;
343 
348  void SetMinDistance(float dist, int mode = -1);
349 
354  float GetMaxDistance(int mode = -1) const;
355 
360  void SetMaxDistance(float dist, int mode = -1);
361 
366  float GetTurnSpeed(int mode = -1) const;
367 
372  void SetTurnSpeed(float speed, int mode = -1);
373 
378  float GetSpringCoef(int mode = -1) const;
379 
384  void SetSpringCoef(float coef, int mode = -1);
385 
390  float GetDampeningCoef(int mode = -1) const;
391 
396  void SetDampeningCoef(float coef, int mode = -1);
397 
402  float GetSpringLength(int mode = -1) const;
403 
408  void SetSpringLength(float length, int mode = -1);
409 
413  bool CheckCameraCD() const;
414 
418  void SetCameraCD(bool useCD);
419 
423  bool GetUseNPCCam();
424 
428  void SetUseNPCCam(bool useNPCCam);
429 
433  float GetTransitionThreshold() const;
434 
438  void SetTransitionThreshold(float threshold);
439 
444  float GetDefaultPitch(int mode = -1) const;
445 
450  void SetDefaultPitch(float pitch, int mode = -1);
451 
456  float GetDefaultYaw(int mode = -1) const;
457 
462  void SetDefaultYaw(float yaw, int mode = -1);
463 
468  float GetSwingCoef(int mode = -1) const;
469 
474  void SetSwingCoef(float swingCoef, int mode = -1);
475 
479  void UseFixedDistanceClipping(float dist);
480 
486  void UseAdaptiveDistanceClipping(int minFPS, int maxFPS, int minDist);
487 
491  int GetLastCameraMode();
492 
494  void SetDistanceCfg(DistanceCfg newcfg);
495 
496  int GetFixedDistClip() { return fixedDistClip; }
497 
498  bool IsInitialized() { return cameraInitialized; }
499 
500 private:
501 
502  struct CameraData
503  {
504  csVector3 worldPos;
505  csVector3 worldTar;
506  csVector3 worldUp;
507 
508  float pitch;
509  float yaw;
510  float roll; // not implemented yet (no need atm)
511 
512  float defaultPitch;
513  float defaultYaw;
514  float defaultRoll;
515 
516  float distance; // the distance between position and target
517  float minDistance;
518  float maxDistance;
519 
520  float turnSpeed;
521 
522  float springCoef;
523  float InertialDampeningCoef;
524  float springLength;
525 
526  float swingCoef;
527 
528  //initialize all here:
529  CameraData()
530  {
531  worldPos.Set(0.0f,0.0f,0.0f);
532  worldTar.Set(0.0f,0.0f,0.0f);
533  worldUp.Set(0.0f,0.0f,0.0f);
534  pitch = yaw = roll = 0;
535  defaultPitch = defaultYaw = defaultRoll = 0.0f;
536  distance = minDistance = maxDistance = 0.0f;
537  turnSpeed = 0.0f;
538  springCoef = InertialDampeningCoef = springLength = 0.0f;
539  swingCoef=0.0f;
540  }
541  };
542 
543  float CalcSpringCoef(float springyness, float scale) const;
544  float CalcInertialDampeningCoef(float springyness, float scale) const;
545  float EstimateSpringyness(size_t camMode) const;
546 
553  void DoCameraIdealCalcs(const csTicks elapsedTicks, const csVector3& actorPos, const csVector3& actorEye, const float actorYRot);
554 
562  csVector3 CalcCollisionPos(const csVector3& pseudoTarget, const csVector3& pseudoPosition, iSector*& sector);
563 
566  void DoCameraTransition();
567 
576  void DoElasticPhysics(bool isElastic, const csTicks elapsedTicks, const CameraData& deltaIdeal, iSector* sector);
577 
581  void EnsureActorVisibility();
582 
596  csVector3 CalcElasticPos(csVector3 currPos, csVector3 idealPos, csVector3 deltaIdealPos,
597  float deltaTime, float springCoef, float dampCoef, float springLength) const;
598 
599 
613  float CalcElasticFloat(float curr, float ideal, float deltaIdeal,
614  float deltaTime, float springCoef, float dampCoef, float springLength) const;
615 
616 
620  void CalculateFromYawPitchRoll(int mode = -1);
621 
625  void CalculateNewYawPitchRoll(int mode = -1);
626 
630  void CalculatePositionFromYawPitchRoll(int mode = -1);
631 
636  float CalculateNewYaw(csVector3 dir);
637 
641  void EnsureCameraDistance(int mode = -1);
642 
647  bool IsActorVisible(int mode = -1) const;
648 
653  float SaturateAngle(float angle) const;
654 
660  bool CloneCameraModeData(int fromMode, int toMode = -1);
661 
665  void SetDistanceClipping(float dist);
666  float GetDistanceClipping();
667 
671  void AdaptDistanceClipping();
672 
673  // each mode's camera data has to be handled seperately
674  CameraData camData[CAMERA_MODES_COUNT];
675 
676  int lastCameraMode;
677  int currCameraMode;
678 
679  // velocity stuff
680  float pitchVelocity;
681  float yawVelocity;
682 
683  // some camera settings
684  // NOTE: the offsets are not in the format you think they are, due to speed issues the offset is restricted to being directly behind the actor (no yaw).
685  csVector3 firstPersonPositionOffset;
686  csVector3 thirdPersonPositionOffset;
687 
688  /* if the squared difference between the actual camera position and the ideal position is less than this,
689  * then we're no longer in the transition phase.
690  */
691  float transitionThresholdSquared;
692  bool inTransitionPhase;
693 
695  bool hasCollision;
696 
698  bool cameraHasBeenPositioned;
699 
701  bool useCameraCD;
702 
704  bool useNPCCam;
705 
707  bool cameraInitialized;
708 
710  bool lockedCameraMode;
711 
712  csWeakRef<iSector> lastTargetSector;
713 
714  GEMClientActor* actor;
715 
717  csRef<iView> view;
718 
720  csRef<iVirtualClock> vc;
721 
723  csRef<iCollideSystem> cdsys;
724 
726  csTicks prevTicks;
727 
729  RaceDefinition *race;
730 
731  DistanceCfg distanceCfg;
732 
737  GEMClientActor* npcModeTarget;
738 
740  csVector3 npcModePosition;
741 
743  float npcOldRot;
744  float vel;
745  int fixedDistClip;
746 };
747 
748 #endif
void SetCameraCD(bool useCD)
sets whether the camera is performing CD or not
void SetTarget(const csVector3 &tar, int mode=-1)
Sets the current target of the camera (different for each mode).
void MoveDistance(float deltaDistance, int mode=-1)
moves the distance from the camera position to its target
void ResetActualCameraData()
resets the actual camera data to the player position and stuff
void MoveYaw(float deltaYaw, int mode=-1)
moves the yaw (left/right) of the camera
This is a player or another &#39;alive&#39; entity on the client.
Definition: pscelclient.h:554
iMeshWrapper * FindMeshUnder2D(int x, int y, csVector3 *pos=NULL, int *poly=NULL)
Returns the mesh under the 2D point.
CAMERA_MODES
Definition: pscamera.h:66
void SetUp(const csVector3 &up, int mode=-1)
Sets the current up vector of the camera (different for each mode).
void ResetCameraPositioning()
flags the camera positioning as bad so that it will reset it next update loop
csVector3 GetTarget(int mode=-1) const
returns the current position of the camera (different for each mode)
int GetFixedDistClip()
Definition: pscamera.h:496
bool Draw()
applies the camera data to the viewport and renders the 3D scene
int GetLastCameraMode()
gets the last used camera mode
void SetSpringCoef(float coef, int mode=-1)
sets the spring coefficient of the camera
void HandleMessage(MsgEntry *msg)
void SetTransitionThreshold(float threshold)
sets the camera transition threshold (the distance between camera position and ideal where the camera...
Defines a race.
Definition: psclientchar.h:205
void SetMinDistance(float dist, int mode=-1)
sets the min distance from the camera position to the target
csVector3 GetUp(int mode=-1) const
Returns the current up vector of the camera (different for each mode).
float GetSwingCoef(int mode=-1) const
returns the swing coefficient of the specific camera mode
The structure of 1 queue entry (pointer to a message)
Definition: message.h:143
void SetDefaultYaw(float yaw, int mode=-1)
sets the default (starting) yaw for the specific camera mode
void SetCameraMode(int mode)
sets the current camera mode
void MovePitch(float deltaPitch, int mode=-1)
moves the pitch (up/down) of the camera
float GetYawVelocity() const
gets the yaw (up/down) velocity of the camera
float GetPitchVelocity() const
gets the pitch (up/down) velocity of the camera
float GetDefaultYaw(int mode=-1) const
returns the default (starting) yaw of the specific camera mode
void SetPosition(const csVector3 &pos, int mode=-1)
sets the current position of the camera (different for each mode)
float GetDefaultPitch(int mode=-1) const
returns the default (starting) pitch of the specific camera mode
void SetPitchVelocity(float pitchVel)
sets the pitch (up/down) velocity of the camera
void SetMaxDistance(float dist, int mode=-1)
sets the max distance from the camera position to the target
float GetYaw(int mode=-1) const
returns the yaw (left/right) of the camera
void npcTargetReplaceIfEqual(GEMClientObject *currentEntity, GEMClientObject *replacementEntity)
Replace the actor the camera is targeting when in npc camera mode.
csString GetCameraModeVerbose() const
returns the current camera mode (text description)
Handles camera modes like first person, thrid person, speak to npc manages collision of camera with m...
Definition: pscamera.h:55
void SetTurnSpeed(float speed, int mode=-1)
sets the turning speed of the camera (ignored for most camera modes)
const char * HandleCommand(const char *cmd)
Interprets a received message and executes the command.
float GetMinDistance(int mode=-1) const
returns the min distance from the camera position to the target
void NextCameraMode()
changes the current camera mode to the next available camera mode
void LoadOptions()
This function is called when the options system has been issued a Save() call.
csVector3 GetForwardVector(int mode=-1) const
returns the forward vector of the camera
void LockCameraMode(bool state)
locks the current camera mode so it cannot be changed
float GetTurnSpeed(int mode=-1) const
returns the turning speed of the camera (ignored for most camera modes)
float GetDampeningCoef(int mode=-1) const
returns the dampening coefficient of the camera
iPerspectiveCamera * GetICamera()
returns the camera that CS use
An object that the client knows about.
Definition: pscelclient.h:373
void SetSwingCoef(float swingCoef, int mode=-1)
sets the swing coefficient of the specific camera mode
void SetDistanceCfg(DistanceCfg newcfg)
float GetTransitionThreshold() const
gets the camera transition threshold (the distance between camera position and ideal where the camera...
iMeshWrapper * Get3DPointFrom2D(int x, int y, csVector3 *worldCoord=0, csVector3 *untransfCoord=0)
returns the 3D point where the line under the given point intersects with the world ...
void UseAdaptiveDistanceClipping(int minFPS, int maxFPS, int minDist)
puts camera to adaptive distance clipping mode - camera will automatically adjust the distance to sta...
bool SaveToFile()
saves the camera settings to a file
float GetPitch(int mode=-1) const
returns the pitch (up/down) of the camera
float GetDistance(int mode=-1) const
returns the distance from the camera position to its target
void SetUseNPCCam(bool useNPCCam)
sets whether the npc chat camera is used or not
void SetYaw(float yaw, int mode=-1)
sets the yaw (left/right) of the camera
bool InitializeView(GEMClientActor *entity)
creates the viewport
void SetYawVelocity(float yawVel)
sets the yaw (up/down) velocity of the camera
void SetActor(GEMClientActor *entity)
Set the actor the camera is following.
bool IsInitialized()
Definition: pscamera.h:498
void SetDampeningCoef(float coef, int mode=-1)
sets the dampening coefficient of the camera
float GetSpringCoef(int mode=-1) const
returns the spring coefficient of the camera
void UseFixedDistanceClipping(float dist)
puts camera to fixed distance clipping mode - distance clipping will be set to constant value ...
int GetCameraMode() const
returns the current camera mode (as integer)
csVector3 GetRightVector(int mode=-1) const
returns the forward vector of the camera
bool CheckCameraCD() const
returns whether the camera is performing CD or not
csVector3 GetPosition(int mode=-1) const
returns the current position of the camera (different for each mode)
float GetSpringLength(int mode=-1) const
returns the spring length of the camera
iView * GetView()
returns the CS viewport
DistanceCfg GetDistanceCfg()
void SaveOptions()
This function is called whenever the options system needs the options class to give it its options va...
bool GetUseNPCCam()
returns whether the npc chat camera is used or not
void SetSpringLength(float length, int mode=-1)
sets the spring length of the camera
void SetPitch(float pitch, int mode=-1)
sets the pitch (up/down) of the camera
virtual ~psCamera()
bool LoadFromFile(bool useDefault=false, bool overrideCurrent=true)
loads the camera settings from a file
float GetMaxDistance(int mode=-1) const
returns the max distance from the camera position to the target
void SetDistance(float distance, int mode=-1)
sets the distance from the camera position to its target
bool RotateCameraWithPlayer() const
moves decides, based on the camera mode, whether the camera should be rotated along-side the player ...
void SetDefaultPitch(float pitch, int mode=-1)
sets the default (starting) pitch for the specific camera mode