Euphoria
fpscontroller.cc
Go to the documentation of this file.
1 #include "core/fpscontroller.h"
2 
3 namespace eu::core
4 {
5  using namespace eu::convert;
6 
8  : rotation_angle(0.0_rad)
9  , look_angle(0.0_rad)
10  , position(zero3f)
11  , look_sensitivity(0.10f)
12  {}
13 
14  void
15  FpsController::look(float x, float y)
16  {
19 
21  }
22 
23  void
25  {
26  is_left_down = down;
27  }
28  void
30  {
31  is_right_down = down;
32  }
33  void
35  {
36  is_forward_down = down;
37  }
38  void
40  {
41  is_backward_down = down;
42  }
43  void
45  {
46  is_up_down = down;
47  }
48  void
50  {
51  is_down_down = down;
52  }
53 
54  void
55  FpsController::on_key(Key key, bool down)
56  {
57  switch(key)
58  {
59  case Key::w:
60  case Key::up: move_forward(down); return;
61  case Key::s:
62  case Key::down: move_backward(down); return;
63  case Key::a:
64  case Key::left: move_left(down); return;
65  case Key::d:
66  case Key::right: move_right(down); return;
67 
68  case Key::space: move_up(down); return;
69  case Key::ctrl_left: move_down(down); return;
70  default: return;
71  }
72  }
73 
74  void
75  FpsController::update(float delta)
76  {
77  int forward = 0;
78  int right = 0;
79  int up = 0;
80 
81  if(is_forward_down)
82  {
83  forward += 1;
84  }
86  {
87  forward -= 1;
88  }
89 
90  if(is_right_down)
91  {
92  right += 1;
93  }
94  if(is_left_down)
95  {
96  right -= 1;
97  }
98 
99  if(is_up_down)
100  {
101  up += 1;
102  }
103  if(is_down_down)
104  {
105  up -= 1;
106  }
107 
108  if(forward == 0 && right == 0 && up == 0)
109  {
110  return;
111  }
112 
113  const auto input = get_rotation()
115  (
116  vec3f
117  {
118  static_cast<float>(right),
119  static_cast<float>(up),
120  static_cast<float>(forward)
121  }
122  )
123  .get_normalized()
124  ;
125  const auto movement = input * move_speed * delta;
126 
127  position += movement;
128  }
129 
130  quatf
131  FpsController::calc_rotation(const Angle& rotation_angle, const Angle& look_angle)
132  {
133  const auto rotation = quatf::from_axis_angle
134  (
136  );
137  const auto look = quatf::from_axis_angle
138  (
140  );
141  return rotation * look;
142  }
143 
144  quatf
146  {
148  }
149 
150 
151  void
153  {
154  look_angle = asin(direction.y);
155  rotation_angle = atan2(direction.x, direction.z) + Angle::from_degrees(180.0f);
156  }
157 
158 }
constexpr unit3f x_axis
Definition: vec3.h:128
constexpr unit3f y_axis
Definition: vec3.h:129
Key
Definition: key.h:26
Angle atan2(float y, float x)
Definition: angle.cc:107
Angle asin(float v)
Definition: angle.cc:82
constexpr vec3f zero3f
Definition: vec3.h:95
void wrap()
Definition: angle.cc:19
constexpr static Angle from_degrees(float degrees)
Definition: angle.h:16
static AxisAngle from_right_hand_around(const unit3f &axis, const Angle &angle)
Definition: axisangle.cc:14
void look(float delta_rot, float delta_look)
quatf get_rotation() const
void move_down(bool down)
Sensitivity look_sensitivity
Definition: fpscontroller.h:23
static quatf calc_rotation(const Angle &rotation_angle, const Angle &look_angle)
void move_forward(bool down)
void move_right(bool down)
void move_backward(bool down)
void on_key(Key key, bool down)
void move_left(bool down)
void update(float delta)
void look_in_direction(const unit3f &v)
void move_up(bool down)
float get_multiplier_with_sign() const
Definition: sensitivity.cc:22
Definition: quat.h:15
static quatf from_axis_angle(const AxisAngle &aa)
Definition: quat.cc:23
vec3f create_from_right_up_in(const vec3f &v) const
returns In*Z + Right*X + Up*Y
Definition: quat.cc:196
Definition: vec3.h:48
float x
Definition: vec3.h:49
float y
Definition: vec3.h:50
float z
Definition: vec3.h:51