Author Topic: Simple Joystick Detection and Interaction  (Read 114 times)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 2352
    • Steve’s QB64 Archive Forum
Simple Joystick Detection and Interaction
« on: February 07, 2020, 01:13:44 PM »
A small little demo to help highlight how one would work with the joystick in QB64:

Code: QB64: [Select]
  1. D = _DEVICES 'Find the number of devices on someone's system
  2. '1 is the keyboard
  3. '2 is the mouse
  4. '3 is the joystick
  5. 'unless someone has a strange setup with multiple mice/keyboards/ect...
  6. 'In that case, you can use _DEVICE$(i) to look for "KEYBOARD", "MOUSE", "JOYSTICK", if necessary.
  7. 'I've never actually found it necessary, but I figure it's worth mentioning, just in case...
  10. DIM Button(_LASTBUTTON(3)) ' number of buttons on the joystick
  11. DIM Axis(_LASTAXIS(3)) 'number of axis on the joystick
  15.     DO
  17.         'This following little segment of code gets the joystick status for us
  18.         'The reason this is inside a DO...LOOP structure as I've created it,
  19.         'is so that my joystick's axis won't generate any form of lag for
  20.         'my program as I scroll them around to generate positive/negative values.
  22.         IF _DEVICEINPUT = 3 THEN 'this says we only care about joystick input values
  23.             FOR i = 1 TO _LASTBUTTON(3) 'this is a loop to check all the buttons
  24.                 IF _BUTTONCHANGE(i) THEN Button(i) = NOT Button(i) 'and this changes my button array to indicate if a button is up or down currently.
  25.             NEXT
  26.             FOR i = 1 TO _LASTAXIS(3) 'this loop checks all my axis
  27.                 'I like to give a little "jiggle" resistance to my controls, as I have an old joystick
  28.                 'which is prone to always give minute values and never really center on true 0.
  29.                 'A value of 1 means my axis is pushed fully in one direction.
  30.                 'A value greater than 0.1 means it's been partially pushed in a direction (such as at a 45 degree diagional angle).
  31.                 'A value of less than 0.1 means we count it as being centered. (As if it was 0.)
  32.                 IF ABS(_AXIS(i)) <= 1 AND ABS(_AXIS(i)) >= .1 THEN Axis(i) = _AXIS(i) ELSE Axis(i) = 0
  33.             NEXT
  34.         ELSE EXIT DO
  35.         END IF
  36.     LOOP
  38.     'And below here is just the simple display routine which displays our values.
  39.     'If this was for a game, I'd choose something like Axis(1) = -1 for a left arrow style input,
  40.     'Axis(1) = 1 for a right arrow style input, rather than just using _KEYHIT or INKEY$.
  43.     CLS
  44.     FOR i = 1 TO _LASTBUTTON(3) 'A loop for each button
  45.         PRINT "BUTTON "; i; ": "; Button(i) 'display their status to the screen
  46.     NEXT
  47.     FOR i = 1 TO _LASTAXIS(3) 'A loop for each axis
  48.         PRINT "Axis "; i; ": "; Axis(i) 'display their status to the screen
  49.     NEXT
  51.     _LIMIT 30
  52. LOOP UNTIL _KEYHIT = 27 'ESCAPE to quit
  54. SYSTEM 'And that's all it is to it! — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 1713
  • Cuz I sez so, varmint!
Re: Simple Joystick Detection and Interaction
« Reply #1 on: February 07, 2020, 01:36:51 PM »
Yes, that's what I was getting at in Terry's asteroids thread. I would think every gamer here would want to add something like this. I mean imagine if Atari came out with a keyboard plugin with just the wasdx and the Enter key on it. Who the hell would pay extra for that control option?


Offline johnno56

  • Forum Resident
  • Posts: 621
  • Live long and prosper.
Re: Simple Joystick Detection and Interaction
« Reply #2 on: February 16, 2020, 07:37:22 PM »
Plugged in my old USB Logitech Extreme3D Pro joystick, after blowing off all the dust... lol, and your programme  detected all 12 buttons and 6 axis correctly... I wonder which QB64 game could make use of my old joystick? Any guesses? (I don't want to put it back in storage... unless...)

Logic is the beginning of wisdom.