//LAB 8
//Crhistian David Lucumi
//02/10/2014
//Codigo Processing
import controlP5.*;
import processing.serial.*;
PImage img;
int iW2, iH2;
int posX, posY;
int velocity, angle;
Serial serial;
String val;
void setup() {
serial = new Serial(this, Serial.list()[0], 9600);
// tamaƱo de la ventana
size(800, 600);
// carga la imagen en la variable
img = loadImage("spaceship.gif");
iW2 = img.width/2;
iH2 = img.height/2;
posX = (width/2)-iW2;
posY = (height/2)-iH2;
velocity = -5;
angle = 0;
}
void draw() {
// limpia la ventana
background(0);
posX += velocity * sin(radians(angle));
if (posX-iW2 < 0) posX = width;
if (posX-iW2 > width) posX = iW2;
posY += velocity * cos(radians(angle));
if (posY-iH2 < 0) posY = height;
if (posY-iH2 > height) posY = iH2;
// dibuja la imagen
pushMatrix();
translate(posX-iW2, posY-iH2);
rotate(radians(-angle));
image(img, -iW2, -iH2);
popMatrix();
Cambio();
}
void Cambio(){
if ( serial.available() > 0)
{ // If data is available,
val = serial.readString()+""; // read it and store it in val
}
println(val); //print it out in the console
if(val==null){val="0";}
if(val.indexOf("arriba")>=0){
velocity -= 5;
val="";
}
if(val.indexOf("abajo")>=0){
velocity += 5;
val="";
}
if(val.indexOf("izq")>=0){
angle += 5;
val="";
}
if(val.indexOf("der")>=0){
angle -= 5;
val="";
}
}