2016年3月31日 星期四

02160120 week06

數據封包的陣列會爆炸 尚未改善
已經改善真實座標和虛擬座標的誤差

Client

//Client's code computer"1"
int myComputer=1;
import de.voidplus.leapmotion.*;
import processing.net.*;

Client client;
PImage ht;
int x=0, y=0, myX=395, myY=295,pX=395,pY=295;
int vx=1, vy=1;
int UDP[][] = new int[10][5];
int data[];
int delayTime=0;

void setup() {
  size(800, 600);
  ht = loadImage("ht.jpg");
  for(int i=0;i<10;i++)for(int j=1;j<5;j++)UDP[i][j]=0;
  client = new Client(this,"127.0.0.1",0124);
}

void draw() {
  background(0);
  image(ht, x, y, 1000, 800);
  mousePosition();
  myInformation();
  clientInformation();
  for(int i=0;i<10;i++)if(i!=myComputer)ellipse(UDP[i][1]+x, UDP[i][2]+y,10,10);
  text((UDP[0][1]+x) + " " + (UDP[0][2]+y),UDP[0][1]+x,UDP[0][2]+y);
}
void mousePosition() {

  fill(255, 0, 0);
  line(myX, myY, mouseX, mouseY);
  ellipse(mouseX, mouseY, 5, 5);

  fill(255);
  ellipse(myX, myY, 10, 10);

  text((myX-x) + " " + (myY-y), myX, myY);

  if (mouseY>myY+20)if (y>-200&&myY==295)y-=10;
  else if (myY<590)myY+=10;
  if (mouseY<myY-20)if (y<0&&myY==295)y+=10;
  else if (myY>10)myY-=10;
  if (mouseX<myX-20)if (x<0&&myX==395)x+=10;
  else if (myX>10)myX-=10;
  if (mouseX>myX+20)if (x>-200&&myX==395)x-=10;
  else if (myX<790)myX+=10;
}
void myInformation() {
  if(millis()-delayTime>50){
    client.write(myComputer + "," + (myX-x) + "," + (myY-y) + "\n");
    delayTime=millis();
  }
    pX=(myX-x);
    pY=(myY-y);
}

void clientInformation() {
  if (client.available()>0) {
    String input;
    input = client.readString();
//    input = input.substring(0, input.indexOf("\n"));  // Only up to the newline
    println(input);
    data = int(split(input, ','));  // Split values into an array
    for(int i=1;i<3;i++)UDP[data[0]][i]=data[i];
    // Draw line using received coords
    stroke(0);
  }
}

server
//Server's code computer"0"
int myComputer=0;
import de.voidplus.leapmotion.*;
import processing.net.*;
PVector []userPosition;
int UDP[][] = new int[10][5];
int data[];
String input;
PImage ht;
int x=0, y=0, myX=395, myY=295,pX=395,pY=395;
int vx=1, vy=1;
int delayTime=0;

Server server;
Client client;
void setup() {
  size(800, 600);
  ht = loadImage("ht.jpg");
  server = new Server(this, 0124);
  for(int i=0;i<10;i++)for(int j=1;j<5;j++)UDP[i][j]=0;
}
void draw() {
  image(ht, x, y, 1000, 800);
  myInformation();
  clientInformation();
  mousePosition();
  for(int i=0;i<10;i++)if(i!=myComputer)ellipse(UDP[i][1]+x, UDP[i][2]+y,10,10);
  text((UDP[1][1]+x) + " " + (UDP[1][2]+y),UDP[1][1]+x,UDP[1][2]+y);
}
void mousePosition() {

  fill(255, 0, 0);
  line(myX, myY, mouseX, mouseY);
  ellipse(mouseX, mouseY, 5, 5);

  fill(255);
  ellipse(myX, myY, 10, 10);

  text((myX-x) + " " + (myY-y), myX, myY);

  if (mouseY>myY+20)if (y>-200&&myY==295)y-=10;
  else if (myY<590)myY+=10;
  if (mouseY<myY-20)if (y<0&&myY==295)y+=10;
  else if (myY>10)myY-=10;
  if (mouseX<myX-20)if (x<0&&myX==395)x+=10;
  else if (myX>10)myX-=10;
  if (mouseX>myX+20)if (x>-200&&myX==395)x-=10;
  else if (myX<790)myX+=10;
}

void myInformation() {

    if(millis()-delayTime>50){
      server.write(myComputer + "," + (myX-x) + "," + (myY-y) + "\n");
      delayTime=millis();
    }
    pX=(myX-x);
    pY=(myY-y);
}

void clientInformation() {
  client = server.available();
  if (client != null) {
    String input;
    input = client.readString();
//    input = input.substring(0, input.indexOf("\n"));  // Only up to the newline
    println(input);
    data = int(split(input, ','));  // Split values into an array
    for(int i=1;i<3;i++)UDP[data[0]][i]=data[i];
    // Draw line using received coords
    stroke(0);

  }
}

沒有留言:

張貼留言