iphone - 找到 Sprite 的旋转方向,即顺时针或逆时针

标签 iphone ios vector cocos2d-iphone

我一直在为此自责,因为我认为这是一个简单的问题,但不知何故我就是找不到解决方案。

我正在根据触摸移动事件围绕其 anchor 旋转 CCSprite。这是我用来旋转 Sprite 的代码。

CGPoint previousLocation = [touch previousLocationInView:[touch view]];
CGPoint newLocation = [touch locationInView:[touch view]];

//preform all the same basic rig on both the current touch and previous touch
CGPoint previousGlLocation = [[CCDirector sharedDirector] convertToGL:previousLocation];
CGPoint newGlLocation = [[CCDirector sharedDirector] convertToGL:newLocation];

CCSprite *handle = (CCSprite*)[_spriteManager getChildByTag:HANDLE_TAG];

CGPoint previousVector = ccpSub(previousGlLocation, handle.position);
CGFloat firstRotateAngle = -ccpToAngle(previousVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);

CGPoint currentVector = ccpSub(newGlLocation, handle.position);
CGFloat rotateAngle = -ccpToAngle(currentVector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);

float rotateAmount = (currentTouch - previousTouch);
handle.rotation += rotateAmount;

我需要以某种方式找到旋转方向,即顺时针或逆时针方向。我尝试通过在 rotateAmount 值上设置“if”条件来做到这一点。但这在特定情况下不起作用。

例如。如果用户沿顺时针方向旋转 Sprite ,在这种情况下,rotateAmount 值将为正值,范围为 0 - 359。当用户即将完成圆圈时,条件将不成立,方向将变为逆时针方向。

有没有更好的检测旋转方向的方法?

最佳答案

试试这个环绕式完整性检查:

float rotateAmount = (currentTouch - previousTouch);

if (rotateAmount > 180)
  rotateAmount -= 360;
else if (rotateAmount < -180)
  rotateAmount += 360;

handle.rotation += rotateAmount;

关于iphone - 找到 Sprite 的旋转方向,即顺时针或逆时针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15885441/

相关文章:

iOS 模拟器在 7.1 和 8.0 上显示不同的大小

c++ - 使用类类型中的字段定义类

c++ - malloc.c:2451 在使用 vector 的 std::vector 的程序中

r - 在 R 中列出多个向量上的唯一元素

ios - 是否可以在 iOS 中嵌套集合/表格 View ?

php - iPhone 主屏幕 Web 应用程序并保持登录状态

objective-c - 错误域=com.alamofire.error.serialization.response 代码=-1011 "Request failed: internal server error (500)"

iPhone模拟器无法启动?

ios - UICollectionView 初始化问题

iphone - 为自定义 UIBarButtonItem 设置文本