document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2. *NICOLAS ROMAN BUITRAGO
  3. *LABORATORIO 8: JOYSTICK
  4. *UNIVERSIDAD SANTIAGO DE CALI
  5. *SISTEMAS EMBEBIDOS
  6. */
  7. import processing.serial.*;
  8.  
  9.  
  10. PImage img;
  11. int iW2, iH2;
  12. int posX, posY;
  13. int velocity, angle;
  14. int velMax = -10;
  15. int velMin = 10;
  16. char motion;
  17. Serial serial;
  18.  
  19. void setup() {
  20.   // tamaƱo de la ventana
  21.   size(1000,800);
  22.   // carga la imagen en la variable
  23.   img = loadImage("spaceship03.gif");
  24.   iW2 = img.width/2;
  25.   iH2 = img.height/2;
  26.   posX = (width/2)-iW2;
  27.   posY = (height/2)-iH2;
  28.   velocity = -5;
  29.   angle = 0;
  30.   serial = new Serial(this, Serial.list()[0], 9600);
  31. }
  32.  
  33. void draw() {
  34.   // limpia la ventana
  35.   background(0);
  36.   posX += velocity * sin(radians(angle));
  37.   if (posX-iW2 < 0) posX = width;
  38.   if (posX-iW2 > width) posX = iW2;
  39.   posY += velocity * cos(radians(angle));
  40.   if (posY-iH2 < 0) posY = height;
  41.   if (posY-iH2 > height) posY = iH2;
  42.   // dibuja la imagen
  43.   pushMatrix();
  44.   translate(posX-iW2, posY-iH2);
  45.   rotate(radians(-angle));
  46.   image(img, -iW2, -iH2);
  47.   popMatrix();
  48.  
  49.   leerSerial();
  50.  
  51. }
  52.  
  53. void leerSerial() {
  54.   if(serial.available()>0){
  55.     motion = serial.readChar();
  56.     println(motion);
  57.     println(velocity);
  58.     if (motion == \'w\')
  59.     //la velocidad de avance no puede ser mayor a -10
  60.       if(velocity>velMax)
  61.       velocity -= 1;    
  62.     if (motion == \'s\')
  63.     //La velocidad de retroceso no puede ser mayor a 10
  64.       if(velocity<velMin)
  65.       velocity += 1;
  66.     if (motion == \'a\')
  67.       angle += 12;    
  68.     if (motion == \'d\')
  69.       angle -= 12;
  70.   }
  71. }
');