Joystick Event Handler

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
blutbeere
Posts: 29
Joined: Sat Nov 19, 2011 10:59 am
Location: Bergisch Gladbach, Germany

Joystick Event Handler

Post 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
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Joystick Event Handler

Post by hybrid »

What about the joystick example from the Irrlicht SDK?
blutbeere
Posts: 29
Joined: Sat Nov 19, 2011 10:59 am
Location: Bergisch Gladbach, Germany

Re: Joystick Event Handler

Post 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
docWild
Posts: 38
Joined: Wed Nov 30, 2011 4:29 pm

Re: Joystick Event Handler

Post 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.
blutbeere
Posts: 29
Joined: Sat Nov 19, 2011 10:59 am
Location: Bergisch Gladbach, Germany

Re: Joystick Event Handler

Post by blutbeere »

Well...

Code: Select all

 bool IsButtonPressed(u32 button) const
But still how do I know which Button to check?
blutbeere
Posts: 29
Joined: Sat Nov 19, 2011 10:59 am
Location: Bergisch Gladbach, Germany

Re: Joystick Event Handler

Post by blutbeere »

Would this work, placed in the loop?

Code: Select all

if((u32)joystickData.IsButtonPressed(1))
                        {
                                //do action
                        }
docWild
Posts: 38
Joined: Wed Nov 30, 2011 4:29 pm

Re: Joystick Event Handler

Post 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?
blutbeere
Posts: 29
Joined: Sat Nov 19, 2011 10:59 am
Location: Bergisch Gladbach, Germany

Re: Joystick Event Handler

Post by blutbeere »

Actually all I was looking for was:

Code: Select all

    if((u32)joystickData.IsButtonPressed(1))
                            {
                                    //do action
                            }
Thanks for your reply :)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Joystick Event Handler

Post 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.
blutbeere
Posts: 29
Joined: Sat Nov 19, 2011 10:59 am
Location: Bergisch Gladbach, Germany

Re: Joystick Event Handler

Post 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
Last edited by blutbeere on Thu Dec 01, 2011 3:23 pm, edited 1 time in total.
docWild
Posts: 38
Joined: Wed Nov 30, 2011 4:29 pm

Re: Joystick Event Handler

Post 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
blutbeere
Posts: 29
Joined: Sat Nov 19, 2011 10:59 am
Location: Bergisch Gladbach, Germany

Re: Joystick Event Handler

Post 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 :)
netpipe
Posts: 669
Joined: Fri Jun 06, 2008 12:50 pm
Location: Edmonton, Alberta, Canada
Contact:

Re: Joystick Event Handler

Post 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.
Live long and phosphor!
-- https://github.com/netpipe/Luna Game Engine Status 95%
CuteAlien
Admin
Posts: 9670
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Joystick Event Handler

Post by CuteAlien »

I suppose it would make sense. If you write a patch I can apply it.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
netpipe
Posts: 669
Joined: Fri Jun 06, 2008 12:50 pm
Location: Edmonton, Alberta, Canada
Contact:

Re: Joystick Event Handler

Post 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);
}
}
Live long and phosphor!
-- https://github.com/netpipe/Luna Game Engine Status 95%
Post Reply