x


Constrain touch joystick around a point

Right now, when you touch the joystick, that touch is 'latched' to the joystick, and it will follow your finger around until you release the joystick, thereby 'unlatching' the touch and allowing the stick to return to it's 'home' position. The last two images are what I am trying to achieve; I think what I need to so is use the location of the joystick and the orient to calculate the distance between the two and force the distance to max out at a specified length regardless of the position of my touch, but I am not sure how to calculate that. Am I going down the right road here?

var joyOrient : GameObject;
var joyButton : GUITexture;
var joyBorder : GUITexture;
var xValue : float;
var yValue : float;

private var savedPosition : Vector3;
private var orientPosition : Vector3;
private var joyTouchID : int;
private var latched : boolean;

function Awake () {

    orientPosition = joyOrient.transform.position; //set orient to location of joyOrient object

} //End Awake

function Update () {

for (var event : Touch in Input.touches) { //check for touches

var JoyHitTest = (joyButton.HitTest(event.position)); //did you touch the joystick?

    if (event.phase == TouchPhase.Began || event.phase == TouchPhase.Moved) { //if phase is Began or Moved
        if(JoyHitTest){ //and if you touched the joystick thumb

         latched = true; //touch is latched to stick

       } //End if

    } //End if

    if (event.phase == TouchPhase.Ended && event.fingerId == joyTouchID){ //if the touch ends, and it is the touch used to latch the joystick   

latched = false;

    } //End If

    if (latched){ //this is the meat, and where I am sure I need to make my edits to constrain the distance the thumb will move

       joyTouchID = event.fingerId; //get ID of latched touch
       joyButton.transform.position.x = (event.position.x/1024); //set position of thumb to finger location on iPad screen
       joyButton.transform.position.y = (event.position.y/768); //must change this so resolution is variable driven 
       joyButton.transform.position.z = 0; //z axis is always zero since we only care about 2D movement here
       savedPosition = joyButton.transform.position; //save current location into a Vec3
       xValue = orientPosition.x-savedPosition.x; 
       yValue = orientPosition.y-savedPosition.y; //calculate X/Y value. Needs work, to include normalization of output

    } //End If

    else if (!latched){

       joyButton.transform.position = orientPosition; //return joystick thumb to the orient location

    }//End If

} //End for

} //End Update
more ▼

asked Oct 03 '11 at 07:32 PM

noradninja gravatar image

noradninja
790 17 23 41

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1969
x583
x216
x212
x49

asked: Oct 03 '11 at 07:32 PM

Seen: 748 times

Last Updated: Oct 03 '11 at 07:32 PM