2013-04-23 23 views
0

我目前在美術學士學位課程,最近我開始嘗試通過閱讀Greg Borenstein的「Making Things See」來學習編程。Kinect。我如何與多個質量中心合作,而不會相互影響其他功能?

我正在開發的作品是試圖追蹤多個質心,因爲觀衆在使用Kinect時在畫廊空間中移動。我希望觀衆在屏幕上留下痕跡,當他們靠近某些區域時,他們的接近將通過線或其他方式顯示出來。我設法做出一條線索,但一旦有人進入視線,他們的關鍵點就會突然相連。 '鄰近線'也似乎只適用於當前用戶,而不適用於以前的用戶。

我想我的問題真的歸結爲如何隔離每個新用戶,以便我可以創建適用於所有這些函數或類但不相互干擾的函數或類。

這裏的程序至今...

import processing.opengl.*; 
import SimpleOpenNI.*; 
import peasy.*; 

PeasyCam cam; 
SimpleOpenNI kinect; 

ArrayList<PVector> trails; 

Hotpoint piece; 

PVector currentPosition; 
PVector previousPosition; 

int pieceX = 0; 
int pieceY = 0; 
int pieceZ = 2000; 
int pieceSize = 500; 

void setup() { 
    size(1280, 680, OPENGL); 
    kinect = new SimpleOpenNI(this); 
    kinect.enableDepth(); 
    kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_NONE); 
    kinect.setMirror(true); 

    trails = new ArrayList(); 

    piece = new Hotpoint(pieceX, pieceY, pieceZ, pieceSize); 

    cam = new PeasyCam(this, 0, 0, 0, 1000); 
} 

void draw() { 
    background(255); 
    kinect.update(); 
    rotateX(radians(180)); 

    lights(); 

    stroke(0); 
    strokeWeight(3); 

    IntVector userList = new IntVector(); 
    kinect.getUsers(userList); 

    piece.draw(); 

    for (int i=0; i<userList.size(); i++) { 
    int userId = userList.get(i); 

    PVector positionCenter = new PVector(); 
    kinect.getCoM(userId, positionCenter); 

    trails.add(positionCenter); 

    createTrail(); 

    piece.check(positionCenter); 

    if(piece.check(positionCenter) == true) { 
     stroke(255, 0, 0); 
     line(positionCenter.x, positionCenter.y, positionCenter.z, 
      pieceX, pieceY, pieceZ); 
     stroke(0); 
    } 
    } 
} 

void createTrail() { 
    for (int e=1; e < trails.size(); e++) { 
    currentPosition = trails.get(e); 
    previousPosition = trails.get(e-1); 
    if (currentPosition.z < 1) { 
     trails.clear(); 
    } 
    else { 
     stroke(0); 
     line(previousPosition.x, previousPosition.y, previousPosition.z, 
     currentPosition.x, currentPosition.y, currentPosition.z); 
    } 
    } 
} 

這是HOTPOINT類部分...

class Hotpoint { 
    PVector center; 
    color fillColor; 
    color strokeColor; 
    int size; 
    int pointsIncluded; 
    int maxPoints; 
    boolean wasJustHit; 
    int threshold; 


    Hotpoint(float centerX, float centerY, float centerZ, int boxSize) { 
    center = new PVector(centerX, centerY, centerZ); 
    size = boxSize; 
    pointsIncluded = 0; 
    maxPoints = 1000; 
    threshold = 0; 

    strokeColor = color(random(255), random(255), random(255)); 
    fillColor = 0; 
    } 

    void setThreshold(int newThreshold){ 
    threshold = newThreshold; 
    } 

    void setMaxPoints(int newMaxPoints){ 
    maxPoints = newMaxPoints; 
    } 

    void setColor(float red, float blue, float green){ 
    fillColor = strokeColor = color(red, blue, green); 
    } 

    boolean check(PVector point) { 
    boolean result = false; 

    if (point.x > center.x - size/2 && point.x < center.x + size/2) { 
     if (point.y > center.y - size/2 && point.y < center.y + size/2) { 
     if (point.z > center.z - size/2 && point.z < center.z + size/2) { 
      result = true; 
      pointsIncluded++; 
     } 
     } 
    } 

    return result; 
    } 

    void draw() { 
    pushMatrix(); 
     translate(center.x, center.y, center.z); 
     shapeMode(LINES); 
     noFill(); 
     stroke(red(strokeColor), blue(strokeColor), green(strokeColor), 255); 
     box(size); 
    popMatrix(); 
    } 


    float percentIncluded() { 
    return map(pointsIncluded, 0, maxPoints, 0, 1); 
    } 


    boolean currentlyHit() { 
    return (pointsIncluded > threshold); 
    } 


    boolean isHit() { 
    return currentlyHit() && !wasJustHit; 
    } 

    void clear() { 
    wasJustHit = currentlyHit(); 
    pointsIncluded = 0; 
    } 
} 

任何幫助將非常感激!


編輯:

謝謝你這麼多的時間和你的答案@ jesses.co.tt但我一直無法理解它... 例如,是在環userList與用戶數組不一樣嗎? 我很擔心我一次只問一件事,所以我已經把它分解開來,試圖首先了解沒有人聯繫的多條路線圖。

import processing.opengl.*; 
import SimpleOpenNI.*; 
import peasy.*; 

SimpleOpenNI kinect; 
PeasyCam cam; 

ArrayList<PVector> trails1; 
ArrayList<PVector> trails2; 

PVector currentPosition; 
PVector previousPosition; 

void setup() { 
    size(1280, 800, OPENGL); 

    kinect = new SimpleOpenNI(this); 
    kinect.enableDepth(); 
    kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_NONE); 
    kinect.setMirror(true); 

    trails1 = new ArrayList(); 
    trails2 = new ArrayList(); 

    cam = new PeasyCam(this, 0, 0, 0, 1000); 
} 

void draw() { 
    kinect.update(); 
    rotateX(radians(180)); 
    background(255); 

    IntVector userList = new IntVector(); 
    kinect.getUsers(userList); 

    //println(userList); 

    for (int i=0; i<userList.size(); i++) { 
    int userId = userList.get(i); 
    //println(userId); 
    PVector positionCenter = new PVector(); 
    kinect.getCoM(userId, positionCenter); 

    stroke(0); 
    strokeWeight(10); 
    point(positionCenter.x, positionCenter.y, positionCenter.z); 

    if (userId == 1) { 

     trails1.add(positionCenter); 
     createTrail1(); 
    } 
    else if (userId == 2) { 
     trails2.add(positionCenter); 
     createTrail2(); 
    } 
    } 
} 

void createTrail1() { 
    for (int e=1; e < trails1.size(); e++) { 
    currentPosition = trails1.get(e); 
    previousPosition = trails1.get(e-1); 
    if (currentPosition.z < 1) { // [possibly x or y or all?] 
     trails1.clear(); 
    } 
    else { 
     // if (currentPosition.x != 0 || previousPosition.x != 0) { // [not working] 
     stroke(0); 
     line(previousPosition.x, previousPosition.y, previousPosition.z, 
     currentPosition.x, currentPosition.y, currentPosition.z); 
     //trails.clear(); 
    } 
    } 
} 

void createTrail2() { 
    for (int e=1; e < trails2.size(); e++) { 
    currentPosition = trails2.get(e); 
    previousPosition = trails2.get(e-1); 
    if (currentPosition.z < 1) { // [possibly x or y or all?] 
     trails2.clear(); 
    } 
    else { 
     // if (currentPosition.x != 0 || previousPosition.x != 0) { // [not working] 
     stroke(0); 
     line(previousPosition.x, previousPosition.y, previousPosition.z, 
     currentPosition.x, currentPosition.y, currentPosition.z); 
     //trails.clear(); 
    } 
    } 
} 

那麼,這會工作的兩個人,我可以寫一個很長的窘況方案,爲大型有限數量的人工作,但我真的希望是,它是動態的... 'if(userId == 1){',我希望它適用於每個人,然後在步道部分,需要有一系列新的路徑,以便每當新人進入時查看我會使用'void onNewUser(int userId){'或其他...?

+0

哎呀,我忘了,包括對HOTPOINT類,所以這是被編輯在現在! – basicallyright 2013-04-24 14:14:37

回答

0

據我所知,你需要製作一個可調整大小的用戶列表 - 這個列表已經由SimpleOpenNI提供給你 - 並且爲你找到的每個新用戶創建一個HotPoint類的新實例(或者將其刪除即消失)就快,因爲你是在已知用戶的列表中正確遍歷每個用戶...

...

您需要添加根據調整HotPoints的ArrayList找到的用戶數量...然後在找到新用戶時創建點的新實例。你現在擁有的方式是,你只有一個實例,所以如果有兩個用戶,它會連接它們 - 這就是你所看到的。

確保根據需要從ArrayList中刪除項目!

你真的很接近...... ;-)

所以......

// Global Variables 
ArrayList users; 

然後,在你的for循環在用戶...

// Add a new HotPoint if we have more users than Points 
if(i > users.size()) { 
    users.add(new HotPoint(...)); 
} 
// Delete From ArrayList if we have too many... 
else if(users.size() > userList.size()) { 
    users.remove(...); 
} 

// Cast and call methods 
HotPoint piece = (HotPoint) users.get(i); 
piece.check(positionCenter); 
piece.draw(); 
etc... 
+0

基本正確 - 爲您工作嗎? – 2013-04-26 15:17:19

+0

非常感謝您的回答@ jesses.co.tt 這絕對是令人鼓舞的,儘管我仍然無法理解這一切。 我已經編輯了這個問題,希望能夠將我的問題更多地分解下來...... 如果您可以進一步提供幫助,它會非常棒! – basicallyright 2013-05-04 15:54:33

相關問題