summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2017-01-16 19:14:19 +0100
committerStanislaw Halik <sthalik@misaki.pl>2017-01-16 19:14:19 +0100
commitce65b5437b26004ad69763443dd167154042c97a (patch)
tree9964af0409f14b416b867b431d5d4707c93e7f84
parentce217c33d2f588fa239a97a772296046888f0b80 (diff)
proto/mouse: don't depend on float difference
It underflowed cause of our high dt. Also get the smallest difference over the screen, for HMD devices. Allow for very low-sensitivity operation. Issue: #523
-rw-r--r--proto-mouse/ftnoir_protocol_mouse.cpp61
-rw-r--r--proto-mouse/ftnoir_protocol_mouse.h11
2 files changed, 25 insertions, 47 deletions
diff --git a/proto-mouse/ftnoir_protocol_mouse.cpp b/proto-mouse/ftnoir_protocol_mouse.cpp
index 85b8cf1d..5e8a7bc1 100644
--- a/proto-mouse/ftnoir_protocol_mouse.cpp
+++ b/proto-mouse/ftnoir_protocol_mouse.cpp
@@ -30,22 +30,18 @@ void mouse::pose(const double *headpose)
if (axis_x >= 0 && axis_x < 6)
{
mouse_x = get_value(headpose[axis_x] * invert[axis_x],
- last_pos_x,
- last_x,
- axis_x >= 3,
- static_cast<slider_value>(s.sensitivity_x));
+ s.sensitivity_x(),
+ axis_x >= 3);
}
if (axis_y >= 0 && axis_y < 6)
mouse_y = get_value(headpose[axis_y] * invert[axis_y],
- last_pos_y,
- last_y,
- axis_y >= 3,
- static_cast<slider_value>(s.sensitivity_y));
+ s.sensitivity_y(),
+ axis_y >= 3);
MOUSEINPUT mi;
- mi.dx = mouse_x;
- mi.dy = mouse_y;
+ mi.dx = get_delta(mouse_x, last_x);
+ mi.dy = get_delta(mouse_y, last_y);
mi.mouseData = 0;
mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_MOVE_NOCOALESCE;
mi.time = 0;
@@ -64,45 +60,28 @@ QString mouse::game_name()
return "Mouse tracker";
}
-double mouse::get_rotation(double val, double last_pos)
+int mouse::get_delta(int val, int prev)
{
- using std::fmod;
- using std::fabs;
- using std::copysign;
- using std::min;
-
- const double x_1 = val - last_pos;
-
- if (x_1 > 180)
- return 360 - x_1;
- else if (x_1 < -180)
- return 360 + x_1;
- else
- return x_1;
+ using std::abs;
+
+ const int a = abs(val - prev), b = abs(val + prev), c = abs(val);
+ if (c < a && c < b)
+ return val;
+ if (b < a && b < c)
+ return val + prev;
+ return val - prev;
}
-int mouse::get_value(double val, double& last_pos, int& last_px, bool is_rotation, double sensitivity)
+int mouse::get_value(double val, double sensitivity, bool is_rotation)
{
static constexpr double c = 1e-1;
+ static constexpr double sgn[] = { 1e-2, 1 };
- if (is_rotation)
- {
- const double rot_delta = get_rotation(val, last_pos);
-
- last_pos = val;
- last_px = int(rot_delta * c * sensitivity);
- }
- else
- {
- val -= last_pos;
-
- last_pos = val;
- last_px = int(val * c * sensitivity / 100 - last_px);
- }
-
- return last_px;
+ return iround(val * c * sensitivity * sgn[unsigned(is_rotation)]);
}
+mouse::mouse() : last_x(0), last_y(0) {}
+
bool mouse::correct()
{
return true;
diff --git a/proto-mouse/ftnoir_protocol_mouse.h b/proto-mouse/ftnoir_protocol_mouse.h
index e920c047..4f0bf5a7 100644
--- a/proto-mouse/ftnoir_protocol_mouse.h
+++ b/proto-mouse/ftnoir_protocol_mouse.h
@@ -20,24 +20,23 @@ struct settings : opts {
opts("mouse-proto"),
Mouse_X(b, "mouse-x", 0),
Mouse_Y(b, "mouse-y", 0),
- sensitivity_x(b, "mouse-sensitivity-x", slider_value(200, 100, 500)),
- sensitivity_y(b, "mouse-sensitivity-y", slider_value(200, 100, 500))
+ sensitivity_x(b, "mouse-sensitivity-x", slider_value(200, 25, 500)),
+ sensitivity_y(b, "mouse-sensitivity-y", slider_value(200, 25, 500))
{}
};
class mouse : public IProtocol
{
public:
- mouse() : last_pos_x(0), last_pos_y(0), last_x(0), last_y(0) {}
+ mouse();
bool correct() override;
void pose( const double *headpose) override;
QString game_name() override;
- double last_pos_x, last_pos_y;
int last_x, last_y;
private:
- static double get_rotation(double val, double last_val);
- static int get_value(double val, double& last_pos, int& last_px, bool is_rotation, double sensitivity);
+ static int get_delta(int val, int prev);
+ static int get_value(double val, double sensitivity, bool is_rotation);
struct settings s;
};