xoreos  0.0.5
satellitecamera.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos 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; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/maths.h"
26 
28 
29 #include "src/graphics/camera.h"
30 
32 
33 namespace Engines {
34 
35 static const float kRotationSpeed = M_PI / 2.f;
36 
38  : _distance(0.0f),
39  _yaw(0.0f),
40  _pitch(0.0f),
41  _pitchSin(0.0f),
42  _pitchCos(1.0f),
43  _height(0.0f),
44  _leftBtnPressed(false),
45  _rightBtnPressed(false),
46  _dirty(true) {
47 }
48 
49 void SatelliteCamera::setTarget(float x, float y, float z) {
50  _target.x = x;
51  _target.y = y;
52  _target.z = z;
53  _dirty = true;
54 }
55 
56 void SatelliteCamera::setDistance(float value) {
57  _distance = value;
58  _dirty = true;
59 }
60 
61 void SatelliteCamera::setPitch(float value) {
62  _pitch = value;
63  float pitchRad = Common::deg2rad(_pitch);
64  _pitchSin = sin(pitchRad);
65  _pitchCos = cos(pitchRad);
66  _dirty = true;
67 }
68 
69 void SatelliteCamera::setHeight(float value) {
70  _height = value;
71  _dirty = true;
72 }
73 
74 void SatelliteCamera::setYaw(float value) {
75  _yaw = value;
76  _dirty = true;
77 }
78 
79 float SatelliteCamera::getYaw() const {
80  return _yaw;
81 }
82 
84  switch (e.type) {
87  switch (e.key.keysym.scancode) {
88  case SDL_SCANCODE_A:
90  return true;
91 
92  case SDL_SCANCODE_D:
94  return true;
95 
96  default:
97  break;
98  }
99  break;
100 
101  default:
102  break;
103  }
104 
105  return false;
106 }
107 
108 void SatelliteCamera::update(float dt) {
109  if (_dirty ||
113  _yaw += kRotationSpeed * dt;
114  _yaw = fmodf(_yaw, 2.0f * M_PI);
115  }
117  _yaw -= kRotationSpeed * dt;
118  _yaw = fmodf(_yaw, 2.0f * M_PI);
119  }
120 
121  float x = _target.x + _distance * sin(_yaw);
122  float y = _target.y - _distance * cos(_yaw);
123  float z = _target.z + _height;
124 
125  CameraMan.setPosition(x, y, z);
126  CameraMan.setOrientation(_pitch, 0.0f, Common::rad2deg(_yaw));
127  CameraMan.update();
128 
129  _dirty = false;
130  }
131 }
132 
134  _leftBtnPressed = false;
135  _rightBtnPressed = false;
136 }
137 
138 } // End of namespace Engines
void setTarget(float x, float y, float z)
bool handleCameraInput(const Events::Event &e)
Mathematical helpers.
Camera management.
#define M_PI
Definition: maths.h:39
Engine utility class for camera handling where camera rotates around PC.
void setPitch(float value)
SDL_Event Event
Definition: types.h:42
Keyboard key was pressed.
Definition: types.h:46
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
void setDistance(float value)
void setYaw(float value)
void setHeight(float value)
uint32 _height
Definition: h263.cpp:54
#define CameraMan
Shortcut for accessing the camera manager.
Definition: camera.h:83
static const float kRotationSpeed
static float rad2deg(float rad)
Definition: maths.h:93
Keyboard key was released.
Definition: types.h:47
static float deg2rad(float deg)
Definition: maths.h:97