MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cameracontroller.cpp
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine 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. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _cameracontroller_cpp
41 #define _cameracontroller_cpp
42 
43 #include "cameracontroller.h"
44 #include "Graphics/cameraproxy.h"
45 #include "mathtool.h"
46 #include "rayquerytool.h"
47 #include "ray.h"
48 #include "enumerations.h"
49 #include "actor.h"
50 
51 namespace Mezzanine
52 {
54  Controlled(ToBeControlled),
55  CurrentMMode(CCM_Fly),
56  HoverHeight(2),
57  YawRad(0),
58  PitchRad(0),
59  RollRad(0),
60  YawLimits(NULL),
61  PitchLimits(NULL),
62  RollLimits(NULL)
63  { }
64 
66  {
67  this->RemoveYawLimits();
68  this->RemovePitchLimits();
69  this->RemoveRollLimits();
70  }
71 
72  void CameraController::CheckAngleRollover(Real Angle)
73  {
74  Real Pi = MathTools::GetPi();
75  if(Angle > Pi)
76  {
77  Angle = -Pi + (Angle - Pi);
78  }
79  else if(Angle < -Pi)
80  {
81  Angle = Pi + (Angle + Pi);
82  }
83  }
84 
85  void CameraController::CheckAngleLimits()
86  {
87  if(this->YawLimits)
88  {
89  if(YawRad > this->YawLimits->Upper) YawRad = this->YawLimits->Upper;
90  if(YawRad < this->YawLimits->Lower) YawRad = this->YawLimits->Lower;
91  }
92  if(this->PitchLimits)
93  {
94  if(PitchRad > this->PitchLimits->Upper) PitchRad = this->PitchLimits->Upper;
95  if(PitchRad < this->PitchLimits->Lower) PitchRad = this->PitchLimits->Lower;
96  }
97  if(this->YawLimits)
98  {
99  if(RollRad > this->RollLimits->Upper) RollRad = this->RollLimits->Upper;
100  if(RollRad < this->RollLimits->Lower) RollRad = this->RollLimits->Lower;
101  }
102  }
103 
104  void CameraController::CheckAllAngles()
105  {
106  this->CheckAngleLimits();
107  this->CheckAngleRollover(this->YawRad);
108  this->CheckAngleRollover(this->PitchRad);
109  this->CheckAngleRollover(this->RollRad);
110  }
111 
112  void CameraController::CheckHeight()
113  {
114  Vector3 Loc(this->Controlled->GetLocation());
115  Real Dist = this->FindDistanceToGround();
116  if(0==Dist)
117  return;
118  Real DeltaDist = Dist - this->HoverHeight;
119  Loc.Y -= DeltaDist;
120  this->Controlled->SetLocation(Loc);
121  }
122 
123  Real CameraController::FindDistanceToGround()
124  {
125  Vector3 Loc(this->Controlled->GetLocation());
126  Vector3 Dest(Loc + Vector3(0,-2000,0));
127  Ray GroundRay(Loc,Dest);
128  UInt32 flags = Mezzanine::WO_MeshTerrain | Mezzanine::WO_HeightfieldTerrain | Mezzanine::WO_VectorFieldTerrain | Mezzanine::WO_VoxelTerrain;
129  if(!RayCaster.GetFirstObjectOnRayByPolygon(GroundRay,flags))
130  { return 0; }
131  Real Distance = Loc.Y - (RayCaster.LastQueryResultsObjectPtr()->GetLocation() + RayCaster.LastQueryResultsOffset()).Y;
132  return Distance;
133  }
134 
135  ///////////////////////////////////////////////////////////////////////////////
136  // Utility
137 
139  {
140  return this->Controlled;
141  }
142 
143  ///////////////////////////////////////////////////////////////////////////////
144  // CameraController Properties
145 
147  {
148  this->CurrentMMode = MoveMode;
149  }
150 
152  {
153  return this->CurrentMMode;
154  }
155 
157  {
158  this->HoverHeight = Hover;
159  }
160 
162  {
163  return this->HoverHeight;
164  }
165 
166  void CameraController::SetYawLimits(const Real& UpperLimit, const Real& LowerLimit)
167  {
168  if(!this->YawLimits)
169  this->YawLimits = new AngleLimits();
170  this->YawLimits->Upper = UpperLimit;
171  this->YawLimits->Lower = LowerLimit;
172  }
173 
175  {
176  if(this->YawLimits)
177  {
178  delete this->YawLimits;
179  this->YawLimits = NULL;
180  }
181  }
182 
183  void CameraController::SetPitchLimits(const Real& UpperLimit, const Real& LowerLimit)
184  {
185  if(!this->PitchLimits)
186  this->PitchLimits = new AngleLimits();
187  this->PitchLimits->Upper = UpperLimit;
188  this->PitchLimits->Lower = LowerLimit;
189  }
190 
192  {
193  if(this->PitchLimits)
194  {
195  delete this->PitchLimits;
196  this->PitchLimits = NULL;
197  }
198  }
199 
200  void CameraController::SetRollLimits(const Real& UpperLimit, const Real& LowerLimit)
201  {
202  if(!this->RollLimits)
203  this->RollLimits = new AngleLimits();
204  this->RollLimits->Upper = UpperLimit;
205  this->RollLimits->Lower = LowerLimit;
206  }
207 
209  {
210  if(this->RollLimits)
211  {
212  delete this->RollLimits;
213  this->RollLimits = NULL;
214  }
215  }
216 
217  ///////////////////////////////////////////////////////////////////////////////
218  // Transform Methods
219 
221  {
222  Vector3 Move(0,0,-Units);
223  this->Controlled->Translate(Move);
224  if(CCM_Walk == this->CurrentMMode)
225  this->CheckHeight();
226  }
227 
229  {
230  Vector3 Move(0,0,Units);
231  this->Controlled->Translate(Move);
232  if(CCM_Walk == this->CurrentMMode)
233  this->CheckHeight();
234  }
235 
237  {
238  Vector3 Move(-Units,0,0);
239  Controlled->Translate(Move);
240  if(CCM_Walk == CurrentMMode)
241  CheckHeight();
242  }
243 
245  {
246  Vector3 Move(Units,0,0);
247  this->Controlled->Translate(Move);
248  if(CCM_Walk == this->CurrentMMode)
249  this->CheckHeight();
250  }
251 
252  void CameraController::Rotate(Real Yaw, Real Pitch, Real Roll)
253  {
254  this->YawRad += Yaw;
255  this->PitchRad += Pitch;
256  this->RollRad += Roll;
257  this->CheckAllAngles();
258  Quaternion YawQuat, PitchQuat, RollQuat;
259 
260  if(0 == this->YawRad)
261  YawQuat = Quaternion(0,0,0,1);
262  else
263  YawQuat = Quaternion(-YawRad,Vector3::Unit_Y());
264 
265  if(0 == this->PitchRad)
266  PitchQuat = Quaternion(0,0,0,1);
267  else
268  PitchQuat = Quaternion(-PitchRad,Vector3::Unit_X());
269 
270  if(0 == this->RollRad)
271  RollQuat = Quaternion(0,0,0,1);
272  else
273  RollQuat = Quaternion(-RollRad,Vector3::Unit_Z());
274 
275  Quaternion CamRot = YawQuat * PitchQuat * RollQuat;
276  this->Controlled->SetOrientation(CamRot);
277  }
278 
280  {
281  Quaternion CamRot(this->Controlled->GetOrientation());
282  CamRot = CamRot *
283  Quaternion(-Yaw,Vector3::Unit_Y()) *
284  Quaternion(-Pitch,Vector3::Unit_X()) *
285  Quaternion(-Roll,Vector3::Unit_Z());
286  this->Controlled->SetOrientation(CamRot);// */
287  }
288 }
289 
290 #endif