2016年3月17日 星期四

Week04_許志遙_期中作品教學

1. 射擊遊戲範例
1-1. 首先用Processing找出食指
import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup(){
  size(600,400);
  leap = new LeapMotion(this);
}

void draw(){
  background(255);
  for(Hand hand : leap.getHands()){
    Finger finger_index = hand.getIndexFinger();
    finger_index.draw();        畫出食指
  }
}

1-2. 做出一個3D的方塊(之後要把方塊套到食指上)
import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup(){
  size(600,400,P3D);        //畫3D
  leap = new LeapMotion(this);
}

void draw(){
  background(255);
  for(Hand hand : leap.getHands()){
    Finger finger_index = hand.getIndexFinger();
    finger_index.draw();
    PVector pos = finger_index.getPosition();        //食指位置
    PVector dir = finger_index.getDirection();        //食指方向
    println(pos);
    println(dir);
  }
  //lights();
  fill(255,0,0);
  translate(width/2, height/2);
  rotateY(radians(frameCount));        //自轉
  box(300,100,100);
}

1-3. 做出圓球會隨著手轉、移動
import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup(){
  size(600,400,P3D);
  leap = new LeapMotion(this);
}
PVector pos=null,dir=null;
void draw(){
  background(255);
  for(Hand hand : leap.getHands()){
    Finger finger_index = hand.getIndexFinger();
    finger_index.draw();
    pos = finger_index.getPosition();
    dir = finger_index.getDirection();
  }
  lights();
  fill(255,0,0);
  pushMatrix();
    translate(width/2, height/2);
    for(int i=0;i<10;i++){          //*10次
      if(dir!=null) translate(dir.x*10*i, dir.y*10*i,dir.z*10*i);        //如果方向不為空,就會改變
      sphere(40);        //畫出圓
    }
  popMatrix();
}

1-4. 會發射子彈

import de.voidplus.leapmotion.*;
LeapMotion leap;
PVector pos=null,dir=null;
PVector[] bullet=new PVector[10];          //子彈陣列(位置)
PVector[] bullet_dir=new PVector[10];        //子彈陣列(方向)
int bulletNow=0;        //子彈數量

void setup(){
  size(600,400,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_index = hand.getIndexFinger();
    finger_index.draw();
    dir = finger_index.getDirection();
  }
  lights();
  fill(255,0,0);
  translate(width/2,height/2);
  pushMatrix();
    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();

  pushMatrix();        //子彈
    fill(0,0,255);
    for(int i=0;i<10;i++){
      pushMatrix();
        translate(bullet[i].x, bullet[i].y, bullet[i].z);        //位置
        sphere(41);
        bullet[i].x += bullet_dir[i].x;       //轉動x,y,z
        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,發10顆會當掉

2-1打棒球
import de.voidplus.leapmotion.*;
LeapMotion leap;
PVector pos, dir, ball;

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

void draw(){
  ball.x +=2;
  ball.y +=2;
  for(Hand hand : leap.getHands()){
    pos = hand.getPosition();
  }  
  background(255);
  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);
  popMatrix();
}


沒有留言:

張貼留言