Convert analogue nub X,Y to angle?
Convert analogue nub X,Y to angle?
Hi Guys,
About a year ago I found code to output an angle in degrees
from the analogue controller input, and now can't seem to find it when I'd like to use it.
Anyone know where this is? It may not have been on this forum...
Cheers, Art.
About a year ago I found code to output an angle in degrees
from the analogue controller input, and now can't seem to find it when I'd like to use it.
Anyone know where this is? It may not have been on this forum...
Cheers, Art.
If not actually, then potentially.
May not be what you saw before, but if I cast my mind way back and remember a little trig, and assuming (0,0) is when the nub is centered:I used python since its easier to tweak as I was getting it right, but should be fairly simple to derive a C version.
I *think* it's right .. seems OK for the handful of values I tested. If you want the value in [-180,180) rather that [0,360) you can tweak the fixups as needed.
Someone may have a nicer way, however :)
Code: Select all
import math
def foo(x,y):
rad = math.atan(y/x)
degrees = rad * (360 / (2*math.pi))
magnitude = math.sqrt(x*x + y*y)
#fixup for 2nd, 3rd and 4th quadrants
if (x<0) and (y<0):
degrees = degrees + 180
elif (x<0):
degrees = degrees + 180
elif (y<0):
degrees +=360
return (magnitude, degrees)
I *think* it's right .. seems OK for the handful of values I tested. If you want the value in [-180,180) rather that [0,360) you can tweak the fixups as needed.
Someone may have a nicer way, however :)
Damn, I need a decent signature!
I tried this, point the stick straight up, and it does output zero.
as I turn the nub clockwise, the degrees var goes up to about 90 degrees, but by that time, the output should be about 260 degrees.
It also crashes if it gets to just under 90 degrees.
by this time the nub is almost pointing left
as I turn the nub clockwise, the degrees var goes up to about 90 degrees, but by that time, the output should be about 260 degrees.
It also crashes if it gets to just under 90 degrees.
by this time the nub is almost pointing left
Code: Select all
sceCtrlSetSamplingMode(1);
sceCtrlPeekBufferPositive(&pad, 1);
yyy = pad.Ly;
xxx = pad.Lx;
rad = atan(yyy/xxx);
degrees = rad * (360 / (2*3.141592));
if (xxx<0 && yyy<0) {
degrees = degrees + 180;
} else if (xxx<0) {
degrees = degrees + 180;
} else if (yyy<0) {
degrees +=360;
}
If not actually, then potentially.
Code: Select all
sceCtrlReadBufferPositive(&pad, 1);
xxx= pad.Lx - 130;
yyy= pad.Ly - 130;
rotation = atan2 (xxx,yyy) / (3.141592/180);
EDIT.. no it works ;)
I just forgot
sceCtrlSetSamplingMode(1);
sceCtrlPeekBufferPositive(&pad, 1);
If not actually, then potentially.
The one I posted outputs with the y axis upside down.
Here's one I corrected.. probably a sloppy way to fix:
Art.
Here's one I corrected.. probably a sloppy way to fix:
Code: Select all
float rotation;
float precalc;
precalc = 3.141592/180;
SceCtrlData pad;
sceCtrlSetSamplingMode(1);
sceCtrlPeekBufferPositive(&pad, 1);
jxxx= pad.Lx - 130;
jyyy= pad.Ly - 130;
if (jyyy<0) {jyyy = fabs(jyyy);} else {jyyy = 0 - jyyy;}
rotation = atan2 (jxxx,jyyy) / (precalc);
if (rotation<0) {rotation = 360 - fabs(rotation);}
If not actually, then potentially.
Code: Select all
jyyy= pad.Ly - 130;
if (jyyy<0) {jyyy = fabs(jyyy);} else {jyyy = 0 - jyyy;}
Code: Select all
jyyy = 130-pad.Ly;
The PSP stick goes from 0 to 255, with the "center" being (theoretically) 128. So 128,128 is the nominal center when the stick isn't being pushed. This varies GREATLY by the PSP and how old it is and how much the user has used the stick. I've found a slop of plus or minus 32 is barely enough to cover the majority of PSPs. So you should really only respond to the stick if it's less than 96 or greater than 160.
I could give you something for this, but its considerably larger than what you're doing above. I don't really know how to simplify analytical geometrical calculations other than following the procedure to find the angle between the lines (128,128),(128,0) and (128,128),(analog value). Then adjust it for the 4 quadrants.