简体   繁体   English

一致的旋转速度,iOS精灵套件

[英]Consistent Rotation Speed, iOS Sprite Kit

I'd like to rotate a spaceship around the z axis with the SKAction rotation method, depending on touch coordinates. 我想使用SKAction旋转方法围绕z轴旋转飞船,具体取决于触摸坐标。 After a touch the spaceship's nose should point to the touch point. 触摸后,宇宙飞船的鼻子应指向触点。

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];

How can I adjust the duration so that the rotation speed is always the same (no matter how big the rotation angle is)? 如何调整持续时间以使旋转速度始终相同(无论旋转角度有多大)?

The problem is that the radian occurs in the interval from -pi to pi (because of atan2). 问题是弧度发生在从-pi到pi的区间(因为atan2)。 So the rotation depends on the quadrant touched and so does the speed. 因此旋转取决于触摸的象限,速度也是如此。

在此输入图像描述

I got it now: 我现在明白了:

I've translated the radian from [-Pi, Pi] to [0, 2Pi]. 我将弧度从[-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

Then I used the difference between the old and the new radian divided by a factor: 然后我用旧弧度和新弧度之间的差值除以一个因子:

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];
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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM