A Joystick Primer 
Author Message
 A Joystick Primer

Here's an impromptu primer on how to program the joystick in BASIC,
seeing there are several threads on the subject already:

Note that this is intended for QBASIC, Visual Basic, QuickBASIC,
powerbasic, and other recent versions of BASIC.  Earlier versions may
not include the functions mentioned here.

There are two major functions required to handle the joystick, STICK and
STRIG.  STICK is concerned with the position of the joystick, while
STRIG is used for trigger buttons.  Both are configured for up to two
joysticks.

The STICK function is best understood with a simple example:

---------- BEGIN CODE ----------
CLS
DO
  LOCATE 1, 1 ' Return to top of screen
  PRINT "X-coordinate of joystick A:"; STICK(0); "  "
  PRINT "Y-coordinate of joystick A:"; STICK(1); "  "
  PRINT "X-coordinate of joystick B:"; STICK(2); "  "
  PRINT "Y-coordinate of joystick B:"; STICK(3); "  "
LOOP UNTIL INKEY$ <> "" ' Loop until any key is hit
---------- END CODE ----------

Only thing to note here is that STICK(0) must be used before STICK(1),
STICK(2), or STICK(3) can be used, because STICK(0) not only returns the
X-coordinate of joystick A, but also records the other joystick
coordinates.  This shouldn't be a problem; just always use STICK(0)
first.

These coordinates range from 1 to 200 in both directions.

STRIG requires some explanation.  STRIG returns -1 if the specified
condition is true, 0 otherwise.  To use it, just do something like:

n = STRIG(0)

The parameter for STRIG (0 in the case above) can be from 0 to 7, and
correspond to the following:

0  Lower trigger on joystick A was pressed since last STRIG(0).
1  Lower trigger on joystick A is currently being pressed.
2  Lower trigger on joystick B was pressed since last STRIG(2).
3  Lower trigger on joystick B is currently being pressed.
4  Upper trigger on joystick A was pressed since last STRIG(4).
5  Upper trigger on joystick A is currently being pressed.
6  Upper trigger on joystick B was pressed since last STRIG(6).
7  Upper trigger on joystick B is currently being pressed.

You may need to execute the statment STRIG ON to enable STRIG in some
early versions of BASIC.

Now, on a more practical side, there is a problem with the STICK
function.  Or, really, there is a problem with joysticks in general.
Namely, joysticks can slide around a bit even when supposedly "centered"
at the same position.  The best way to rememdy this is a calibration
test before each game.

Calibration works this way: require the user to center hir joystick and
hit a button.  When that is done, record the josytick's X and Y
coordinates.  When you later want to test if the joystick is pushed
forward, test if it's some distance away from these pre-recorded center
coordiantes.  Thus, test "IF JoystickY < JoystickCenterX - 10" or
something like that.  This will eliminate any small wanderings of the
joystick while centered.

Also, some joysticks can't go all the way to 1 and 200 in the X and Y
plane.  You might want to record the X and Y coordinates when the
joystick is pushed to the upper-left and lower-right, then compensate
based on these positions.

That's it!  Copy this, give it away, add to it, whatever, just give
credit where it's due.

Brent P. Newhall
--------------------------------------------------------------
"Good laws make it easier to do right and harder to do wrong."
-- William E. Gladstone



Fri, 14 May 1999 03:00:00 GMT  
 A Joystick Primer


Quote:

>Here's an impromptu primer on how to program the joystick in BASIC,
>seeing there are several threads on the subject already:
<snip>
>That's it!  Copy this, give it away, add to it, whatever, just give
>credit where it's due.

Just as a matter of interest, credit who exactly ?

TTfn

Craig Wright___

..the truth is out there.......but not in this newsgroup...



Sat, 15 May 1999 03:00:00 GMT  
 A Joystick Primer

Quote:



> <snip>
> >That's it!  Copy this, give it away, add to it, whatever, just give
> >credit where it's due.

> Just as a matter of interest, credit who exactly ?

Me, or anyone else who might add to the Primer. :)  Not to be vain or
anything; it's just there to cover in case the Primer worms its way out
into the real world, which these things tend to do every so often.

Brent P. Newhall
--------------------------------------------------------------
"Good laws make it easier to do right and harder to do wrong."
-- William E. Gladstone



Sun, 16 May 1999 03:00:00 GMT  
 A Joystick Primer

Quote:

> Here's an impromptu primer on how to program the joystick in BASIC,
> seeing there are several threads on the subject already:

> These coordinates range from 1 to 200 in both directions.

Uhm, I don't know if BASIC limits the coordinates returned by STICK(),
but actually this number depends on the computer you are running it on.
The faster the computer, the bigger the number. If I remember correctly
it represents the number of cycles it takes for a value (1) written to
the joystick port to settle back to zero. So, the faster the computer,
the more cycles will pass. In BASIC this values should stay under 400
(because it's slow compared to other languages). I have seen some
ASM/Pascal functions which display over 1000 cycles on my 486 DX4/100.

Quote:
> Also, some joysticks can't go all the way to 1 and 200 in the X and Y
> plane.  You might want to record the X and Y coordinates when the
> joystick is pushed to the upper-left and lower-right, then compensate
> based on these positions.

That would be because of the speed of the computer system. To test this
just try calibrating your joystick and press the slow-down button
(TURBO) on your computer's case (if you have one). If your threshold is
not big enough your joystick will decalibrate and move to the upper-left
corner.

Just trying to fill in some gaps other than that your primer was pretty
good,

Marco Koegler

http://www.geocities.com/SiliconValley/8690  <-- CD-ROM programming



Sun, 16 May 1999 03:00:00 GMT  
 A Joystick Primer

Quote:


> > Here's an impromptu primer on how to program the joystick in BASIC,
> > seeing there are several threads on the subject already:

> > These coordinates range from 1 to 200 in both directions.

> Uhm, I don't know if BASIC limits the coordinates returned by STICK(),
> but actually this number depends on the computer you are running it
> on. The faster the computer, the bigger the number. If I remember
> correctly
> it represents the number of cycles it takes for a value (1) written to
> the joystick port to settle back to zero. So, the faster the computer,
> the more cycles will pass. In BASIC this values should stay under 400
> (because it's slow compared to other languages). I have seen some
> ASM/PASCAL functions which display over 1000 cycles on my 486 DX4/100.

Mea culpa!  I was using a reference which was obviously spotty; I just
tried STICK() in QBASIC and got numbers from 1 to 250 on both axes.  You
are correct.  My apologies.

Quote:
> > Also, some joysticks can't go all the way to 1 and 200 in the X and
> > Y plane.  You might want to record the X and Y coordinates when the
> > joystick is pushed to the upper-left and lower-right, then
> > compensate based on these positions.

> That would be because of the speed of the computer system. To test
> this just try calibrating your joystick and press the slow-down button
> (TURBO) on your computer's case (if you have one). If your threshold
> is not big enough your joystick will decalibrate and move to the
> upper-left corner.

Well, problem is, most people don't want to say in their programs,
"Please calibrate your joystick then turn off your TURBO."  And I
described calibration somewhere in the primer.

I'm sure that works, though.

Quote:
> Just trying to fill in some gaps other than that your primer was
> pretty good,

Thanks both for your encouragement and the tips.

Brent P. Newhall
--------------------------------------------------------------
"Good laws make it easier to do right and harder to do wrong."
-- William E. Gladstone



Tue, 18 May 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Joystick output (Joystick Programming in vb)

2. ADO Primer

3. MDI application: Primer

4. Primer on Threading

5. RichTextBox primer?

6. Visual Basic Primers

7. Visual Basic / SQL Server Primer

8. TCP/IP primer, pointers, FAQs, How to-s?

9. Automatic Webbased updates - pointer to code/primer

10. API primer?

11. Finding VB Primer edition

12. Visual Basic Primer anywhere?

 

 
Powered by phpBB® Forum Software