2016-01-14 152 views
1

我必須識別用戶是正在做順時針旋轉手勢還是逆時針旋轉。我已經開始Vector位置以及當前和之前的觸摸。雖然我認爲啓動向量不能太多使用,因爲如果用戶也可以改變它們之間的旋轉。那就是他可以從順時針旋轉到逆時針旋轉。就像旋轉x-box的d-pad一樣。 對於Dead Trigger 2開發者的做法,只需使用手勢在屏幕上進行旋轉即可。 我如何識別它?識別順時針或逆時針旋轉

回答

2

要確定2D輸入是順時針旋轉還是逆時針旋轉,請從旋轉起點找出兩個矢量的叉積並查看它是負還是正。一個矢量來自前一個輸入,另一個矢量來自當前輸入。

在僞代碼中。

centerX = screenCenterX; // point user is rotating around 
centerY = screenCenterY; 
inputX = getUserX(); // gets X and Y input coords 
inputY = getUserY(); // 
lastVecX = inputX - centerX; // the previous user input vector x,y 
lastVecY = inputY - centerY; //  

while(true){ // loop 
    inputX = getUserX(); // gets X and Y input coords 
    inputY = getUserY(); // 

    vecInX = inputX - centerX; // get the vector from center to input 
    vecInY = inputY - centerY; // 

    // now get the cross product 
    cross = lastVecX * vecInY - lastVecY * vecInX; 

    if(cross > 0) then rotation is clockwise 
    if(cross < 0) then rotation is anticlockwise 
    if(cross == 0) then there is no rotation 

    lastVecX = vecInX; // save the current input vector 
    lastVecY = vecInY; // 
} // Loop until the cows come home. 

爲了得到角度,你需要對矢量進行歸一化,然後叉積是角度變化的罪。

在僞代碼

vecInX = inputX - centerX; // get the vector from center to input 
vecInY = inputY - centerY; // 

// normalized input Vector by getting its length 
length = sqrt(vecInX * vecInX + vecInY * vecInY); 

// divide the vector by its length 
vecInX /= length; 
vecInY /= length; 

// input vector is now normalised. IE it has a unit length 

// now get the cross product 
cross = lastVecX * vecInY - lastVecY * vecInX; 

// Because the vectors are normalised the cross product will be in a range 
// of -1 to 1 with < 0 anticlockwise and > 0 clockwise 

changeInAngle = asin(cross); // get the change in angle since last input 
absoluteAngle += changeInAngle; // track the absolute angle 

lastVecX = vecInX; // save the current normalised input vector 
lastVecY = vecInY; //  
// loop 
相關問題