我需要确定用户是进行顺时针旋转手势还是逆时针。我有起始向量位置以及当前和上一个触摸点。虽然我认为起始向量可能没有太大用处,因为用户还可以在旋转过程中更改旋转方向。就像旋转x-box的d-pad一样。 为了像Dead Trigger 2开发人员那样实时演示想法,屏幕上没有按钮,只需使用手势在屏幕上旋转即可。 我该如何确定它?
我需要确定用户是进行顺时针旋转手势还是逆时针。我有起始向量位置以及当前和上一个触摸点。虽然我认为起始向量可能没有太大用处,因为用户还可以在旋转过程中更改旋转方向。就像旋转x-box的d-pad一样。 为了像Dead Trigger 2开发人员那样实时演示想法,屏幕上没有按钮,只需使用手势在屏幕上旋转即可。 我该如何确定它?
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
dot = lastVecX * vecInX + lastVecY * vecInY;
。如果 dot < 0,则角度在第二或第三象限。 - Blindman67ang = Math.asin(Math.min(1, Math.max(-1, x2 * y1 - y2 * x1))); return x2 * x1 + y2 * y1 < 0 ? ( ang < 0 ? -Math.PI - ang : Math.PI - ang ) : ang;
其中x1,y1
和x2,y2
是归一化向量。 - Blindman67