Skip to content

Commit

Permalink
feat: ✨ Add fisheye joystick modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Nov 2, 2024
1 parent e7f7495 commit 297295a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/gamepad/joystick_modifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ class ExpoCurve : public AbstractTransformation {
float m_y_curve;
};

class Fisheye : public AbstractTransformation {
public:
Fisheye(float radius)
: m_radius(radius) {}

std::pair<float, float> get_value(std::pair<float, float>) override;
private:
float m_radius;
};

class Transformation final {
friend class TransformationBuilder;
public:
Expand Down
16 changes: 16 additions & 0 deletions src/gamepad/joystick_modifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ std::pair<float, float> ExpoCurve::get_value(std::pair<float, float> value) {
return {x, y};
}

std::pair<float, float> Fisheye::get_value(std::pair<float, float> value) {
float x = value.first;
float y = value.second;
float x_abs = abs(x);
float y_abs = abs(y);
float j = std::sqrt(m_radius * m_radius - 127 * 127);
if (x_abs >= j && y_abs >= j) {
float theta = std::atan2(y_abs, x_abs);
x_abs *= std::acos(abs(std::remainder(theta, 90)));
y_abs *= std::acos(abs(std::remainder(theta, 90)));
}
x = std::copysign(std::min(127.0f, x_abs), x);
y = std::copysign(std::min(127.0f, y_abs), y);
return {x, y};
}

std::pair<float, float> Transformation::get_value(std::pair<float, float> value) {
std::pair<float, float> ret_value {};
return std::accumulate(m_all_transforms.begin(), m_all_transforms.end(), ret_value,
Expand Down

0 comments on commit 297295a

Please sign in to comment.