Applications often need to ignore small movements in relative mode to handle various real-world issues like sensor noise, user tremors, or imprecise input devices. Here's a comprehensive approach to handling small movements:
#include <SDL.h>
#include <cmath>
#include "Window.h"
class MotionFilter {
public:
struct FilteredMotion {
float x;
float y;
bool SignificantMotion;
};
FilteredMotion Filter(int RawX, int RawY) {
// Convert to floating point for
// precise calculations
float X{static_cast<float>(RawX)};
float Y{static_cast<float>(RawY)};
// Calculate magnitude of movement
float Magnitude{std::sqrt(X * X + Y * Y)};
// Check if movement exceeds dead zone
bool IsSignificant{Magnitude > DeadZone};
if (!IsSignificant) {
return {0.0f, 0.0f, false};
}
// Apply scaling to significant movements
float Scale{
(Magnitude - DeadZone) / Magnitude};
return {X * Scale, Y * Scale, true};
}
void SetDeadZone(float NewDeadZone) {
DeadZone = NewDeadZone;
}
private:
float DeadZone{2.0f};
};
class Camera {
public:
void Update(SDL_MouseMotionEvent& E) {
auto Filtered{
Filter.Filter(E.xrel, E.yrel)};
if (Filtered.SignificantMotion) {
Rotation.x += Filtered.x * Sensitivity;
Rotation.y += Filtered.y * Sensitivity;
std::cout << "\nRaw input: "
<< E.xrel << ", " << E.yrel
<< "\nFiltered: " << Filtered.x << ", "
<< Filtered.y
<< "\nRotation: " << Rotation.x << ", "
<< Rotation.y;
}
}
private:
MotionFilter Filter;
SDL_FPoint Rotation{0.0f, 0.0f};
float Sensitivity{0.1f};
};
int main() {
SDL_Init(SDL_INIT_VIDEO);
Window GameWindow;
Camera Cam;
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_Event E;
while (true) {
while (SDL_PollEvent(&E)) {
if (E.type == SDL_MOUSEMOTION) {
Cam.Update(E.motion);
} else if (E.type == SDL_QUIT) {
SDL_Quit();
return 0;
}
}
GameWindow.Update();
GameWindow.Render();
}
}
Key reasons for filtering small movements:
The dead zone approach shown above is just one method. Other techniques include:
Answers to questions are automatically generated and may not have been reviewed.
Learn how to restrict cursor movement to a window whilst capturing mouse motion continuously.