/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This file contains the background stuff for "... for 5 joysticks". You probably don't need to edit anything here EXCEPT if you want to change keys that are used for emulating the 5 joysticks. To do that, read the next section - it should explain everything. basically, this file contains 2 things: - keyboard functions : if EMULATE is set to true, these watch the keyboard to imitate 5 joysticks - serial functions : if EMULATE is set to false, we have 5 joysticks actually plugged in to a serial port. both talk to an object of Joysticks class : regardless of EMULATE, this class gets updated by one of the previous 2 chunks of code. The joysticks class always contains the current state of the 5 joysticks. The main program can ask it at any time for the joysticks' values. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ boolean EMULATE = true; // leave this as true unless you have 5 joysticks plugged into a device sending serial data to this program! int serialPortIndex = 2; // this isn't even used if EMULATE is true. if you do use the serial port, you'll need to know which port in the list yours is. int serialBaudRate = 19200; // ditto (see an example at http://processing.org/reference/libraries/serial/Serial.html) /************************************************************************************************** The next section contains the code for emulating the joysticks with a keyboard. An array of key states (keyIsPressed) is adjusted on each keyPressed or keyReleased call. Immediately after each keyPressed or keyReleased call, we look at the state of all the keys and translate them into values for the five "joysticks". Each joystick is described by 4 keys: UP, RIGHT, DOWN, LEFT. Joystick 1: 2,w,a,q Joystick 2: 4,r,d,e Joystick 3: 6,y,g,t Joystick 4: 8,i,j,u Joystick 5: 0,p,l,o If your keyboard is different from mine, you might want to change the keys. This can be done by changing the values of the array joystickMapping. Keep in mind that the joysticks are engineered to go in only one direction at a time, either UP, RIGHT, DOWN, or LEFT. It is physically impossible to go both UP and LEFT. (To do that, we'd need to buy more expensive joysticks!) For more information on this, see the hardware documentation. Anyway, this is the reason that doJoystickMapping will only allow one direction to be selected at a time for each joystick. If no directions are selected, then the joystick is in the middle, or NEUTRAL. ***************************************************************************************************/ boolean keyIsPressed[] = new boolean[128]; char joystickMapping[][] = {{'2','w','a','q'}, // joystick 1 (up, right, down {'4','r','d','e'}, // joystick 2 {'6','y','g','t'}, // joystick 3 {'8','i','j','u'}, // joystick 4 {'0','p','l','o'}};// joystick 5 // keyPressed is called every time a key is pressed on the keyboard. //if the key is held down, it's called over and over void keyPressed() { if (EMULATE) { keyIsPressed[key & 0x7f] = true; doJoystickMapping(); } if ( key == 'c' ) { collide = !collide; println("Collision turned " + (collide ? "on." : "off.")); } if ( key == 'z' ) reset(); } // keyReleased is called every time a key is released on the keyboard. void keyReleased() { if (EMULATE) { keyIsPressed[key & 0x7f] = false; doJoystickMapping(); } } // doJoystickMapping translates the keyIsPressed array into "states" for the 5 joysticks. void doJoystickMapping() { for (int i=0; i> 3; // mask out the upper 5 bits and shift right 3 places int theDirection = theByte & 0x07; joysticks.setJoystickPosition(theJoystick,theDirection); } void serialEvent(Serial port) { while (port.available()>0) { int inByte = port.read(); mapByteToJoystick(inByte); } }