Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Cant get data to poll  (Read 5927 times)
0 Members and 1 Guest are viewing this topic.
Offline Zarium

Junior Newbie





« Posted 2011-03-24 03:24:34 »

I wrote a small class to implement jinput for joysticks into my code, its picking up the joysticks but the poll data never updates. I have run jtest and it works just fine. Can someone point out whats going wrong before this lib makes me what to hurl an input device? Huh

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
import net.java.games.input.Controller;
import net.java.games.input.ControllerListener;
import net.java.games.input.ControllerEvent;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Component;

public class Joystick implements ControllerListener
{
    private Controller    Joystick_Controller = null;

    private Component[]   Joystick_Parts;
    private float[]       Joystick_oldState;

    public Joystick()
    {
        // add this module to controller listeners
        ControllerEnvironment.getDefaultEnvironment()
                .addControllerListener(this);

        // find a joystick
        processfindJoystick();
       
    }

    public void processfindJoystick()
    {
        this.Joystick_Controller = null;

        // poll a list of present controllers.
        Controller[] tempList = ControllerEnvironment.getDefaultEnvironment()
                .getControllers();

        for(Controller currentController: tempList)
            if(checkisJoystick(currentController))
            {
                setJoystick(currentController);
                return;
            }

        System.out.println("No joystick found.");
    }

    public void setJoystick(Controller newJoy)
    {
                this.Joystick_Controller = newJoy;
                this.Joystick_Parts      = newJoy.getComponents();
                this.Joystick_oldState   = new float[Joystick_Parts.length];


                System.out.println("Active Joystick set to: "
                        + this.Joystick_Controller.getName());
               
                this.Joystick_Controller.poll();

                for(int currentState = 0; currentState <
                        this.Joystick_oldState.length; currentState++)
                    this.Joystick_oldState[currentState] =
                            this.Joystick_Parts[currentState].getPollData();
    }

    public boolean checkisJoystick(Controller thisController)
    {
        if(thisController.getType() == Controller.Type.GAMEPAD ||
           thisController.getType() == Controller.Type.STICK)
            return true;

        return false;
    }

    public void controllerAdded(ControllerEvent ev)
    {
       //  attach to new joystick if theres not one already
        if(Joystick_Controller == null)
          if(checkisJoystick(ev.getController()))
              this.setJoystick(ev.getController());
    }

    public void controllerRemoved(ControllerEvent ev)
    {
       // if we loose the joystick, try to find another one.
        if(ev.getController() == Joystick_Controller)
            this.processfindJoystick();
    }

    public void Poll()
    {
        if(this.Joystick_Controller != null)
            if(!this.Joystick_Controller.poll())
                this.processfindJoystick();
        else
            return;

        for(int currentState = 0; currentState <
            this.Joystick_oldState.length; currentState++)
            if(this.Joystick_oldState[currentState] !=
                 this.Joystick_Controller.getComponents()[currentState].getPollData())
                   System.out.println(this.Joystick_Parts[currentState]
                           .getName() + " " +
                        this.Joystick_Controller.getComponents()[currentState]
                           .getPollData());
                           
    }
}


Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-03-24 04:52:11 »

If you mean the list of input devices never updating, then look at this thread:
http://www.java-gaming.org/topics/jinput-controller-connect-disconnect-problem-sovled/23964/view.html

Offline endolf

JGO Coder


Medals: 7
Exp: 15 years


Current project release date: sometime in 3003


« Reply #2 - Posted 2011-03-24 10:40:20 »

If you mean the list of input devices never updating, then look at this thread:
http://www.java-gaming.org/topics/jinput-controller-connect-disconnect-problem-sovled/23964/view.html
I think they mean that the data itself never updates?, and I can't see anything obvious at this time (spent a whole 20 seconds looking), you're doing the poll on the controller and then getting the poll data, so it should work. Have you tried running the tests that come with JInput?

Endolf

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zarium

Junior Newbie





« Reply #3 - Posted 2011-03-24 12:47:08 »

Yes. Ive run the test module and it works fine. Button presses are displayed normally. When I run the code provided, the only output I get is the notice that my joystick has been found. However button/axis presses do not generate output.
Offline endolf

JGO Coder


Medals: 7
Exp: 15 years


Current project release date: sometime in 3003


« Reply #4 - Posted 2011-03-24 12:51:37 »

Ok, that means it's not JInput. Hmmmm

Offline Zarium

Junior Newbie





« Reply #5 - Posted 2011-03-24 19:11:27 »

Ive swapped controllers, checked my security permissions, and tried a few other things. I noticed the module gives some errors when loading but so does the test module. And the test module still works correctly.

My console output from the code is:

Quote
Loading: net.java.games.input.LinuxEnvironmentPlugin
Failed to open device (/dev/input/event11): Failed to open device /dev/input/event11 (13)

Failed to open device (/dev/input/event10): Failed to open device /dev/input/event10 (13)

Failed to open device (/dev/input/event7): Failed to open device /dev/input/event7 (13)

Failed to open device (/dev/input/event6): Failed to open device /dev/input/event6 (13)

Failed to open device (/dev/input/event5): Failed to open device /dev/input/event5 (13)

Failed to open device (/dev/input/event4): Failed to open device /dev/input/event4 (13)

Failed to open device (/dev/input/event3): Failed to open device /dev/input/event3 (13)

Failed to open device (/dev/input/event2): Failed to open device /dev/input/event2 (13)

Failed to open device (/dev/input/event1): Failed to open device /dev/input/event1 (13)

Failed to open device (/dev/input/event0): Failed to open device /dev/input/event0 (13)

Linux plugin claims to have found 1 controllers
Active Joystick set to: Gravis GamePad Pro USB



Offline endolf

JGO Coder


Medals: 7
Exp: 15 years


Current project release date: sometime in 3003


« Reply #6 - Posted 2011-03-24 19:15:20 »

Are you sure the device you are interested in is seen by JInput as a gamepad or stick type device?, check with the ControllerTextTest to see the type of the device.

HTH

Endolf

Offline Zarium

Junior Newbie





« Reply #7 - Posted 2011-03-24 20:17:20 »

I changed several things in the code, and now it works well. I'm not really sure why, but I think I was either calling pollData() to many times instead of storing it to a variable, or because the function calling the poll of this module had a Synchronized keyword on it. But I rewrote it this way and it works now.

 Also: I never did get controller connection/disconnection listener to work. That may be because im on a linux build

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;

public class Joystick
{
    private Controller    Joystick_Controller = null;

    private float[]       Joystick_oldpoll  = new float[20];

    public Joystick()
    {
        // find a joystick
        processfindJoystick();
       
    }

    public void processfindJoystick()
    {
        this.Joystick_Controller = null;

        // poll a list of present controllers.
        Controller[] tempList = ControllerEnvironment.getDefaultEnvironment()
                .getControllers();

        for(Controller currentController: tempList)
            if(checkisJoystick(currentController))
            {
                setJoystick(currentController);
                return;
            }

        System.out.println("No joystick found.");
    }

    public void setJoystick(Controller newJoy)
    {
                this.Joystick_Controller = newJoy;

                System.out.println("Active Joystick set to: "
                        + this.Joystick_Controller.getName());              
    }

    public boolean checkisJoystick(Controller thisController)
    {
        if(thisController.getType() == Controller.Type.GAMEPAD ||
           thisController.getType() == Controller.Type.STICK)
            if(thisController.poll())
             return true;

        return false;
    }

    public void poll()
    {
        if(this.Joystick_Controller != null)
            if(!this.Joystick_Controller.poll())
                this.processfindJoystick();


        if(this.Joystick_Controller == null)
        {
            System.out.println("No Joystick");
            return;
        }

        float tempPollData = 0.0f;
           
        for(int currentComp = 0;
        currentComp < this.Joystick_Controller.getComponents().length;
        currentComp++)
        {
            tempPollData = this.Joystick_Controller
                    .getComponents()[currentComp].getPollData();

            if(tempPollData != this.Joystick_oldpoll[currentComp])
            {
                System.out.println(this.Joystick_Controller.getComponents()
                        [currentComp].getName());

                this.Joystick_oldpoll[currentComp] = tempPollData;

            }
        }                          
    }
}


Offline Zarium

Junior Newbie





« Reply #8 - Posted 2011-03-24 21:02:20 »

Seems my problem wasnt cleared up completely by the code change, only I am now more confused than before. I have two joysticks, which I am not plugging in at the same time. Both joysticks, when run with jtest respond correctly and fully. However with my code above, only one joystick responds correctly.

The first one a standard USB gravis gamepad, works fine in my code and jtest.

The second one, an adapter hub for ps2 controllers, works fully with jtest.
However under my code, its detected, and only one component axis change is detected, only right after the program starts.
I add a few lines to dump a list of all the detected controllers, and I tried plugging the ps2 controller into player 2's slot to see
if I had the wrong controller. Again jtest works fine with it attached to player 1's port on the adapter hub.

Quote
Linux plugin claims to have found 2 controllers
Available Controller List
Twin USB Joystick
Twin USB Joystick
Active Joystick set to: Twin USB Joystick
x:-1.0
y:-1.0
z:-1.0
rz:-1.0

The axis change that is detected doesnt seem to be related to the controller, because I get the same output with not controller plugged into the hub after starting the program. I am confused as to why jtest would find the output and not this code,
Offline endolf

JGO Coder


Medals: 7
Exp: 15 years


Current project release date: sometime in 3003


« Reply #9 - Posted 2011-03-25 09:05:43 »

Does it work with ControllerReadTest or ControllerEventTest?

Endolf

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zarium

Junior Newbie





« Reply #10 - Posted 2011-03-25 13:49:08 »

It works with both tests, heres the console output from text test.

Quote
Twin USB Joystick
Type: Stick
Component Count: 17
Component 0: Trigger
    Identifier: Trigger
    ComponentType: Absolute Digital
Component 1: Thumb
    Identifier: Thumb
    ComponentType: Absolute Digital
Component 2: Thumb 2
    Identifier: Thumb 2
    ComponentType: Absolute Digital
Component 3: Top
    Identifier: Top
    ComponentType: Absolute Digital
Component 4: Top 2
    Identifier: Top 2
    ComponentType: Absolute Digital
Component 5: Pinkie
    Identifier: Pinkie
    ComponentType: Absolute Digital
Component 6: Base
    Identifier: Base
    ComponentType: Absolute Digital
Component 7: Base 2
    Identifier: Base 2
    ComponentType: Absolute Digital
Component 8: Base 3
    Identifier: Base 3
    ComponentType: Absolute Digital
Component 9: Base 4
    Identifier: Base 4
    ComponentType: Absolute Digital
Component 10: Base 5
    Identifier: Base 5
    ComponentType: Absolute Digital
Component 11: Base 6
    Identifier: Base 6
    ComponentType: Absolute Digital
Component 12: x
    Identifier: x
    ComponentType: Absolute Analog
Component 13: y
    Identifier: y
    ComponentType: Absolute Analog
Component 14: z
    Identifier: z
    ComponentType: Absolute Analog
Component 15: rz
    Identifier: rz
    ComponentType: Absolute Analog
Component 16: pov
    Identifier: pov
    ComponentType: Absolute Digital

Offline endolf

JGO Coder


Medals: 7
Exp: 15 years


Current project release date: sometime in 3003


« Reply #11 - Posted 2011-03-25 16:22:34 »

and does it get axis updates?, just trying to make sure this is not a JInput issue, but a usage issue.

Endolf

Offline Zarium

Junior Newbie





« Reply #12 - Posted 2011-03-25 21:23:42 »

yes, axis and buttons in jtest seem to update fine
Offline endolf

JGO Coder


Medals: 7
Exp: 15 years


Current project release date: sometime in 3003


« Reply #13 - Posted 2011-03-25 22:59:28 »

Not jtest, ControllerReadTest or ControllerEventTest, not just displaying the names etc, but actual values when the controller changes. I'm trying to figure out of JInput is not detecting them, or if it's the usage of JInput.

Cheers

Endolf

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (41 views)
2014-10-17 17:59:02

Norakomi (33 views)
2014-10-17 05:22:06

Norakomi (26 views)
2014-10-17 05:20:20

lcass (30 views)
2014-10-16 06:18:58

TehJavaDev (59 views)
2014-10-14 14:39:48

TehJavaDev (60 views)
2014-10-14 14:35:47

TehJavaDev (50 views)
2014-10-14 14:32:37

BurntPizza (66 views)
2014-10-12 13:24:42

BurntPizza (38 views)
2014-10-12 13:10:45

BurntPizza (80 views)
2014-10-12 12:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-10 12:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-03 12:36:02

List of Learning Resources
by Longor1996
2014-08-17 00:40:00

List of Learning Resources
by SilverTiger
2014-08-06 09:33:27

Resources for WIP games
by CogWheelz
2014-08-02 06:20:17

Resources for WIP games
by CogWheelz
2014-08-02 06:19:50

List of Learning Resources
by SilverTiger
2014-08-01 06:29:50

List of Learning Resources
by SilverTiger
2014-08-01 06:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!