2016年3月17日 星期四

同學期中作品示範

1.射擊
2.打棒球
先做出能偵測拇指的程式
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();
  }
}
========================================================================

畫出方塊加P3D改成3D
import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup(){
    size(600,400,P3D);
    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,255);////顏色
  translate(width/2,height/2);/////畫舞台中間
  rotateY(radians(frameCount));////讓他轉
  box(200,100,100);////大小
}
========================================================================

畫出雞雞
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();
  //println(pos);
  //println(dir);
  }
  lights();
  fill(255,0,255);
  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();
  //rotateY(radians(frameCount));
  //box(200,100,100);
}
========================================================================

可以用雞雞射子彈了!!!!!!!!!!!!!
import de.voidplus.leapmotion.*;
PVector pos=null,dir=null;
PVector []bullet =new PVector[100];////設100發子彈
PVector []bullet_dir =new PVector[100];
int bulletNow=0;
LeapMotion leap;
void setup(){
    size(600,400,P3D);
    leap = new LeapMotion(this);
    for(int i=0;i<100;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();
   pos = finger_index.getPosition();
   dir = finger_index.getDirection();
  //println(pos);
  //println(dir);
  }
  lights();
  fill(255,0,255);
  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<100;i++){
                                                                        pushMatrix();////設定子彈飛行狀態,注意別寫在外面的pp函式中
  translate(bullet[i].x,bullet[i].y,bullet[i].z);
  sphere(42);
  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*10;
bullet_dir[bulletNow].y=dir.y*10;
bullet_dir[bulletNow].z=dir.z*10;
bulletNow++;
}
修改程式改掉子彈射完的bug
void keyPressed(){
bullet[bulletNow].x=0;bullet[bulletNow].y=0;bullet[bulletNow].z=0;
bullet_dir[bulletNow].x=dir.x*10;
bullet_dir[bulletNow].y=dir.y*10;
bullet_dir[bulletNow].z=dir.z*10;
bulletNow++;
if(bulletNow>=100){
  bulletNow=0;
    }
========================================================================
來做打棒球八
首先做球棒跟球然後用dist的涵式作偵測判斷

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(300, 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);
  ball.x-=300;
  ball.y-=300;
  }
  popMatrix();
}

沒有留言:

張貼留言