Your browser does not support the canvas tag.

Week 10
actual math problems
algorithms presentation

Video and live video


Game Control Plus
To use a Joystic we need to install the Game Control Plus Library. Normally I would suggest using the Examples and the API to understand how a library works, however, in this case use the examples on this page. This method plays better with the work we have already done in class, and greatly simplifies the process (in my opinion).

Joystick Hello world

This first code example should show the number of the joystick button being pressed. If it doesn't, look in the meesage window. The line " println(control.deviceListToText("")); " is asking the program to look at the list of all the USB devices on the computer and print them in the window. The line "device = control.getDevice(2);" is alerting the program that the joystick is in position 2 (don't forget that arrays start counting at 0). If your joystick is not the third item (at possition 2), then change the number to reflect the correct port. You will need to adjust this number for every example sketch that uses the joystick!

Just buttons

import org.gamecontrolplus.*;
ControlIO control;
ControlDevice device;
ControlButton button;
PFont font; 
int buttonTotal; 

void setup() {
  size(400, 400);
  control = ControlIO.getInstance(this);
  println(control.deviceListToText("")); 
  device = control.getDevice(2); // uses the third item on the list as the joystick
  println("number of buttons :" + device.getNumberOfButtons());
  font = createFont("Helvetica", 48); 
  textFont(font);
  textAlign(CENTER);
  buttonTotal = device.getNumberOfButtons(); 
}

void draw() {
  background(255);
  fill(0); 
  for (int i = 0; i < buttonTotal; i ++) { // go from 0 to total number of buttons
    button = device.getButton(i); // put the state of the current button into "button"
    if (button.pressed() == true) { // if that button is pressed ...
      text(i, i * 24 + 20, height/2 + 20); // put the number of the button on the screen. 
    }
  }
}

Buttons and Sliders

This example brings in the annalog sticks, which the library calls "sliders". This code is really usefull for determining which slider or button is which. The library puts the buttons and sliders in arrays, so we just need to use for loops to extract that information.
import org.gamecontrolplus.*;
ControlIO control;
ControlDevice device;
ControlButton button; 
ControlSlider slider; 

int totalButtons; 
int totalSliders; 

void setup() {
  control = ControlIO.getInstance(this);
  size(400, 400); 
  println(control.deviceListToText("")); 
  device = control.getDevice(2); 
  device.setTolerance(.05); 
  println(device.getNumberOfButtons() + " number of buttons"); 
  println(device.getNumberOfSliders() + " number of sliders"); 
  totalButtons = device.getNumberOfButtons(); 
  totalSliders = device.getNumberOfSliders();
}

void draw() {
  background(0);
  fill(255); 
  text("Buttons", 20, height/2 + 10); 
  for (int i = 0; i < totalButtons; i ++) { // go from 0 to the number of buttons
    button = device.getButton(i); // the button i into the instance "button"
    if (button.pressed() == true) { // if that button is pressed ...
      fill(255, 0, 0);
    } else {
      fill(255);
    }
    text(i, i * 26 + 10, height/2 + 30); // put the number of the button on the screen.
  }
  fill(255); 
  for (int i = 0; i < totalSliders; i ++) { // go from 0 to the number of sliders
    slider = device.getSlider(i); // put the state of the current slider into "slider"
    text("slider" + i + " :" + slider.getValue(), 30, i * 30 + 30);
  }
}

Using just the buttons and sliders you need

Use the code above to detemine which sliders and buttons you need, you can write code that directly references the button or slider you want without using a for loop.

import org.gamecontrolplus.*;
ControlIO control;
ControlDevice device;
ControlButton buttonLeft, buttonRight; // make a special button variable for each button used
ControlSlider sliderMoveX, sliderMoveY; // make a variable for each slider 

float brushX, brushY; // brush location
float speedX, speedY; // speed of brush

void setup() {
  control = ControlIO.getInstance(this);
  size(400, 400); 
  device = control.getDevice(2); 
  device.setTolerance(.05f); 
  println(device.getNumberOfButtons() + " number of buttons"); 
  println(device.getNumberOfSliders() + " number of sliders"); 
  //  totalButtons = device.getNumberOfButtons(); 
  //  totalSliders = device.getNumberOfSliders();
  sliderMoveX = device.getSlider(0); 
  sliderMoveY = device.getSlider(1); 
  buttonLeft = device.getButton(3); 
  buttonRight = device.getButton(1); 
  smooth(); 
  brushX = width/2; 
  brushY = height/2;
}

void draw() {
  if (buttonLeft.pressed() == true) {
    fill(255, 0, 0);
  } else if (buttonRight.pressed() == true) {
    fill(255, 255, 0);
  } else {
    fill(200);
  }
  speedX = sliderMoveX.getValue() * 3; 
  speedY = sliderMoveY.getValue() * 3; 
  brushY += speedY; 
  brushX += speedX; 
  ellipse(brushX, brushY, 20, 20);
}

In class ...