Need help with a joystick script

Hi there,
I’m trying to make a joystick script that measures left right up and down. At the moment I am measuring the pointer’s position over a square area where x = 0 to 1 from left to right and y = 0 to 1 from botton to top.
This is working really well for placing graphics at the right spot and most things but I cant find a good way to measure when it is in each of the zones based on that x,y data.

so in the meantime I stuck with a patheticly lame script (please don’t mock me for it :slight_smile: ).

function RightButtonPosCheck(){
	if (RightButtonPosVector2.y >= .75)RightButtonPosition = 1;//Up	
	if (RightButtonPosVector2.y <= .25)RightButtonPosition = 3;//Down
	if (RightButtonPosVector2.x <= .25)RightButtonPosition = 4;//Left
	if (RightButtonPosVector2.x >= .75)RightButtonPosition = 2;//Right
}

Any way it works but it’s pretty crappy, does anyone have any good suggestions on how to detect when the pointer is in those zones. I just want it to feed bak a 1-4 value for me…

Thanks
Pete

  1. measure distance to center → radius. if larger than radius of the inner circle, you know you are in the zone

  2. use the angle of the direction center → touch in relation to “right” to define the sector ( Mathf.Sign(yPos - centerY)*acos( xPos - centerX), which gives you angle from 0 to ± 180 deg)

Woah, that was fast!
Cool thanks Dremora, That Sounds like the way to do it!
:smile:

Hey Dreamora,
Just trying to get this joystick script working and the distance part works fine but I’m having a little trouble getting the angle,

This is what I have so far -

(Mathf.Sign(HitPos.y - .5)) * (Mathf.Acos(HitPos.x - .5))

I’m not that great with this type of thing but I’m not sure how that would end up giving me a usable angle.

If you’ve got a sec could you point me in the right direction, sorry if I’m missing something obvious.

Pete

Acos() only returns an angle in the range [0, pi], as that’s all that can be determined from the cosine alone. The sign of the y coordinate is then used to determine whether the actual angle falls in the range [0, pi] or (-pi, 0).

However, I would just use Atan2() instead. That way you don’t have to worry about determining the sign, and you also don’t have to worry about the input falling outside the range [-1, 1] due to numerical imprecision (which may not be an issue here, but with Atan2() you’ll be safe either way).

Thanks!
I’ve put this formula in and it seems to be working well

RightJoysickAngle = (Mathf.Atan2((Pos.y - .5), (Pos.x - .5))* Mathf.Rad2Deg);
   if (RightJoysickAngle >= 45  RightJoysickAngle < 135) print ("1");
   if (RightJoysickAngle >= -45  RightJoysickAngle < 45) print ("2");
   if (RightJoysickAngle >= -135  RightJoysickAngle < -45) print ("3");
   if (RightJoysickAngle >= -180  RightJoysickAngle < -135 || RightJoysickAngle >= 135  RightJoysickAngle < 180) print ("4");

Something like that should work fine, but if you’re interested in cleaning it up a bit, here’s a little trick (C#, untested):

int ToIndex(float x, float y)
{
    float angle = (Mathf.Atan2(y, x) * Mathf.Rad2Deg) + 45f;
    if (angle < 0f) {
        angle += 360f;
    }
    return (int)(angle / 90f);
}

I use this method in my own code, although I may or may not have reproduced it here correctly (I think I got it right though).

ahh thanks man! That makes it heaps cleaner!