Page 1 of 2

Joystick Event Handler

Posted: Thu Dec 01, 2011 9:01 am
by blutbeere
Hey there and thanks for checking by.
Though I´ve been recently busy with Unity,
I´ve to say that creating Games with Irrlicht is a lot more satisfying. :D
Atm I am trying to create a simple but working Joystick Handler,
I have searched the forum or wiki but I cant seem to find something working.
So I started to build my own on base of the Tutorial.

Joystick Class:

Code: Select all

class Joystick : public IEventReceiver
{
public:
        virtual bool OnEvent(const SEvent& event)
        {
        if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT
                        && event.JoystickEvent.Joystick == 0)
                {
                        JoystickState = event.JoystickEvent;
                }
 
                return false;
        }
 
        const SEvent::SJoystickEvent & GetJoystickState(void) const
        {
                return JoystickState;
        }
 
 
                Joystick()
        {
        }
 
private:
        SEvent::SJoystickEvent JoystickState;
};
Main:

Code: Select all

array<SJoystickInfo> joystickInfo; //create array to load info
        if(device->activateJoysticks(joystickInfo)) //add active joysticks to my number
 
Loop:

Code: Select all

bool movedWithJoystick = false; //to check if we are using joystick 
        if(joystickInfo.size() > 0) //if we are using joystick do:
        {
                f32 moveHorizontal = 0.f;
                f32 moveVertical = 0.f;
                const SEvent::SJoystickEvent & joystickData = receiver.GetJoystickState();
                const f32 DEAD_ZONE = 0.05f; //to prevent
 
                moveHorizontal = 
                        (f32)joystickData.Axis[SEvent::SJoystickEvent::AXIS_X] / 32767.f;
                    if(fabs(moveHorizontal) < DEAD_ZONE)
                                moveHorizontal = 0.f;
 
            moveVertical =
                        (f32)joystickData.Axis[SEvent::SJoystickEvent::AXIS_Y] / -32767.f;
                if(fabs(moveVertical) < DEAD_ZONE)
                                moveVertical = 0.f;     
 const u16 povDegrees = joystickData.POV / 100;
                        if(povDegrees < 360)
                        {
                                if(povDegrees > 0 && povDegrees < 180)
                                        moveHorizontal = 1.f;
                                else if(povDegrees > 180)
                                        moveHorizontal = -1.f;
 
                                if(povDegrees > 90 && povDegrees < 270)
                                        moveVertical = -1.f;
                                else if(povDegrees > 270 || povDegrees < 90)
                                        moveVertical = +1.f;
                        }
        }
Okay...basicly I am able to use my Analog Sticks and that works just fine.
But should I do to implement Joystick Buttons?
I found this
u32 irr::SEvent::SJoystickEvent::ButtonStates
and
IsButtonPressed()
But I am not sure how to implement them with Joysticks :(

It would be nice if someone could add 1 simple Button_A to the Code above. <3
Thanks for your time,
Chris

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 11:15 am
by hybrid
What about the joystick example from the Irrlicht SDK?

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 11:19 am
by blutbeere
What about the joystick example from the Irrlicht SDK?
build my own on base of the Tutorial / to implement Joystick Buttons
There are no Buttons in the Tutorial :O

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 12:09 pm
by docWild
Check out the SJoystickEvent struct in the IEventReceiver.h file or on the API here :http://irrlicht.sourceforge.net/docu/st ... event.html

You basically get a 32bit bitmap in a u32 which you can poll against known joystick info. I've never done it with irrlicht, but it seems straightforward enough.

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 12:23 pm
by blutbeere
Well...

Code: Select all

 bool IsButtonPressed(u32 button) const
But still how do I know which Button to check?

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 1:53 pm
by blutbeere
Would this work, placed in the loop?

Code: Select all

if((u32)joystickData.IsButtonPressed(1))
                        {
                                //do action
                        }

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 1:57 pm
by docWild
blutbeere wrote:Would this work, placed in the loop?

Code: Select all

if((u32)joystickData.IsButtonPressed(1))
                        {
                                //do action
                        }
Yes. The integer "1" relates to the 32 bit binary number: 0000 0000 0000 0001.. you have 31 other bits to test for other buttons. If you want to use integers, then remember the order: 1,2,4,8,16 etc.. powers of two. Testinfg the integer 3, in this case, is actually a way of testing for the first and second bits, eg: 0000 0000 0000 0011. This could tell you that both buttons are being pressed (this probably is not what you want).

That is what you have to test for. I'm no expert on irrlicht but I imagine that, internally, it will be able to differentiate between different buttons being pressed. It will mark this by activating a bit in a 32 bit bitmap, giving you a possibility of identifying 32 separate buttons. What it will not do is return a message saying "the blue one next to the red one is pressed". What you have to do, is test the event codes against what buttons you are pressing. When you know that the third bit, for example, relates to the blue button then you know how to react to that event. The simplest way to understand this is to write some printf or cout statements for your program to report what is happening, so you can plan your reactions appropriately. You can see what is going on inthe .h file already mentioned:

Code: Select all

return (ButtonStates & (1 << button)) ? true : false;

If you don't know what the unary (?:), bitwise AND (&) or bitshift(<<), (>>) operators do, then you should do some more reading. Does that make sense?

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 2:35 pm
by blutbeere
Actually all I was looking for was:

Code: Select all

    if((u32)joystickData.IsButtonPressed(1))
                            {
                                    //do action
                            }
Thanks for your reply :)

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 2:40 pm
by hybrid
docWild wrote:

Code: Select all

return (ButtonStates & (1 << button)) ? true : false;
If you don't know what the unary (?:), bitwise AND (&) or bitshift(<<), (>>) operators do, then you should do some more reading. Does that make sense?
Actually, this code shows that you should put in button numbers 1,2,3,4, and so forth. Because the button number is shifted left and checked against the bit in ButtonState. So isPressed(3) means third button, if oyu want to test buttons 1 and 2 you have to make two calls.

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 2:44 pm
by blutbeere
want to test buttons 1 and 2 you have to make two calls.
Thx, I know.
I am using my wireless Xbox 360 Controller he is displayed with 5 Axis and 10 Buttons, which means I can use 10-1 = 9 Buttons...
EDIT:
If some1 needs the Numbers:

Xbox Controller->
(A)=0
(B)=1
(X)=2
(Y)=3

[LB]=4
[RB]=5

Back=6
Start=7

StickPressL=8
StickPressR=9

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 2:53 pm
by docWild
hybrid wrote:
docWild wrote:

Code: Select all

return (ButtonStates & (1 << button)) ? true : false;
If you don't know what the unary (?:), bitwise AND (&) or bitshift(<<), (>>) operators do, then you should do some more reading. Does that make sense?
Actually, this code shows that you should put in button numbers 1,2,3,4, and so forth. Because the button number is shifted left and checked against the bit in ButtonState. So isPressed(3) means third button, if oyu want to test buttons 1 and 2 you have to make two calls.
:oops: my mistake

Re: Joystick Event Handler

Posted: Thu Dec 01, 2011 3:24 pm
by blutbeere
No Problem xD,
I have added all the Numbers of the Buttons in my last Reply if some1 might need them.
Thanks again for your help :)

Re: Joystick Event Handler

Posted: Mon Jun 20, 2022 8:56 am
by netpipe
IsButtonPressed() worked for me. i was wondering though if there should be a device->deactivateJoysticks () so that it does not generate junk data messages for me after checking if joysticks are present.

Re: Joystick Event Handler

Posted: Mon Jun 20, 2022 10:14 am
by CuteAlien
I suppose it would make sense. If you write a patch I can apply it.

Re: Joystick Event Handler

Posted: Mon Jun 20, 2022 11:36 pm
by netpipe
it looks like we would just use this code again from the devicecreate constructor

for (u32 joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
{
if (ActiveJoysticks[joystick].fd >= 0)
{
close(ActiveJoysticks[joystick].fd);
}
}