***LeapMotion函式庫只能在Processing2.2.1使用(未更新)***
繼續上禮拜的程式。
Leap Motion 下載:https://www.leapmotion.com/setup
size(640, 480, P3D); //開啟3D深度
將初始點(0,0)移動到畫面中間,並自動旋轉。最後再將初始點
移回原始點。
translate(width/2, height/2);
rotateY(radians(frameCount));
translate(-width/2, -height/2);
---------------------------------------------------------------------------------
增加z軸的位移量
pt[f][0].z=now.z*3-135;
放慢旋轉速度
rotateY(radians(frameCount)/4);
最後畫出背後網格
stroke(60, 100, 100);
for (int i=0; i<640; i+=20)
{
for (int j=0; j<480; j+=20)
{
noFill() ; rect(i,j,20,20);
}
}
執行結果
最後程式碼
import de.voidplus.leapmotion.*;
LeapMotion leap;
PVector [][] pt = new PVector[5][50];
void setup() {
size(640, 480, P3D);
for (int f=0; f<5; f++) {
for (int i=0; i<50; i++) {
pt[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=49; i>0; i--) {
pt[f][i].x=pt[f][i-1].x;
pt[f][i].y=pt[f][i-1].y;
pt[f][i].z=pt[f][i-1].z;
}
}
for (Hand hand : leap.getHands ()) {
for (Finger finger : hand.getFingers ()) {
int f=finger.getType();
PVector now = finger.getPosition();
pt[f][0].x=now.x;
pt[f][0].y=now.y;
pt[f][0].z=now.z*3-135;
if (f==1) println(now);
}
}
translate(width/2, height/2 ,0);
rotateY(radians(frameCount)/4);
translate(-width/2, -height/2,0);
for (int f=0; f<5; f++) {
for (int i=1; i<50; i++)
{
strokeWeight(3);
stroke(f*20, 100, 100);
line(pt[f][i].x, pt[f][i].y, pt[f][i].z, pt[f][i-1].x, pt[f][i-1].y, pt[f][i-1].z);
}
}
stroke(60, 100, 100);
for (int i=0; i<640; i+=20)
{
for (int j=0; j<480; j+=20)
{
noFill() ; rect(i,j,20,20);
}
}
}
---------------------------------------------------------------------------------
調色盤設計
透過
hand.isLeft() / hand.isRight()分辨左右手
左手:調色盤
右手:畫筆
建立調色盤顏色
左手必須new 第一支伸出右手時才不會有bug
最後程式碼
import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup() {
size(640, 480, P3D);
leap = new LeapMotion(this);
leftPos = new PVector(0,0,0);
}
color nowColor = color(0, 0, 0);
PVector leftPos;
void draw() {
background(255);
for (Hand hand : leap.getHands ()) {
PVector pos = hand.getPosition();
if (hand.isLeft()) {
fill(0, 0, 0);
ellipse(pos.x, pos.y, 200, 100);
fill(255, 0, 0);
ellipse(pos.x+100, pos.y-50, 50, 50);
fill(255, 255, 0);
ellipse(pos.x+50, pos.y-75, 50, 50);
fill(0, 255, 0);
ellipse(pos.x, pos.y-100, 50, 50);
fill(0, 255, 255);
ellipse(pos.x-50, pos.y-75, 50, 50);
fill(0, 0, 255);
ellipse(pos.x-100, pos.y-50, 50, 50);
leftPos = pos;
} else if (hand.isRight()) {
fill(nowColor);
rect(pos.x, pos.y, 10, 100);
println(pos);
println(leftPos);
if (dist(pos.x, pos.y, leftPos.x+100, leftPos.y-50)<30) {
nowColor = color(255, 0, 0);
}
}
}
}
---------------------------------------------------------------------------------
期中作品想做:小朋友打雪仗類型的遊戲
沒有留言:
張貼留言