提前感谢任何人都能给予的帮助。
作为一名初学者并且具有较弱的几何技能,我发现自己存在以下问题。
我试图拥有一艘"船"由以内容速度在屏幕上移动的用户操纵。无论船舶指向什么方向,它的方向都是移动的(就像游戏中的15枚硬币一样)。用户可以触摸屏幕的右半部分以顺时针旋转船舶,或者向左旋转船舶逆时针旋转。如果你按住船继续旋转。
船正确旋转。然而,它移动的方向并不是我所期望的。每当我触摸屏幕的右侧或左侧(在模拟器中)时,船舶将改变其在45-180度之间移动的方向。我无法确定它的模式。我花了一天的时间没有成功地记录价值并试图在纸上进行数学计算。我哪里错了?
触摸通过以下touchesBegan和touchesEned代码处理并且似乎正确发生:
static const int dontRotate = 0;
static const int rotateClockwise = 1;
static const int rotateCounterClockwise = 2;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
if(touchLocation.x > screenWidth/2){
shipRotation = rotateClockwise;
}
if(touchLocation.x <= screenWidth/2){
shipRotation = rotateCounterClockwise;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
shipRotation = dontRotate;
}
我在更新中更新了船舶的旋转,方向和位置:
#define SK_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f)
#define SK_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f)
-(void)update:(CFTimeInterval)currentTime {
[self enumerateChildNodesWithName:@"player" usingBlock:^(SKNode *node, BOOL *stop) {
shipOffsetAngle = shipDirection + SK_DEGREES_TO_RADIANS(90);
shipLocation.x = shipLocation.x + speed * cos(SK_RADIANS_TO_DEGREES(shipOffsetAngle));
shipLocation.y = shipLocation.y + speed * sin(SK_RADIANS_TO_DEGREES(shipOffsetAngle));
node.position = shipLocation;
if (shipRotation == rotateClockwise) {
shipDirection = shipDirection - 0.01;
node.zRotation = shipDirection;
}
if (shipRotation == rotateCounterClockwise) {
shipDirection = shipDirection + 0.01;
node.zRotation = shipDirection;
}
}];
}
正如@ LearnCocos2D所建议的,我也尝试了内置的GLKit转换,但得到了相同的结果。
test1 = SK_DEGREES_TO_RADIANS(90);
test2 = 90 * (M_PI / 180);
test3 = GLKMathDegreesToRadians(90);
NSLog(@"test1 = %f", test1);
NSLog(@"test2 = %f", test2);
NSLog(@"test3 = %f", test3);
全部注销为1.570796
经过大量的忙碌后,我发现了自己的错误。下面,在第一行中,shipoffsetAngle以弧度为单位。在接下来的两条sin / cos线中,它尝试使用我设置的SK_DEGREES_TO_RADIANS将其乘以0.01745329252将其转换为弧度。 shipOffsetAngle = shipDirection + SK_DEGREES_TO_RADIANS(90);
shipLocation.x = shipLocation.x + speed * cos(SK_RADIANS_TO_DEGREES(shipOffsetAngle));
shipLocation.y = shipLocation.y + speed * sin(SK_RADIANS_TO_DEGREES(shipOffsetAngle));
下面,我刚拿出来并切换到GLKit中的GLKMathDegreesToRadians,现在一切正常。
shipOffsetAngle = shipDirection + GLKMathDegreesToRadians(90);
shipLocation.x = shipLocation.x + speed * cos(shipOffsetAngle);
shipLocation.y = shipLocation.y + speed * sin(shipOffsetAngle);