Welcome! Log In Create A New Profile

Advanced

Best practise to move TaskObject around screen with a joystick?

Posted by jamesbutler01 
Best practise to move TaskObject around screen with a joystick?
December 05, 2019 04:34PM
I wish to make a task where when they hold a particular direction on the joystick, a TaskObject (image) on the screen will move in the direction the joystick is held. So hold left --> the cue tracks left while held --> hold up --> the cue now tracks up for as long as joystick is held (resulting in a path like this: __| )

Two ways I see to do this are:
1) Create lots of TaskObjects in a circumference around the joystick's resting position for each angle and then continually call MultiTarget with a low HoldTime and respond accordingly. This has the disadvantage of requiring me to bin the screen into different angles of a particular width and doesn't give me true 360° range of motion
2) Continually sample the tracker's XY data (joy_.XYData) and transform the xy position into an angle myself. I'm not clear on the best practise to do this and whether this is appropriate? Could I just use a continual while loop with a break clause at some point?

Ideally, I'd like to get smooth movement of the cue with no juddering

Thanks!
Re: Best practise to move TaskObject around screen with a joystick?
December 05, 2019 08:07PM
Anything is possible, but can you explain more about what kind of movement you want to see? If you show an image on the position of the joystick cursor (which you can do with the Joystick Cursor menu and the showcursor function), it will move just as the joystick is moving, so won't go straight left (or up) nor look like smooth movement.
Re: Best practise to move TaskObject around screen with a joystick?
December 06, 2019 09:49AM
Hey Jaewon

Basically, I want to play the arcade game Snake on MonkeyLogic by using a joystick to move the snake around the screen. The specific behaviour I want is:

Joystick XY | Cue movement
(0, 0) | No movement
(1, 0) | TaskObject continually moves right at 1 cm per 100 ms (or whatever speed I wish)
(-1, 0) | TaskObject continually moves left
(0, 1) | TaskObject continually moves up
(0, -1) | TaskObject continually moves down

where JoystickXY is relative to the joystick position at the origin, such that when no direction is pressed on the joystick, the XY data is (0, 0).

The inbuilt joystick cursor functions wont work for this as when the joystick returns to the origin (i.e. Joystick XY reads (0,0) in this case), this will move the image on the screen back to the origin as well.

Converting the Joystick XY relative to the origin I can do fine, I'm just unsure of the best way to sample joy_.XYData. I guess I should just continually call a MultiTarget after each XY sample and TaskObject translation to update the screen where the WaitTime will be the update rate of the cue movement?
Re: Best practise to move TaskObject around screen with a joystick?
December 06, 2019 05:24PM
There is no point of accessing XYData of a tracker directly in the timing script. The field is automatically filled with new samples by NIMH ML, when an adapter chain including the tracker is executed by run_scene. You need to write your own adapter and build an adapter chain with it and joy_. In the adapter, you can access XYData like obj.Tracker.XYData.
https://monkeylogic.nimh.nih.gov/docs_RuntimeFunctions.html#Trackers

See the brick breaker game in the task\runtime v2\23 brick breaker directory, for example.

The National Institute of Mental Health (NIMH) is part of the National Institutes of Health (NIH), a component of the U.S. Department of Health and Human Services.