一樣先下載安裝LeapMotion
還有解壓縮processing 再下載LeapMotion for Processing的library
1.抓出食指
要先抓出食指才能玩遊戲~
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();
}
}
2. 再加上方塊並旋轉
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,0);
translate(width/2,height/2); //位置
rotateY(radians(frameCount)); //旋轉
box(300,100,100); //畫方塊
}
這樣就會出現一個紅方塊再轉了~
第二節課囉
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++){
if(dir!=null)
translate(dir.x*10*i,dir.y*10*i,dir.z*10*i);
sphere(40);
}
popMatrix();
}
4.可以射子彈
手指還沒偵測到前是一顆藍色圓球
偵測到手指會出現砲管
按空白鍵可以發射藍色子彈
但陣列只有10 超過10個子彈會當掉
import de.voidplus.leapmotion.*;
PVector pos=null,dir=null;
PVector []bullet=new PVector[10];
PVector []bullet_dir=new PVector[10];
int bulletNow=0;
LeapMotion leap;
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;
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++;
}
5.射完10個後 會把之前射出去的子彈偷回來重射
import de.voidplus.leapmotion.*;
PVector pos=null,dir=null;
PVector []bullet=new PVector[10];
PVector []bullet_dir=new PVector[10];
int bulletNow=0;
LeapMotion leap;
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;
bullet[i].y+=bullet_dir[i].y;
bullet[i].z+=bullet_dir[i].z;
popMatrix();
}
popMatrix();
}
void keyPressed(){
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;
}
}
畫面永遠只會出現10顆子彈
第三節課囉
換做新的東西
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); //box會跟著手指移動
popMatrix();
pushMatrix();
translate(ball.x,ball.y);
sphere(30); //球會持續移動 因為前面x,y有+2
if(dist(ball.x,ball.y,pos.x,pos.y)<30)
background(255,0,0); //畫面變紅
popMatrix();
}
沒有留言:
張貼留言