2013-10-18 209 views
1

我想創建一個Multitouch遊戲手柄控制處理和使用它來控制遠程Arduino機器人。 我想在Processing上製作GUI併爲Android編譯它。遊戲手柄控制處理+ Android來控制Arduino機器人

這裏是GUI遊戲手柄進行處理我已創建至今:

float easing = 0.09; 
// start position 
int posX = 50; 
int posY = 200; 
// target position 
int targetX = 50; 
int targetY = 200; 

boolean dragging = false; 

void setup() 
{ 
    size(500,250); 
    smooth(); 
} 

void draw() 
{ 
background(255); 
if (!dragging) 
{ 
    // calculate the difference in position, apply easing and add to vx/vy 
    float vx = (targetX - (posX)) * easing; 
    float vy = (targetY - (posY)) * easing; 


    // Add the velocity to the current position: make it move! 
    posX += vx; 
    posY += vy; 
} 

if(mousePressed) 
{ 
    dragging = true; 
    posX = mouseX; 
    posY = mouseY; 
} 
else 
{ 
    dragging = false; 
} 

DrawGamepad(); 
DrawButtons(); 
} 

void DrawGamepad() 
{ 
    //fill(0,155,155); 
    //rect(0, 150, 100, 100, 15); 

    ellipseMode(RADIUS); // Set ellipseMode to RADIUS 

    fill(0,155,155); // Set fill to blue 
    ellipse(50, 200, 50, 50); // Draw white ellipse using RADIUS mode 

    ellipseMode(CENTER); // Set ellipseMode to CENTER 
    fill(255); // Set fill to white// 
    ellipse(posX, posY, 35, 35); // Draw gray ellipse using CENTER mode 
} 

void DrawButtons() 
{ 
    fill(0,155,155); // Set fill to blue 
    ellipse(425, 225, 35, 35); 
    ellipse(475, 225, 35, 35); 
    fill(255,0,0); // Set fill to blue 
    ellipse(425, 175, 35, 35); 
    ellipse(475, 175, 35, 35); 
} 

我已經意識到,可能是代碼將不支持Android上的多點觸摸事件,所以我想出了這個鏈接

上找到另一個代碼

Can Processing handle multi-touch?

所以這個項目的目的是創造去多點觸控手柄用來控制我的Arduino機器人。遊戲手柄應檢測按下哪個按鍵以及操縱桿的方向。

任何幫助表示讚賞。

+0

到底是什麼問題?爲什麼不能使用http://stackoverflow.com/questions/17166522/can-processing-handle-multi-touch上的代碼? –

+1

關鍵是我現在有兩個代碼。一個處理多玩偶事件,另一個處理遊戲手柄控制。 我想將所有代碼加入到一起,並在我的Android設備上運行它。我希望功能遊戲手柄完成並與多點觸控代碼結合使用,但我無法使其工作。我將不勝感激任何幫助。最好的問候 – Iker

回答

1

我想說的第一件事是您可能不需要實現任何多點觸控兼容性。

我沒有一個Android測試這個,但下面的代碼應該工作:

import android.view.MotionEvent; 

int TouchEvents; 

/* 
* xTouch and yTouch are arrays of the x and y coordinates of all fingers touching the screen, in arbitrary order. 
* e.g. xTouch[2] and yTouch[2] are the coordinates of the third arbitrarily ordered finger touching the screen. 
*/ 
float[] xTouch; 
float[] yTouch; 
int currentPointerId = 0; 
boolean printFPS; 

float easing = 0.09; 
// start position 
float posX = 50; 
float posY = 200; 
// target position 
float targetX = 50; 
float targetY = 200; 

boolean dragging = false; 

void setup() 
{ 
    size(500,250); 
    smooth(); 
    //size(displayWidth, displayHeight); 
    orientation(LANDSCAPE); 
    background(0, 255, 0); 
    fill(0, 0, 244); 
    rect(100, 100, 100, 100); 
    stroke(255); 

    // Initialize Multitouch x y arrays 
    xTouch = new float [10]; 
    yTouch = new float [10]; // Don't use more than ten fingers! 
} 

void draw() 
{ 
    background(255); 
    if (!dragging) 
    { 
    // calculate the difference in position, apply easing and add to vx/vy 
    float vx = (targetX - (posX)) * easing; 
    float vy = (targetY - (posY)) * easing; 


    // Add the velocity to the current position: make it move! 
    posX += vx; 
    posY += vy; 

    for (int i = 0; i < xTouch.length; i++) 
    { 
     ellipse(xTouch[i], yTouch[i], 150, 150); 
    } 
    } 
    DrawGamepad(); 
    DrawButtons(); 

    targetX = xTouch[currentPointerId]; 
    targetY = yTouch[currentPointerId]; 
} 

void DrawGamepad() 
{ 
    //fill(0,155,155); 
    //rect(0, 150, 100, 100, 15); 

    ellipseMode(RADIUS); // Set ellipseMode to RADIUS 

    fill(0,155,155); // Set fill to blue 
    ellipse(50, 200, 50, 50); // Draw white ellipse using RADIUS mode 

    ellipseMode(CENTER); // Set ellipseMode to CENTER 
    fill(255); // Set fill to white// 
    ellipse(posX, posY, 35, 35); // Draw gray ellipse using CENTER mode 
} 

void DrawButtons() 
{ 
    fill(0,155,155); // Set fill to blue 
    ellipse(425, 225, 35, 35); 
    ellipse(475, 225, 35, 35); 
    fill(255,0,0); // Set fill to blue 
    ellipse(425, 175, 35, 35); 
    ellipse(475, 175, 35, 35); 
} 

public boolean surfaceTouchEvent(MotionEvent event) 
{ 
    // Number of places on the screen being touched: 
    TouchEvents = event.getPointerCount(); 

    // If no action is happening, listen for new events else 
    for(int i = 0; i < TouchEvents; i++) 
    { 
    int pointerId = event.getPointerId(i); 
    xTouch[pointerId] = event.getX(i); 
    yTouch[pointerId] = event.getY(i); 
    float siz = event.getSize(i); 
    } 

    // ACTION_DOWN 
    if(event.getActionMasked() == 0) 
    { 
    print("Initial action detected. (ACTION_DOWN)"); 
    print("Action index: " +str(event.getActionIndex())); 
    } 
    // ACTION_UP 
    else if(event.getActionMasked() == 1) 
    { 
    print("ACTION_UP"); 
    print("Action index: " +str(event.getActionIndex())); 
    } 
    // ACTION_POINTER_DOWN 
    else if(event.getActionMasked() == 5) 
    { 
    print("Secondary pointer detected: ACTION_POINTER_DOWN"); 
    print("Action index: " +str(event.getActionIndex())); 
    } 
    // ACTION_POINTER_UP 
    else if(event.getActionMasked() == 6) 
    { 
    print("ACTION_POINTER_UP"); 
    print("Action index: " +str(event.getActionIndex())); 
    } 
    // 
    else if(event.getActionMasked() == 4) 
    { 

    } 

    // If you want the variables for motionX/motionY, mouseX/mouseY etc. 
    // to work properly, you'll need to call super.surfaceTouchEvent(). 
    return super.surfaceTouchEvent(event); 
} 
+1

感謝您的迴應瑞安。該代碼不是100%工作,但我會設法讓它工作。因此,我應該讓代碼在大圓圈內獲得操縱桿Button,並且在按下按鈕鍵的任何時候都可以執行操作。 – Iker