2016年3月24日 星期四

WEEK04_02160136_陳威志

1.偵測食指

完整程式碼
import de.voidplus.leapmotion.*;

LeapMotion leap;

void setup()
{
   size(800,600); //視窗大小
   leap = new LeapMotion(this); // 宣告leapmotion
}

void draw()
{
   background(255); //背景顏色

for(Hand hand : leap.getHands())
{
  Finger finger_1=hand.getIndexFinger(); // 偵測手指
  finger_1.draw(); 
}

}



2.手指換成方塊
增加程式碼
  PVector pos = finger_1.getPosition(); //偵測手指位置
  PVector dir = finger_1.getDirection(); //偵測手指

  lights(); //打光
  fill(255,255,0);//填滿顏色在方塊
  translate(width/2,height/2); //移動方塊
  rotateY(radians(frameCount)); //讓方塊旋轉
  box(300,100,100); // 畫方塊


完整程式碼
import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup()
{
  size(800,600,P3D);
  leap = new LeapMotion(this);
}

void draw()
{
  background(255);
for(Hand hand : leap.getHands())
{
  Finger finger_1=hand.getIndexFinger();
  finger_1.draw(); 
  PVector pos = finger_1.getPosition();
  PVector dir = finger_1.getDirection();
}
  lights();
  fill(255,255,0);
  translate(width/2,height/2);
  rotateY(radians(frameCount));
  box(300,100,100);
}


1.變成球球
增加程式碼
  for(int i=0;i<10;i++)
  {
     if(dir!=null)translate(dir.x*10*i,dir.y*10*i,dir.z*10*i); //移動求的位置,就會變棒棒

      sphere(40); //橢圓形
  }

完整程式碼
import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup()
{
  size(800,600,P3D);
  leap = new LeapMotion(this);
}
PVector pos =null;
PVector dir = null;
void draw()
{
  background(255);
for(Hand hand : leap.getHands())
{
  Finger finger_1=hand.getIndexFinger();
  finger_1.draw(); 
 pos = finger_1.getPosition();
 dir = finger_1.getDirection();
}
  lights();
  fill(255,255,0);
  pushMatrix();
    translate(width/2,height/2);
  for(int i=0;i<10;i++)
  {
     if(dir!=null)translate(dir.x*10*i,dir.y*10*i,dir.z*10*i);
      sphere(40); 
  }
  popMatrix();
}



















2.將球射出去
增加程式碼
PVector[]bullet=new PVector[10]; //宣告陣列
PVector[]bullet_dir=new PVector[10];//宣告陣列
int bulletnow=0;//子彈是第幾顆

  pushMatrix();//保護
    fill(0,0,255);
    for(int i=0;i<10;i++)
    {
        pushMatrix();
        translate(bullet[i].x,bullet[i].y,bullet[i].z);
        sphere(45);
        bullet[i].x += bullet_dir[i].x;//子彈的x位置
        bullet[i].y += bullet_dir[i].y;//子彈的y位置
        bullet[i].z += bullet_dir[i].z;//子彈的z位置
        popMatrix();
    }
    popMatrix();//保護
}

void keyPressed()//按下空白建
{
//增加xyz 所以會有射出去的感覺
  bullet_dir[bulletnow].x=dir.x*5; 
  bullet_dir[bulletnow].y=dir.y*5;
  bullet_dir[bulletnow].z=dir.z*5;
  bulletnow++;
}


完整程式碼
import de.voidplus.leapmotion.*;
LeapMotion leap;
PVector pos =null;
PVector dir = null;
PVector[]bullet=new PVector[10];
PVector[]bullet_dir=new PVector[10];
int bulletnow=0;
void setup()
{
  size(800,600,P3D);
  leap = new LeapMotion(this);
  for(int i=0;i<10;i++)
  {
    bullet[i]=new PVector(0,0,0);
    bullet_dir[i]=new PVector(0,0,0);
  }
}

void draw()
{
  background(255);
for(Hand hand : leap.getHands())
{
  Finger finger_1=hand.getIndexFinger();
  finger_1.draw(); 
 pos = finger_1.getPosition();
 dir = finger_1.getDirection();
}
  lights();
  fill(255,255,0);
  translate(width/2,height/2);
  pushMatrix();
  sphere(40);
  popMatrix();
  
  pushMatrix();
    fill(0,0,255);
    for(int i=0;i<10;i++)
    {
        pushMatrix();
        translate(bullet[i].x,bullet[i].y,bullet[i].z);
        sphere(45);
        bullet[i].x += bullet_dir[i].x;
        bullet[i].y += bullet_dir[i].y;
        bullet[i].z += bullet_dir[i].z;
        popMatrix();
    }
    popMatrix();
}
void keyPressed()
{
  bullet_dir[bulletnow].x=dir.x*5;
  bullet_dir[bulletnow].y=dir.y*5;
  bullet_dir[bulletnow].z=dir.z*5;
  bulletnow++;
}


修正bug
//位置回歸
  bullet[bulletnow].x=0;
  bullet[bulletnow].y=0;
  bullet[bulletnow].z=0;

  bullet_dir[bulletnow].x=dir.x*5;
  bullet_dir[bulletnow].y=dir.y*5;
  bullet_dir[bulletnow].z=dir.z*5;
  bulletnow++;
//超過十,回歸一
  if(bulletnow>=10)
  {
   bulletnow=0; 
  }


1.碰撞測試

完整程式碼(碰觸後變紅色)
import de.voidplus.leapmotion.*;
LeapMotion leap;
PVector pos =null;
PVector dir = null;
PVector ball = null;

void setup()
{
  size(800,600,P3D);
  leap = new LeapMotion(this);
  pos = new PVector(300,200,0);
  ball = new PVector(0,0,0);
}

void draw()
{
  background(255);
  ball.x+=2;ball.y+=2;//球往斜對角射擊
for(Hand hand : leap.getHands())
{
  pos=hand.getPosition();
}

//將手指換成方塊
 fill(255,0,0);
 pushMatrix();
 translate(pos.x,pos.y,pos.z);
 box(30,30,30);
 popMatrix();

 //偵測碰撞
 pushMatrix();
 translate(ball.x,ball.y);
 sphere(30);
 if(dist(ball.x,ball.y,pos.x,pos.y)<30)background(255,0,0);
 popMatrix();
}

沒有留言:

張貼留言