ProcessingでPS3のコントローラを使う(Win & Mac)

ここの方法でできそうだったけど、コントローラが微妙に違うのでできないっぽい。

http://www.instructables.com/id/Use-a-PS3-Controller-to-control-an-Arduino-NXT-Bot/step3/Connect-the-PS3-controller-to-Processing/

http://www.ailab.t.u-tokyo.ac.jp/~aoki/ardrone/index.php?Processing%20%2B%20AR.Drone%20%2B%20PlayStation3%20Controller

procontrolのexampleのprintDevicesでDevice名を確認する

<<< available MotioninJoy Virtual Game Controller sticks: >>>

     0: Slider Dial
     1: Y Rotation Z Rotation
     2: Z Axis X Rotation
     3: X axis Y axis

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available MotioninJoy Virtual Game Controller sliders: >>>

     0: Dial absolute
     1: Slider absolute
     2: Z Rotation absolute
     3: Y Rotation absolute
     4: X Rotation absolute
     5: Z Axis absolute
     6: Y axis absolute
     7: X axis absolute

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<<< available MotioninJoy Virtual Game Controller buttons: >>>

     0: 
     1: 
     2: ?
     3: 
     4: L2
     5: R2
     6: L1
     7: R1
     8: Start
     9: Select
     10: L3
     11: R3
     12: PS
     13: Button 13
     14: Button 14
     15: Button 15
     16: Button 16
     17: Button 17
     18: Button 18
     19: Button 19
     20: cooliehat: Hat Switch

<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

自分のDevice名はMotioninJoy Virtual Game Controllerということなので変更する。


しかし依然としてgetSliderとかでNULLポインタが入ってる。しばらく試行錯誤した結果、文字列じゃなくて配列番号でアクセスすればいいことに気づいた。

 修正前
ControllSlider sliderX = device.getSlider("x");
 修正後1 まだダメ
ControllSlider sliderY = device.getSlider("7");
 最終版 文字列じゃなくて数字
ControllSlider sliderX = device.getSlider(7);


以下が動いたコード

import procontroll.*;
import java.io.*;

ControllIO controll;
ControllDevice device;
ControllStick stick;
ControllButton button;

void setup(){
  size(400,400);
  
  controll = ControllIO.getInstance(this);

  //device = controll.getDevice("PLAYSTATION(R)3 Controller");
  device = controll.getDevice("MotioninJoy Virtual Game Controller");
  device.printSticks();
  device.printSliders();
  device.printButtons();
  device.setTolerance(0.05f);
  
  //ControllSlider sliderX = device.getSlider("x");
  //ControllSlider sliderY = device.getSlider("y");
  ControllSlider sliderX = device.getSlider(7);
  ControllSlider sliderY = device.getSlider(6);

  stick = new ControllStick(sliderX,sliderY);
   
  button = device.getButton(8);
  
  fill(0);
  rectMode(CENTER);
}

float totalX = width/2;
float totalY = height/2;

void draw(){
  background(255);


  if(button.pressed()){
    fill(255,0,0);
  }else{
    fill(0);
  }

  totalX = constrain(totalX + stick.getX(),10,width-10);
  totalY = constrain(totalY + stick.getY(),10,height-10);

  rect(totalX,totalY,20,20);
}

(追記)Macだとやり方が違った。