-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrols.cpp
156 lines (118 loc) · 3.42 KB
/
controls.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "controls.hpp"
#include "engine.hpp"
#include "vec.hpp"
///error
cl_float4 rot(double x, double y, double z, cl_float4 rotation)
{
double i0x=x;
double i0y=y;
double i0z=z;
double i1x=i0x;
double i1y=i0y*cos(rotation.x) - sin(rotation.x)*i0z;
double i1z=i0y*sin(rotation.x) + cos(rotation.x)*i0z;
double i2x=i1x*cos(rotation.y) + i1z*sin(rotation.y);
double i2y=i1y;
double i2z=-i1x*sin(rotation.y) + i1z*cos(rotation.y);
double i3x=i2x*cos(rotation.z) - i2y*sin(rotation.z);
double i3y=i2x*sin(rotation.z) + i2y*cos(rotation.z);
double i3z=i2z;
cl_float4 ret;
ret.x=i3x;
ret.y=i3y;
ret.z=i3z;
return ret;
}
control_input::control_input(decltype(get_input_delta) delta_func, decltype(process_controls) controls_func)
{
get_input_delta = delta_func;
process_controls = controls_func;
}
///time in microseconds
///should really template this and give it a tag
///or stick it in a function map with an enum
input_delta get_input_delta_default(float delta_time, const input_delta& input, engine& eng)
{
sf::Keyboard keyboard;
int distance_multiplier=1;
cl_float4 in_pos = input.c_pos;
cl_float4 in_rot = input.c_rot_keyboard_only;
cl_float4 delta_pos = {0,0,0};
cl_float4 delta_rot = {0,0,0};
///handle camera input. Update to be based off frametime
if(keyboard.isKeyPressed(sf::Keyboard::LShift))
{
distance_multiplier=10;
}
else
{
distance_multiplier=1;
}
float distance=0.04f*distance_multiplier*30 * delta_time / 8000.f;
if(keyboard.isKeyPressed(sf::Keyboard::W))
{
cl_float4 t=rot(0, 0, distance, in_rot);
delta_pos = add(delta_pos, t);
}
if(keyboard.isKeyPressed(sf::Keyboard::S))
{
cl_float4 t=rot(0, 0, -distance, in_rot);
delta_pos = add(delta_pos, t);
}
if(keyboard.isKeyPressed(sf::Keyboard::A))
{
cl_float4 t=rot(-distance, 0, 0, in_rot);
delta_pos = add(delta_pos, t);
}
if(keyboard.isKeyPressed(sf::Keyboard::D))
{
cl_float4 t=rot(distance, 0, 0, in_rot);
delta_pos = add(delta_pos, t);
}
if(keyboard.isKeyPressed(sf::Keyboard::E))
{
delta_pos.y-=distance;
}
if(keyboard.isKeyPressed(sf::Keyboard::Q))
{
delta_pos.y+=distance;
}
float camera_mult = delta_time / 8000.f;
if(keyboard.isKeyPressed(sf::Keyboard::Left))
{
delta_rot.y-=0.001*30 * camera_mult;
}
if(keyboard.isKeyPressed(sf::Keyboard::Right))
{
delta_rot.y+=0.001*30 * camera_mult;
}
if(keyboard.isKeyPressed(sf::Keyboard::Up))
{
delta_rot.x-=0.001*30 * camera_mult;
}
if(keyboard.isKeyPressed(sf::Keyboard::Down))
{
delta_rot.x+=0.001*30 * camera_mult;
}
return {delta_pos, delta_rot, 0.f};
}
void process_controls_default(float delta_time, engine& eng)
{
auto c_pos = eng.c_pos;
auto c_rot = eng.c_rot;
sf::Keyboard keyboard;
if(keyboard.isKeyPressed(sf::Keyboard::F10))
{
eng.request_close();
}
if(keyboard.isKeyPressed(sf::Keyboard::B))
{
std::cout << "rerr: " << c_pos.x << " " << c_pos.y << " " << c_pos.z << std::endl;
}
if(keyboard.isKeyPressed(sf::Keyboard::N))
{
std::cout << "rotation: " << c_rot.x << " " << c_rot.y << " " << c_rot.z << std::endl;
}
}
void process_controls_empty(float delta_time, engine& eng)
{
}