2016年3月10日 星期四

Week03_Leap Motion偵測兩隻手_02160421黃志楷

第三週課程:Leap Motion偵測兩隻手

week03:Two Hands

第一節課(複習上星期並劃出3D空間)


1.下載Leap Motion 驅動程式並安裝


2.網路出點問題,用Proxy


Google設定 -> 變更Proxy -> LAN設定 -> 將Proxy伺服器打勾 

-> 並輸入位址:proxy.mcu.edu.tw 連接阜:3128





3.複習上星期的進度

記得要將Processing下載 LeapMotion for Processing的library

(利用Leap Motion偵測五隻手指頭,畫出有殘影的線)




4.畫出3D空間

增加三行


 translate(width/2,height/2); //將初始點移到中間

 rotateY(radians(frameCount));//開始對Y軸旋轉
 translate(-width/2,-height/2);//將初始點移到中間

程式碼


import de.voidplus.leapmotion.*;

LeapMotion leap;
PVector [][] p=new PVector[5][10];
void setup()
{
 size(800,600,P3D); 
 for(int f=0;f<5;f++)
 {
    for(int i=0;i<10;i++){ p[f][i]=new PVector(0,0,0);}
 }

 leap = new LeapMotion(this);
 colorMode(HSB,100);
}
void draw()
{
  background(0);
  for(int f=0;f<5;f++)
  {
      for(int i=5;i>0;i--){p[f][i].x=p[f][i-1].x;p[f][i].y=p[f][i-1].y;}
  }

  for(Hand hand : leap.getHands())
  {
    for(Finger finger : hand.getFingers())
    {
      int f=finger.getType();
      PVector now =finger.getPosition();
       p[f][0].x=now.x;
       p[f][0].y=now.y;
    }
  }
  
 translate(width/2,height/2); //將初始點移到中間
 rotateY(radians(frameCount));//開始對Y軸旋轉
 translate(-width/2,-height/2);//將初始點移到中間
    for(int f=0;f<5;f++)
  {
    stroke(f*20,100,100);
       for(int i=1;i<5;i++){line(p[f][i].x , p[f][i].y , p[f][i].z , p[f][i-1].x , p[f][i-1].y , p[f][i-1].z);}
  }
}


                                         第二節課(完成3D空間)



1.完成3D空間 並畫出方格來對比

增加

畫方格

  stroke(60, 100, 100);
  for (int i=0; i<800; i+=20)
  {
    for (int j=0; j<600; j+=20)
    {
      noFill();
      rect(i, j, 20, 20);
    }
  }

寫出數值

  if (f==1)println(now);

畫出z

p[f][0].z=now.z*2-100;


import de.voidplus.leapmotion.*;

LeapMotion leap;
PVector [][] p=new PVector[5][10];
void setup()
{
  size(800, 600, P3D); 
  for (int f=0; f<5; f++)
  {
    for (int i=0; i<10; i++) { 
      p[f][i]=new PVector(0, 0, 0);
    }
  }

  leap = new LeapMotion(this);
  colorMode(HSB, 100);
}
void draw()
{
  background(0);
  for (int f=0; f<5; f++)
  {
    for (int i=9; i>0; i--) {
      p[f][i].x=p[f][i-1].x;
      p[f][i].y=p[f][i-1].y;
    }
  }

  for (Hand hand : leap.getHands ())
  {
    for (Finger finger : hand.getFingers ())
    {
      int f=finger.getType();
      PVector now =finger.getPosition();
      p[f][0].x=now.x;
      p[f][0].y=now.y;
      
      if (f==1)println(now);
    }
  }

  translate(width/2, height/2, 0);
  rotateY(radians(frameCount/4.0));
  translate(-width/2, -height/2, 0);

  for (int f=0; f<5; f++)
  {
    
    for (int i=1; i<10; i++) {
      stroke(f*20, 100, 100);
      line(p[f][i].x, p[f][i].y, p[f][i].z, p[f][i-1].x, p[f][i-1].y, p[f][i-1].z);
    }
  }

  stroke(60, 100, 100);  
  for (int i=0; i<800; i+=20)
  {
    for (int j=0; j<600; j+=20)
    {
      noFill();
      rect(i, j, 20, 20);
    }
  }
}

'





                                         第三節課(做出調色盤)


1.偵測兩隻手並畫上橢圓跟方形


import de.voidplus.leapmotion.*;

LeapMotion leap;
void setup()
{
  size(800, 600, P3D); 
  leap = new LeapMotion(this);
}
void draw()
{
  background(0);

  for (Hand hand : leap.getHands ())
  {
    PVector pos =hand.getPosition();

    if(hand.isLeft()) //偵測左手
    {
      ellipse(pos.x,pos.y,200,100); //畫橢圓
    }
    else if(hand.isRight())  //偵測右手

    {
      rect(pos.x,pos.y,10,100); 橢圓
    }
  }
}




2.可以把調整畫筆顏色

增加初始值

eftpos=new PVector(0,0,0); //要記得給初始值,否則會當機



import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup()
{
  size(800, 600, P3D); 
  leap = new LeapMotion(this);
  leftpos=new PVector(0,0,0); //要記得給初始值,否則會當機
}
color nowc=color(255, 0, 0);
PVector leftpos;
void draw()
{
  background(255);

  for (Hand hand : leap.getHands ())
  {
    PVector pos =hand.getPosition();
    if (hand.isLeft())
    {
      noFill();  //畫顏料
      ellipse(pos.x, pos.y, 200, 100);
      fill(255, 0, 0);  
      ellipse(pos.x+150, pos.y, 40, 40);
      fill(255, 255, 0);  
      ellipse(pos.x+120, pos.y-40, 40, 40);
      fill(0, 255, 0);  
      ellipse(pos.x+90, pos.y-60, 40, 40);
      fill(0, 0, 255);  
      ellipse(pos.x+60, pos.y-60, 40, 40);
      fill(255, 0, 255);  
      ellipse(pos.x+30, pos.y-60, 40, 40);
      leftpos=pos;
      
    } else if (hand.isRight())
    {
      fill(nowc); 
      rect(pos.x, pos.y, 10, 100);
      println(leftpos);
      println(pos);
      if (dist(pos.x, pos.y, leftpos.x+120, leftpos.y-40)<30)
 {
        nowc=color(255, 255, 0);
      }
    }
  }
}
















3.期中作品


想做出貓狗大戰



















沒有留言:

張貼留言