| View previous topic :: View next topic |
| Author |
Message |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Mon Sep 07, 2009 10:36 am Post subject: 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. _________________ If not actually, then potentially. |
|
| Back to top |
|
 |
cheriff Regular
Joined: 23 Jun 2004 Posts: 262 Location: Sydney.au
|
Posted: Mon Sep 07, 2009 11:36 am Post subject: |
|
|
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: | Code: | 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 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 :) _________________ Damn, I need a decent signature! |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Mon Sep 07, 2009 12:16 pm Post subject: |
|
|
Thanks, I will give it a shot :)
I'm after 360 degree circle yes. _________________ If not actually, then potentially. |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Mon Sep 07, 2009 6:22 pm Post subject: |
|
|
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
| Code: |
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. |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Mon Sep 07, 2009 6:37 pm Post subject: |
|
|
This is gonna crash if xxx is 0. Why not use atan2?
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Mon Sep 07, 2009 6:45 pm Post subject: |
|
|
| Code: |
sceCtrlReadBufferPositive(&pad, 1);
xxx= pad.Lx - 130;
yyy= pad.Ly - 130;
rotation = atan2 (xxx,yyy) / (3.141592/180);
|
I've tried this one, but it crashes instantly.
EDIT.. no it works ;)
I just forgot
sceCtrlSetSamplingMode(1);
sceCtrlPeekBufferPositive(&pad, 1); _________________ If not actually, then potentially. |
|
| Back to top |
|
 |
cheriff Regular
Joined: 23 Jun 2004 Posts: 262 Location: Sydney.au
|
Posted: Tue Sep 08, 2009 8:03 am Post subject: |
|
|
| Jim wrote: | This is gonna crash if xxx is 0. Why not use atan2?
Jim | Nice, I didnt know about atan2. you learn something new every day Thanks! _________________ Damn, I need a decent signature! |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Tue Sep 08, 2009 8:30 am Post subject: |
|
|
The one I posted outputs with the y axis upside down.
Here's one I corrected.. probably a sloppy way to fix:
| Code: |
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);}
|
Art. _________________ If not actually, then potentially. |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Wed Sep 09, 2009 7:03 am Post subject: |
|
|
| Code: |
jyyy= pad.Ly - 130;
if (jyyy<0) {jyyy = fabs(jyyy);} else {jyyy = 0 - jyyy;}
|
Instead of this, write
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Wed Sep 09, 2009 8:02 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
Torch

Joined: 28 May 2008 Posts: 842
|
Posted: Wed Sep 09, 2009 2:13 pm Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
|