Tutorial :Logarithmic Mouse Movement


Alright, this is probably gonna be a pretty simple question to answer. I haven't had a math class dealing with logarithms in a few years, so I apologize. So I have a USB Controller that I'm using to control the mouse on the screen with the left joystick. Now how this works right now is the controller returns a double between 0.00 and 1.00 depending on how far the push the joystick in the direction (0.00 center, 1.00 pushed all the way over). I'm using this to adjust the speed of the mouse movement by multiplying the returned value by a given speed (returned double * speed). This gives me a linear speed. But for the purpose of accuracy of the mouse and clicking things on screen, I'd like it to be more logarithmic, so as it's really slow when barely pushing, and then the speed increases logarithmically as you move the joystick farther. That way you can get good speed for moving across the screen, while also having good sensitivity when moving it slowly. So I just need help with the formula, as I'm sure it's pretty simple. Also, I'm working in Java. Right now my formula is:

double value (value given by controller)  int speed = 25;  value += value * speed;  

I then use this to move the mouse. Thanks, Brayden


I presume you meant exponential. An exponential function looks like http://hotmath.com/images/gt/lessons/genericalg1/exponential_graph.gif: the more value is, the larger the derivative (the more speed will increase for the same change in value).

double value = ...;  int base = 25;  value = java.lang.Math.pow(base, value);  

Not sure if java.lang.Math is necessary in its full form or whether you need to import java.lang.Math first. I'm not a Java programmer.


I agree with MvanGeest, I think you want an exponential formula. That way its small with little distance, and very big with larger distances.

I'm not sure what mouse speed values are fast or slow, but you could do something like

double value (value given by controller);  int speed (maximum speed value);  value = Math.pow(speed, value);  

You could also make the value something like 2*(whatever the controller gives you) to make a wider range of speeds.


Something like:

f(value) = value * MAXSPEED * Math.Pow (base, 1 - value)    f(0) = 0 // no movement  f(1) = MAXSPEED // maximum movement when joystick is full throttle  

All values should be covered in the range. Base in this case can be any value greater than 1.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »