Skip to content

Commit

Permalink
improve pointer response to diagonal stick directions
Browse files Browse the repository at this point in the history
  • Loading branch information
rsn8887 committed Sep 9, 2018
1 parent 58d3b02 commit 6b3cfdf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.MD
Expand Up @@ -119,6 +119,10 @@ Mouse + keyboard tested working with the "Jelly Comb Mini Bluetooth Keyboard Wit

CHANGELOG
=====
1.70

- Improve pointer response to diagonal stick directions

1.69

- Improve pointer response to slow finger motion
Expand Down
41 changes: 37 additions & 4 deletions src/od-joy.cpp
Expand Up @@ -338,12 +338,45 @@ void read_joystick(int nr, unsigned int *dir, int *button)
//max movement is mouseScale.
//that way, when in one of the other mouse modes,
//the Y button to change scale still works
const float maxAxis = 32767.0f;
magnitude=sqrt(analogX*analogX+analogY*analogY);
if (magnitude >= deadZone)
if (magnitude > deadZone && deadZone < maxAxis)
{
scalingFactor=1.0f/magnitude*(magnitude-deadZone)/(32769.0f-deadZone);
analogX = analogX * scalingFactor;
analogY = analogY * scalingFactor;
//adjust maximum magnitude
float absAnalogX = fabs(analogX);
float absAnalogY = fabs(analogY);
float maxX;
float maxY;
if (absAnalogX > absAnalogY){
maxX = maxAxis;
maxY = (maxAxis * analogY) / absAnalogX;
}else{
maxX = (maxAxis * analogX) / absAnalogY;
maxY = maxAxis;
}
float maximum = sqrt(maxX * maxX + maxY * maxY);
if (maximum > 1.25f * maxAxis) maximum = 1.25f * maxAxis;
if (maximum < magnitude) maximum = magnitude;

// find scaled axis values with magnitudes between zero and maximum
float scalingFactor = maximum / magnitude * (magnitude - deadZone) / (maximum - deadZone);
analogX = (analogX * scalingFactor);
analogY = (analogY * scalingFactor);

// clamp to ensure results will always lie between 0 and 1.0f
float clampingFactor = 1.0f / maxAxis;
absAnalogX = fabs(analogX);
absAnalogY = fabs(analogY);
if (absAnalogX > maxAxis || absAnalogY > maxAxis){
if (absAnalogX > absAnalogY)
clampingFactor = 1.0f / absAnalogX;
else
clampingFactor = 1.0f / absAnalogY;
}

analogX *= clampingFactor;
analogY *= clampingFactor;

lastmx += (int) (analogX * mouseScale);
lastmy += (int) (analogY * mouseScale);
newmousecounters=1;
Expand Down

0 comments on commit 6b3cfdf

Please sign in to comment.