识别顺时针或逆时针旋转

Identify clockwise or anti clockwise rotation

我必须确定用户是在做顺时针旋转手势还是逆时针旋转手势。我有起始矢量位置以及当前和以前的触摸。虽然我认为起始矢量没有多大用处,因为如果用户也可以在两者之间改变旋转。那就是他可以将旋转从顺时针变为逆时针。就像旋转 x-box 的方向键一样。 对于像 Dead Trigger 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