/**
*NICOLAS ROMAN BUITRAGO
*LABORATORIO 8: JOYSTICK
*UNIVERSIDAD SANTIAGO DE CALI
*SISTEMAS EMBEBIDOS
*/
import processing.serial.*;
PImage img;
int iW2, iH2;
int posX, posY;
int velocity, angle;
int velMax = -10;
int velMin = 10;
char motion;
Serial serial;
void setup() {
// tamaƱo de la ventana
size(1000,800);
// carga la imagen en la variable
img = loadImage("spaceship03.gif");
iW2 = img.width/2;
iH2 = img.height/2;
posX = (width/2)-iW2;
posY = (height/2)-iH2;
velocity = -5;
angle = 0;
serial = new Serial(this, Serial.list()[0], 9600);
}
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();
leerSerial();
}
void leerSerial() {
if(serial.available()>0){
motion = serial.readChar();
println(motion);
println(velocity);
if (motion == \'w\')
//la velocidad de avance no puede ser mayor a -10
if(velocity>velMax)
velocity -= 1;
if (motion == \'s\')
//La velocidad de retroceso no puede ser mayor a 10
if(velocity<velMin)
velocity += 1;
if (motion == \'a\')
angle += 12;
if (motion == \'d\')
angle -= 12;
}
}