Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (773)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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  
  JInput getPollData() never changes, regardless of input  (Read 4287 times)
0 Members and 1 Guest are viewing this topic.
Offline daemor

Junior Newbie





« Posted 2016-11-24 02:33:48 »

Hey guys,
Im working on a robotics project and am attempting to have support for controllers via Jinput, however, I am having difficulty getting the polling to work. I am using an Xbox One controller which I have confirmed works the following ways: Played steam games with it, works as expected for all tests provided by jinput, shows up in when I run my code as a connected device. However, when it comes to getting poll data, the values never seem to change. From what I can tell with the debugger, the components all display the "has_pulled" value to be false. I have followed several tutorials (including the one on this forum) and cannot seem to get it working. My values all stay at the same default value of 0.0 or -something for thumbsticks.

Here is my code:
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  
public class ControllerTest {
   
   public static void main(String[] args){
      //System.out.println("Hello World");
     
      Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
      Controller gamepad = null;
      Component[] components = null;
      EventQueue eventQueue;
      // Run through the list of available input devices and get the gamepad
      for(int i = 0; i < ca.length; i ++){
         if(ca[i].getType().equals(Controller.Type.GAMEPAD)){  // Snag the first instance of a gamepad
            gamepad = ca[i];
         }
      }
      // Print the name of the controller and its type
      if(gamepad != null){
         System.out.println(gamepad.getName() + ": " + gamepad.getType());
         components = gamepad.getComponents();
         System.out.println("COMPONENTS:");
         for(int i = 0; i < components.length; i ++){
            StringBuffer buffer = new StringBuffer();
            buffer.append("Component #" + i + ": ");  // Get the Component #
            buffer.append("\t" + components[i].getName());  // Get the component name
            // Check if the component is analog or relative
            // Relative relies on its past position for values
            // Analog is a current position
            // Analog absolute is a toggle (think your a,b,y,x button)
            if(components[i].isRelative()){
               buffer.append(" [Relative]");
            }
            else if(components[i].isAnalog()){
               buffer.append(" [Analog]");
            }
            else{
               buffer.append(" [Analog Absolute]");
            }
            System.out.println(buffer.toString());
         }
      }
      else{
         System.out.println("No gamepad connected");
      }
     
      float prevPollData = 100f;
      float curPollData;
      String compName;
     
      while(true){
         
         gamepad.poll();
         
         for(int i = 0; i < components.length; i ++){
            curPollData = components[i].getPollData();
            compName = components[i].getName();
            System.out.println(compName + ": " + curPollData);
         }
         
         try {
            Thread.sleep(50);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
}


I am quite baffled as to why this works every way but when I use my code, and by all accounts, I believe I am following the correct method for polling and retrieving information. Any help would be greatly appreciated.
Offline FabulousFellini
« Reply #1 - Posted 2016-11-25 23:55:11 »

Hey, I'm not sure if you got it working, and I'm not sure about your code, but i figured I'd show you mine since it works.  I use a 360 controller  Here it is:

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  
ControllerPad controller = new ControllerPad();  // ControllerPad is a class I made to handle controller events

public void run() {
                        /**
          * IF WE ARE USING A CONTROLLER, CHECK IF IT IS DISCONNECTED IF NOT,
          * GET CONTROLLER CONTROLS
          */

         if (!hasKeyboard) {
            // GET CURRENT STATE OF CONTROLLER, AND CHECK IF CONTROLLER IS
            // DISCONNECTED
            if (!controller.joystick.pollController()) {
               System.out.println("Controller disconnected!");
            }

            // GET CONTROLLER CONTROLS
            controller.getControls();   //this is the method in ControllerPad that handles all of the controller events
         }
}

public void init() {

      /**
       * GET LIST OF CONTROLLERS
       */

      Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();

      for (int i = 0; i < ca.length; i++) {
         /* GET THE NAME OF THE CONTROLLER */
         System.out.println("Controller " + i + ca[i].getName());
      }

      /**
       * CHECK IF CONTROLLER WAS FOUND IF NOT, ADD KEY LISTENER
       */

      if (!controller.joystick.isControllerConnected()) {
         keyboard = new Keyboard(this);
         addKeyListener(keyboard);
         hasKeyboard = true;
         System.out.println("No controller found!");
      }
}


Here is all the code: https://github.com/IanFell/DodgerDog/blob/master/DODGERDOG.java for the main game stuff, but unfortunately the ControllerPad class is not uploaded yet.  Let me know if you want to see it and I'll put it up.  It's basically just a bunch of if/then statements though.

-FabulousFellini
www.fabulousfellini.com
Offline daemor

Junior Newbie





« Reply #2 - Posted 2016-11-27 18:03:53 »

It would be cool to see the ControllerPad class if you could.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline FabulousFellini
« Reply #3 - Posted 2016-11-29 16:02:29 »

It would be cool to see the ControllerPad class if you could.

Cool, I'll put it up when I get home from work.  I thought I had it here but it looks like I deleted the game files off my work computer.

-FabulousFellini
www.fabulousfellini.com
Offline FabulousFellini
« Reply #4 - Posted 2016-11-29 19:02:11 »

Ok, nevermind, turns out I had it at work after all.  Here it is (some of the formatting is messed up so let me know if you have questions:

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  
public class ControllerPad {
   
    public JInputJoystick joystick    = new JInputJoystick(Controller.Type.STICK, Controller.Type.GAMEPAD);
   
    public static boolean joystickButton_0;      // X BUTTON
    public static boolean joystickButton_1;      // A BUTTON  
    public static boolean joystickButton_2;      // B BUTTON
    public static boolean joystickButton_3;      // Y BUTTON
    public static boolean joystickButton_6;           // X BOX BUTTON (left trigger?)
    public static boolean joystickButton_7;      // RIGHT TRIGGER
    public static boolean joystickButton_8;      // SELECT BUTTON
    public static boolean joystickButton_9;      // START BUTTON
   
    public ControllerPad(){}
   
     /**
     * getControls() gets called every tick in the game loop
     */

    public void getControls() {
 
    /**
    * Only checks for Type GAMEPAD
    */

    if(joystick.getControllerType().equals(Controller.Type.GAMEPAD)){
        joystickButton_0       = joystick.getButtonValue(0);
        joystickButton_1       = joystick.getButtonValue(1);
        joystickButton_2       = joystick.getButtonValue(2);
        joystickButton_3       = joystick.getButtonValue(3);
        joystickButton_6       = joystick.getButtonValue(6);
        joystickButton_7       = joystick.getButtonValue(7);
         
        float hatSwitchPosition = joystick.getHatSwitchPosition();
    }
   
      if (Float.compare(hatSwitchPosition, Component.POV.UP) == 0 || joystick.getY_LeftJoystick_Percentage() < 30) { }
   
      if (Float.compare(hatSwitchPosition, Component.POV.DOWN) == 0 || joystick.getY_LeftJoystick_Percentage() > 70) { }
   
           if (Float.compare(hatSwitchPosition, Component.POV.LEFT) == 0 || (joystick.getX_LeftJoystick_Percentage() < 35)) { }
   
      if (Float.compare(hatSwitchPosition, Component.POV.RIGHT) == 0 || (joystick.getX_LeftJoystick_Percentage() > 65)) { }
   
      if (joystickButton_0) { }
     
                 else if (joystickButton_1) { }
           
      else if (joystickButton_2) { }
   
      else if (joystickButton_3) { }
   
      if (joystick.getZAxisPercentage() == 0) { }
   
      if (joystickButton_7) { }

      joystick.pollController();

   }

}

-FabulousFellini
www.fabulousfellini.com
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1861 views)
2018-06-10 19:43:48

EgonOlsen (1883 views)
2018-06-10 19:43:44

EgonOlsen (1252 views)
2018-06-10 19:43:20

DesertCoockie (1684 views)
2018-05-13 18:23:11

nelsongames (1350 views)
2018-04-24 18:15:36

nelsongames (1968 views)
2018-04-24 18:14:32

ivj94 (2739 views)
2018-03-24 14:47:39

ivj94 (1942 views)
2018-03-24 14:46:31

ivj94 (3032 views)
2018-03-24 14:43:53

Solater (1083 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!