Joystick movement help please

in builder, right? So shouldn’t go back to centre when released, but the position should be updated whenever it’s pulled towards a certain direction.
Guess you can

  1. declare a few variables in advance, say
    joystickposition = [0,0]; to keep track of the position
    joythreshold = 0.2; minimal movement at the joystick required
    joystepsize = 0.01; to set a default pace
    joyspeedfactorY = 1; if you need to increase stepsize relative on x/y
    joyspeedfactorX = 1;

create an image that serves as cursor, update position each frame based on joystickposition
add joystick component, turn off end of trial upon press

  1. (abs values instead of <> ± might’ve made sense here :wink: )
    if (joystick.device.getX() < -joythreshold):
    joystickposition[0] = joystickposition[0] - (joystepsize * joyspeedfactorX);
    if (-joystick.device.getY() < -joythreshold):
    joystickposition[1] = joystickposition[1] - (joystepsize * joyspeedfactorY);
    if (joystick.device.getX() > joythreshold):
    joystickposition[0] = joystickposition[0] + (joystepsize * joyspeedfactorX);
    if (-joystick.device.getY() > joythreshold):
    joystickposition[1] = joystickposition[1] + (joystepsize * joyspeedfactorY);

You also want to make sure the movement can’t go out of bounds / off the screen, e.g.
if (joystickposition[1] > 0.5):
joystickposition[1] = 0.5;
if (joystickposition[1] < - 0.5):
joystickposition[1] = -0.5;
if (joystickposition[0] > 0.85):
joystickposition[0] = 0.85;
if (joystickposition[0] < - 0.85):
joystickposition[0] = -0.85;

  1. Check whether you’re in some rectangle
    if (abs(joystickposition[0] - Region1Position[0]) < Region1Size[0]/2):
    if (abs(joystickposition[1] - Region1Position[1]) < Region1Size[1]/2):
    joystickposition = [0,0];
    yadidadida;

Finally, I don’t know whether the screen refresh rate offers enough debouncing on this, but you can always add a timer or only update the values every n frames.

Not exactly elegant ;), but hope it helps.

Don’t know whether it’s easer or not for nh primates (do they have a prior about pushing left - moving left etcet?), but you can also get rid of the joystick using some cheap big arcade buttons and a board to emulate a keyboard.

1 Like