procontroll

edited July 2014 in Library Questions

Hello community,

I have a question about the use of a controller. I would like to use the joystick of a ps3 controller to map the mouse movement. I use a pc/windows. I have download the MotioninJoy for the driver and I have mapped the stick, but the movement is not correct. can someone help me, please

this is the code

import procontroll.*;
import java.io.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

Robot robot;
ControllIO controll;
ControllDevice device;
ControllStick stick; 

void setup(){

noFill();

smooth();
frameRate(20);

try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}

controll = ControllIO.getInstance(this);

device = controll.getDevice("MotioninJoy Virtual Game Controller");
device.setTolerance(0.05f);


 stick = device.getStick(3); 
 stick.setTolerance(0.05f); 
}
void draw(){

 x1 +=stick.getY();
  y1 +=stick.getX();  

  robot.mouseMove(x1,y1);

Answers

  • When posting code into this forum highlight it and press Ctrl+K so that it is formatted for the forum.

    stick.getX() and stick.getY() will return a value in the range -1.0 to 1.0 but the variables x1 and y1 are integers so the statement x1 and y1 will rarely change because of rounding the += calculations.

    Change x1 and y1 to floats and use robot.mouseMove(round(x1), round(y1));

    That might wotk but proControll is an old library and has some calls to methods deprecated in Processing 2. You might try the Game Control Library (install via Add Library in PDE) library instead.

    I used the Game Control Plus library and created this program for an XBOX 360 controller and it worked for me. (You will need to change the device and slider names to suit your device)

    import org.gamecontrolplus.gui.*;
    import org.gamecontrolplus.*;
    import net.java.games.input.*;
    
    import java.io.*; 
    import java.awt.AWTException; 
    import java.awt.Robot; 
    import java.awt.event.KeyEvent;
    
    Robot robot; 
    ControlIO control; 
    ControlDevice gpad; 
    
    float x1=0, y1=0;
    float posX = 0, posY = 0;
    
    void setup() {
      size(300, 300);
      noFill();
      // Initialise to the centre of the sketch window
      x1 = width/2;
      y1 = height/2;
      smooth(); 
      frameRate(20);
    
      try { 
        robot = new Robot();
      } 
      catch (AWTException e) { 
        e.printStackTrace();
      }
    
      control = ControlIO.getInstance(this);
      // Change the name of the device and sliders to match 
      // your device
      gpad = control.getDevice("Controller"); 
      gpad.getSlider("x").setTolerance(0.1);
      gpad.getSlider("y").setTolerance(0.1);
    } 
    
    void getInput() {
      // Get the current position on screen
      posX = frame.getLocationOnScreen().x;
      posY = frame.getLocationOnScreen().y;
      // Upadte the desired mouse position
      x1 += gpad.getSlider("x").getValue();
      y1 += gpad.getSlider("y").getValue();
    }
    
    void draw() {
      getInput();
      robot.mouseMove(round(posX + x1), round(posY + y1));
    }
    
  • hey quark,

    thx for your answer, I have download the Game Control Library and have change the device and the sliders, but when I run the sketch I get an error - "illegal component state exception: component must be showing on the screen to determine its location" - I have search in the internet but I didn't find anything, can you help me please.

  • This exception is thrown when you call frame.getLocationOnScreen() before the frame is displayed.

    I am not saying this will solve the problem but try replacing the last 2 methods with

    boolean getInput() {
      try {
        // Get the current position on screen
        posX = frame.getLocationOnScreen().x;
        posY = frame.getLocationOnScreen().y;
        // Upadte the desired mouse position
        x1 += gpad.getSlider("x").getValue();
        y1 += gpad.getSlider("y").getValue();
        return true;
      }
      catch(Exception e) {
        return false;
      }
    }
    
    void draw() {
      if (getInput()) {
        robot.mouseMove(round(posX + x1), round(posY + y1));
      }
    }
    
  • BTW if a question is aimed at a particular member then put the @ in front of their user-name e.g. @kokagr

Sign In or Register to comment.