1. #creativePact Days 18-20

    #creativePact haven’t updated for a few days, but i did spend ages working on this on Saturday but only got it working today. It’s now controllable with a PS2 controller (I got a PS2 controller to USB cable off Amazon for it). The Left Analog stick replaces the functionality of the mouse position. The Triangle, Circle etc. replace the Button presses. I’ll add some more functionality soon.

    I sort of solved a problem with Scott McLaughlin’s worms http://creativepact-2010-scottmclaughlin.blogspot.com/ leaving the screen, by using the constrain() function to stop the swarms being able to leave the screen. Haven’t quite figured how to stop the PS2 controls position leaving the screen yet though…I’ll have a think.

    import procontroll.*;

    import net.java.games.input.*;

    Mover[] mover = new Mover[400];

    ControllIO controllIO;

    ControllDevice joypad;

    ControllStick stick1;

    ControllStick stick2;

    ControllButton buttonX;

    ControllButton buttonC;

    ControllButton buttonT; 

    ControllButton buttonS;

    ControllButton buttonL1;

    ControllButton buttonL2;

    ControllButton buttonL3;

    ControllButton buttonR1;

    ControllButton buttonR2;

    ControllButton buttonR3;

    ControllButton buttonSelect;

    ControllButton buttonStart;

    ControllButton buttonUp;

    ControllButton buttonDown;

    ControllButton buttonLeft;

    ControllButton buttonRight;

    void setup() {

      size(640, 480);

      smooth();

      background(0);

      controllIO = ControllIO.getInstance(this);

      joypad = controllIO.getDevice("Twin USB Joystick");

      stick1 = joypad.getStick(0);

      stick2 = joypad.getStick(1);

      buttonT = joypad.getButton(0);

      buttonC = joypad.getButton(1);

      buttonX = joypad.getButton(2);

      buttonS = joypad.getButton(3);

      for (int i = 0; i < mover.length; i++) {

        mover[i] = new Mover();

      }

    }

    void draw() {

      background(180, 100, 180);

      for (int i = 0; i < mover.length; i++) {

        mover[i].update();

        mover[i].display();

        mover[i].checkEdges();

      }

    }

    class Mover {

      PVector location;

      PVector velocity;

      PVector acceleration;

      float topspeed;

      float xstick2;

      float ystick2;

      float xsitck3;

      float ystick3;

      Mover() {

        location = new PVector(random(width), random(height));

        velocity = new PVector(0, 0);

        topspeed = 5;

      }

      void update() {

        float xstick2 = stick2.getTotalY() + height/2;

        float ystick2 = stick2.getTotalX() + width/2;

        float xstick3 = constrain(xstick2, 0, width);

        float ystick3 = constrain(ystick2, 0, height);

        PVector mouse = new PVector(xstick3, ystick3);

        PVector dir = PVector.sub(mouse, location);

        dir.normalize();

        if (buttonT.pressed() == true) {

          dir.mult(0.000001);

        } 

        else if (buttonC.pressed() == true) {

          dir.mult(1);

        } 

        else if (buttonX.pressed() == true) {

          dir.mult(5);

        } 

        else if (buttonS.pressed() == true) {

          dir.mult(0.8);

        } 

        else {

          dir.mult(0.2);

        }

        acceleration = dir;

        velocity.add(acceleration);

        velocity.limit(topspeed);

        location.add(velocity);

      }

      void display() {

        stroke(location.x, location.y, location.y);

        fill(location.x, location.y, location.y);

        ellipse(location.x, location.y, 2, 2);

      }

      void checkEdges() {

        if ((location.x > width) || (location.x < 0)) {

          velocity.x = velocity.x * -1;

        } 

        if ((location.y > height) || (location.y < 0)) {

          velocity.y = velocity.y * -1;

        }

      }

    }