Board index » delphi » Joystick Help Required

Joystick Help Required

Hi c.l.p.b,

It's been a few months since I was last here, so can I say a quick hello
to you all.

Anyway, down to business.

I need to implement joystick control but I'm having a problem with both
the unit I wrote, and all other joystick units.

With a simple loop like this (pseudo-code)

Repeat
        if Joystick_Left then Write 'L'
        if Joystick_Right then Write 'R'
        if Joystick_Up then Write 'U'
        if Joystick_Down then Write 'D'
Until whatever

I get a constant stream of output, even when the stick is centred. There
seems to be some flucuation as to the values of a stick when centred, so
that quite often it registers as a direction has been pressed. Which
would cause the main item of control e.g main sprite, to be bouncing all
over the place.

Since all the units I've tried have done the same I'm presuming there is
some way around this which needs to be implemented in the main
game/program loop. So, this question is to anyone who has programmed a
game and knows how to detect one press at a time, with nothing done when
the stick is centred.

Can you help me?

This wasn't too easy to put into words, but I hope I've conveyed
everything ok.

Thanks,

Karl
--
Karl Dyson <ka...@feklore.demon.co.uk>
"Who and what I am I hide from the enemy."

 

Re:Joystick Help Required


Quote
Karl Dyson wrote:
> Hi c.l.p.b,

> It's been a few months since I was last here, so can I say a quick hello
> to you all.

> Anyway, down to business.

> I need to implement joystick control but I'm having a problem with both
> the unit I wrote, and all other joystick units.

> With a simple loop like this (pseudo-code)

> Repeat
>         if Joystick_Left then Write 'L'
>         if Joystick_Right then Write 'R'
>         if Joystick_Up then Write 'U'
>         if Joystick_Down then Write 'D'
> Until whatever

> I get a constant stream of output, even when the stick is centred. There
> seems to be some flucuation as to the values of a stick when centred, so
> that quite often it registers as a direction has been pressed. Which
> would cause the main item of control e.g main sprite, to be bouncing all
> over the place.

Okay, couple problems.  First, since you mention jumping, I'll have to assume
this is an analog joystick.  Second, since you don't show any code, I also
have to assume any code you've used wasn't written with any great
understanding of *exactly* how a joystick works.

Joysticks just aren't an exact science.  The hardware is a basically an analog
digital converter.  On a PC, timing and position is determined by signal
timing.  There's always going to be some variance.

Also, you should to calibrate the joystick each time before using it so that
you know the range the joystick will travel on each axis, and where the
approximate center will be.  Typically, the faster the computer, the larger
the range will be.

Quote
> Since all the units I've tried have done the same I'm presuming there is
> some way around this which needs to be implemented in the main
> game/program loop. So, this question is to anyone who has programmed a
> game and knows how to detect one press at a time, with nothing done when
> the stick is centred.

You have to consider the joystick's center with an error of margin.  If you
assume center is once place, sure your little sprite is going to bounce around
all over.

+-----+-----+
|           |
|  +--+--+  |
|  |     |  |
+  +  +  +  +
|  |     |  |
|  +--+--+  |
|           |
+-----+-----+

Think of the joystick as something like this.  The outer box represents the
polar extremes.  The inner box is the response threshold.  Depending on the
joystick's accuracy, you could make the threshold something like 10% to 30%
away from center to register a response.  It's my understanding that the
distance between the low end and the center and between the high end and the
center -- so you may have to scale both sides accordingly.

Quote
> Can you help me?

> This wasn't too easy to put into words, but I hope I've conveyed
> everything ok.

Kind of abstract since I don't know anything about the code you're using, but
hopefully this helps make sense of it a little more.

Quote
> Thanks,

> Karl
> --
> Karl Dyson <ka...@feklore.demon.co.uk>
> "Who and what I am I hide from the enemy."

--
Scott Earnest            | SPAM protection in effect. Remove  |
setech@_ix.netcom.com    | "_" as needed for true addresses.  |
earnests@_homenet.lm.com |    UIN:1136443  EFnet:pale_blue    |
sinykal@_{*word*104}space.org  | URL: http://www.netcom.com/~setech |

Re:Joystick Help Required


In article <34B487BE.A5EE808F@_ix.netcom.com>, Scott Earnest
<setech@_ix.netcom.com> writes

Quote
>> I get a constant stream of output, even when the stick is centred. There
>> seems to be some flucuation as to the values of a stick when centred, so
>> that quite often it registers as a direction has been pressed. Which
>> would cause the main item of control e.g main sprite, to be bouncing all
>> over the place.
>Okay, couple problems.  First, since you mention jumping, I'll have to assume
>this is an analog joystick.  

Nope. The one I'm using isn't. But the program needs to be able to use
both.

Quote
>Second, since you don't show any code, I also
>have to assume any code you've used wasn't written with any great
>understanding of *exactly* how a joystick works.

I know how a joystick works but assume away....

Quote
>Joysticks just aren't an exact science.  The hardware is a basically an analog
>digital converter.  On a PC, timing and position is determined by signal
>timing.  There's always going to be some variance.

True... but game engines have to take that into account - and that,
seems to be where I'm having the problems.

Quote
>Also, you should to calibrate the joystick each time before using it so that
>you know the range the joystick will travel on each axis, and where the
>approximate center will be.  Typically, the faster the computer, the larger
>the range will be.
>> Since all the units I've tried have done the same I'm presuming there is
>> some way around this which needs to be implemented in the main
>> game/program loop. So, this question is to anyone who has programmed a
>> game and knows how to detect one press at a time, with nothing done when
>> the stick is centred.
>You have to consider the joystick's center with an error of margin.  If you
>assume center is once place, sure your little sprite is going to bounce around
>all over.

Understood. I've messed around the margins before and it did improve
things - but, not quite enough. And other peoples units have given me
the same problems....

Quote
>Think of the joystick as something like this.  The outer box represents the
>polar extremes.  The inner box is the response threshold.  Depending on the
>joystick's accuracy, you could make the threshold something like 10% to 30%
>away from center to register a response.  It's my understanding that the
>distance between the low end and the center and between the high end and the
>center -- so you may have to scale both sides accordingly.

Are you sure this is definetly a margin problem? If it is (and it might
be) I'll take your advice and persevere with my own code.

I just needed to know whether I was lacking some special technique
people use in game engines to get around this problem.

Quote
>> This wasn't too easy to put into words, but I hope I've conveyed
>> everything ok.
>Kind of abstract since I don't know anything about the code you're using, but
>hopefully this helps make sense of it a little more.

I didn't see the point in posting the code. I'm attempting to get a
really decent joystick unit written for my game engine, and as far as I
was concerned - the small pseudo-code example I gave was enough to
illustrate what problem I'm having.

I appreciate your help and rest assured - these damn sticks haven't
beaten me yet. :)

Karl
--
Karl Dyson <ka...@feklore.demon.co.uk>
"Destroy first, ask questions later."

Re:Joystick Help Required


Quote
Karl Dyson wrote:
> In article <34B487BE.A5EE808F@_ix.netcom.com>, Scott Earnest
> <setech@_ix.netcom.com> writes

> >> I get a constant stream of output, even when the stick is centred. There
> >> seems to be some flucuation as to the values of a stick when centred, so
> >> that quite often it registers as a direction has been pressed. Which
> >> would cause the main item of control e.g main sprite, to be bouncing all
> >> over the place.
> >Okay, couple problems.  First, since you mention jumping, I'll have to assume
> >this is an analog joystick.
> Nope. The one I'm using isn't. But the program needs to be able to use
> both.

Hm.  It runs in my mind that when I've seen games which support both analog
and digital, there may be an option which lets you choose between one or the
other.  I have both an analog and a digital joystick, and I've never attempted
to program the digital one.  Specifically, mine's a Gravis Gamepad Pro, and
considering all the buttons (10 total), I'm not sure how it's accomplished.
It would seem to be that with a digital joystick, it should just report
whether an axis is triggered and in what direction.

Quote
> >Second, since you don't show any code, I also
> >have to assume any code you've used wasn't written with any great
> >understanding of *exactly* how a joystick works.
> I know how a joystick works but assume away....

Bad assumption then, sorry.

Quote
> >Joysticks just aren't an exact science.  The hardware is a basically an analog
> >digital converter.  On a PC, timing and position is determined by signal
> >timing.  There's always going to be some variance.
> True... but game engines have to take that into account - and that,
> seems to be where I'm having the problems.

Do you happen to know just how much variance there is?  Writing a relatively
simple program to watch the values might give some indication.  Also, what
make of joystick is it?  I've found that some brands of joystick are just
plain junk that drift a lot -- cheap pots, I would guess.

Quote
> [...]
> >Think of the joystick as something like this.  The outer box represents the
> >polar extremes.  The inner box is the response threshold.  Depending on the
> >joystick's accuracy, you could make the threshold something like 10% to 30%
> >away from center to register a response.  It's my understanding that the
> >distance between the low end and the center and between the high end and the
> >center -- so you may have to scale both sides accordingly.
> Are you sure this is definetly a margin problem? If it is (and it might
> be) I'll take your advice and persevere with my own code.

It's a possible problem, I can't say if it's *the* problem.  I'm only making
guesses on what I can figure from what you've told me, but there's plenty that
you haven't -- system type and speed, specifics about the joystick(s) you're
using to test, the actual code, and the algorithm you're using to handle
feedback from the joystick.  I think you posted a little bit about the
algorithm, but that article is no longer available on my news server.

Quote
> I just needed to know whether I was lacking some special technique
> people use in game engines to get around this problem.

> >> This wasn't too easy to put into words, but I hope I've conveyed
> >> everything ok.
> >Kind of abstract since I don't know anything about the code you're using, but
> >hopefully this helps make sense of it a little more.
> I didn't see the point in posting the code. I'm attempting to get a
> really decent joystick unit written for my game engine, and as far as I
> was concerned - the small pseudo-code example I gave was enough to
> illustrate what problem I'm having.

I'm still in the dark about anything that will help you out, then?  I don't
suppose you have any small program you could send so I can get a better idea
of what's going on here?

Quote
> I appreciate your help and rest assured - these damn sticks haven't
> beaten me yet. :)

That's a good sign.

Quote
> Karl
> --
> Karl Dyson <ka...@feklore.demon.co.uk>
> "Destroy first, ask questions later."

--
Scott Earnest            | SPAM protection in effect. Remove  |
setech@_ix.netcom.com    | "_" as needed for true addresses.  |
earnests@_homenet.lm.com |    UIN:1136443  EFnet:pale_blue    |
sinykal@_{*word*104}space.org  | URL: http://www.netcom.com/~setech |

Re:Joystick Help Required


In article <34BD56B0.6AEE31CE@_ix.netcom.com>, Scott Earnest
<setech@_ix.netcom.com> writes

Quote
>> >Okay, couple problems.  First, since you mention jumping, I'll have to assume
>> >this is an analog joystick.
>> Nope. The one I'm using isn't. But the program needs to be able to use
>> both.
>Hm.  It runs in my mind that when I've seen games which support both analog
>and digital, there may be an option which lets you choose between one or the
>other.  

Good point. But from what I've seen if the program is set up to use a
analogue joystick then an digital one generally works but not always
very nicely.

Quote
>I have both an analog and a digital joystick, and I've never attempted
>to program the digital one.  Specifically, mine's a Gravis Gamepad Pro, and
>considering all the buttons (10 total), I'm not sure how it's accomplished.
>It would seem to be that with a digital joystick, it should just report
>whether an axis is triggered and in what direction.

You'd think so wouldn't you. I know that's how the Amiga joystick ports
worked (amongst others).

Quote
>> >Second, since you don't show any code, I also
>> >have to assume any code you've used wasn't written with any great
>> >understanding of *exactly* how a joystick works.
>> I know how a joystick works but assume away....
>Bad assumption then, sorry.

Not at all. If you assume that the person knows nothing then you stand a
good chance of telling them some small detail they had forgotten or
indeed, never known.

Quote
>> >Joysticks just aren't an exact science.  The hardware is a basically an
>analog
>> >digital converter.  On a PC, timing and position is determined by signal
>> >timing.  There's always going to be some variance.
>> True... but game engines have to take that into account - and that,
>> seems to be where I'm having the problems.
>Do you happen to know just how much variance there is?  

A *huge* variance. e.g Dropping from 360 down to 8.

Quote
>Writing a relatively
>simple program to watch the values might give some indication.  

Did that. :)

Quote
>Also, what
>make of joystick is it?  I've found that some brands of joystick are just
>plain junk that drift a lot -- cheap pots, I would guess.

It's a Primax. Read: cheap junk. :)

Quote
>> Are you sure this is definetly a margin problem? If it is (and it might
>> be) I'll take your advice and persevere with my own code.
>It's a possible problem, I can't say if it's *the* problem.  I'm only making
>guesses on what I can figure from what you've told me, but there's plenty that
>you haven't -- system type and speed,

That's the strange thing. I've just upgraded my PC the other day - and
now the problem has gone. Can you believe this? I've spent many days and
many sleepness nights writing countless joystick control units and
making millions of moderations to other people's units - only to have
the problem disappear.

Quote
>> I appreciate your help and rest assured - these damn sticks haven't
>> beaten me yet. :)
>That's a good sign.

Yup. Determination. What all good programmers need.

Newbies:
Don't fall into the trap of thinking that you need skill to write good
programs. You just need a lot of time, and a stubborn nature. :)

Thanks for all your assistance Scott. I'm just sorry that we weren't
able to solve the problem before it disappeared.

:)

Karl
--
Karl Dyson <ka...@feklore.demon.co.uk>
"Flee before me or else you'll fall before me."

Other Threads