2016-01-14 107 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 
相关问题