Physical Controller

We learned how to combine arduino and processing together to achieve the real interaction last class. Before it, I only know how to build a circuit to control sensors and code in processing individually. This time, I chose potentiometer as physical controller in my project to control picaman in my picman game. When you touch the different part on the potentiometer, the picmen will move toward different directions.

 

Here is the code.

import processing.serial.*;

Serial myPort;

float xPos, yPos, viz;
boolean start = false;
boolean goU = false;
boolean goD = false;
boolean goL = false;
boolean goR = false;

float xDir =0;
float yDir = 0;

void setup() {

  size(800, 600);
  println(Serial.list());
  String portName = "/dev/tty.usbserial-DA00VFL3";////

  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil('\n');

  xPos = width/2; //x position
  yPos = height/2; //y position
  
  restart();
}


float x, y;
float V;
float Vx, Vy; //horizontal speed; vertical speed
float Real;
float R;
float sphereR;
float spherex;
float spherey;
float Dxsphere;
float Dysphere;
float myArc = 0;
int myColor = 0;

//void setup() {
//  size(700, 700);
//  noStroke();
//  x = width/2;
//  y = height/2;
//  Real = 10;
//  spherex = random(0, width);
//  spherey = random(0, height);
//  sphereR = random(8, R);
//}
void draw() {
  myColor ++;
  background(0, myColor, 200);

  myArc += 0.02;
  if (myArc > PI/5) {
    myArc = 0;
  }
  V = 50/Real + 1;
  float Dx = xPos - x; //
  float Dy = yPos - y;
  float D = sqrt(sq(Dx)+sq(Dy)); // Using point position (x,y) to get the distance from origin yo the point.
  Vx = (V/D)*Dx;// horizontal speed is 
  Vy = (V/D)*Dy;
  float Dxsphere = spherex - x;
  float Dysphere = spherey - y;
  float sphereD = sqrt(sq(Dxsphere)+sq(Dysphere));

  // this is the chaser
  fill(250, 250, 0);
  ellipse(x, y, 2*R, 2*R);
 ellipse(xPos, yPos, 20, 20);
  fill(250, 250, 0);
  arc(x, y, 2*R, 2*R, myArc, 2*PI - myArc);
  fill(250, 0, 0);
  ellipse(x+3, y-19, 10, 10);

  x+=Vx;
  y+=Vy;

  // this is the chasee
  fill(250, 0, 0);
  ellipse(spherex, spherey, 2*sphereR, 2*sphereR);

  fill(250, 0, 0);
  arc(spherex, spherey, 2*sphereR, 2*sphereR, myArc, 2*PI - myArc);
  fill(250, 250, 0);
  ellipse(spherex+3, spherey-19, 10, 10);

  if (sphereD < abs(R) - abs(sphereR)) {
    Real = sqrt(sq(R)+sq(sphereR));
    spherex = random(0, width);
    spherey = random(0, height);
    sphereR = random(9, R);
  }
  if (R < Real) {
    R+= 2.5;
  }
  fill(255);

  xPos = xPos +xDir;
}

void serialEvent(Serial myPort) {
  String input = myPort.readString();
  //String[] numbers = split(input, ',');
  //float[] values = float(numbers);
  float myVal = float(input);
  //println(myVal);

  if (myVal >= 1 && myVal <= 600) {
    xDir = 1;
  } else if (myVal  >= 601 && myVal <= 1023) {
    xDir = -1;
  }
}







void keyPressed() {
  if (key == 'r') {
    restart();
  }
}
void restart() {
  x = width/2;
  y = height/2;
  Real = 10;
  R = Real;
  spherex = random(0, width);
  spherey = random(0, height);
  sphereR = random(3, R);
}

 

Leave a Reply