我希望使用SKAction旋转方法围绕z轴旋转飞船,具体取决于触摸坐标。触摸后,宇宙飞船的鼻子应该指向触点。
CGFloat rad = atan2f(touchPos.y - sprite.position.y, touchPos.x - sprite.position.x); // calc rad between vectors
SKAction *rotation = [SKAction rotateToAngle: rad duration: ??? shortestUnitArc:YES];
如何调整持续时间以使旋转速度始终相同(无论旋转角度有多大)?
问题是弧度发生在从-pi到pi的区间(因为atan2)。因此,旋转取决于触摸的象限,速度也是如此。
答案 0 :(得分:2)
我现在明白了:
我已将[-Pi,Pi]的弧度转换为[0,2Pi]。
CGFloat prevRad = sprite.zRotation; // actual sprite rotation
if (prevRad < 0) prevRad = (M_PI)+((M_PI)+prevRad); // atan2 from 0 to 2PI
CGFloat rad = atan2f(touchLocation.y - sprite.position.y, touchLocation.x - sprite.position.x); // calc rad between vectors
if (rad < 0) rad = (M_PI)+((M_PI)+rad); // atan2 from 0 to 2PI
然后我使用旧弧度和新弧度之间的差值除以因子:
CGFloat factor = 4.0f;
CGFloat diff = ABS(sprite.zRotation - rad);
CGFloat time = diff / factor;
SKAction *rotation = [SKAction rotateToAngle:rad duration: time shortestUnitArc:YES];
rotation.timingMode = SKActionTimingEaseInEaseOut;
[sprite runAction:rotation];
答案 1 :(得分:1)
CGFloat factor = 5.0; //Adjust this to get your desired speed.
CGFloat rad = atan2f(touchPos.y - sprite.position.y, touchPos.x - sprite.position.x); // calc rad between vectors
CGFloat diff = ABS(node.zRotation - rad);
CGFloat time = diff / factor;
SKAction *rotation = [SKAction rotateToAngle: rad duration: time shortestUnitArc:YES];