views:

87

answers:

3

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

+1  A: 

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.

MvanGeest
does not give a zero movement for when the joystick is centered!
cristobalito
Agreed, maybe that needs to be checked separately.
MvanGeest
This was exactly what I needed actually, thank you very much. I guess I didn't even realize what I was asking for. Haha. But again thank you :).
Brayden
A: 

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.

Eric
or 4*, etc. whatever number will give you a big enough range.
Eric
this causes some minor movement when value is 0 - x^0 = 1 not 0
cristobalito
true. just make it value = Math.pow(speed, value) - 1@cristobalito: what do you mean in your answer? Can't Math.pow only take 2 arguments?
Eric
I also used your idea of having the value*2, gave me a good range of speeds. Thank you very much!
Brayden
@Eric: Not familiar with Java but essentially you can use any base.
cristobalito
A: 

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.

cristobalito