使用Sprite Kit我试图根据给定的角度设置SKPhysicsBody
移动,例如,如果你想让精灵向右移动,你可以指定1.571弧度。要将指定的角度转换为速度,我使用下面的方法将弧度转换为CGVector
。我从内存中实现的ORIGINAL版本具有将所有角度偏移90度的奇怪效果。 (即如果使用0度,则精灵向右移动(就像你指定90度时那样)
我已通过交换dx
和dy
分配在新版本中修复此问题。我的问题是为什么会发生这种情况,我在原版中是否有错(似乎有其他人在网上这样做)或者是否存在基于所使用的特定坐标系的某些原因。
// ORIGINAL
- (CGVector)convertAngleToVector:(CGFloat)radians {
CGVector vector;
vector.dx = cos(radians) * 10;
vector.dy = sin(radians) * 10;
NSLog(@"DX: %0.2f DY: %0.2f", vector.dx, vector.dy);
return vector;
}
// NEW, SWAPPED DX & DY
- (CGVector)convertAngleToVector:(CGFloat)radians {
CGVector vector;
vector.dy = cos(radians) * 10;
vector.dx = sin(radians) * 10;
NSLog(@"DX: %0.2f DY: %0.2f", vector.dx, vector.dy);
return vector;
}
注意:也在Sprite Kit顺时针旋转为负,到目前为止convertAngleToVector
正在进行正向顺时针旋转(即1.571弧度是正确的,它应该留在哪里)我可以做cos(radians*-1)
和sin(radians*-1)
但基于我交换dx和dy可能会有一些潜在的原因。
答案 0 :(得分:3)
是的,SpriteKit默认为右边。 Physics Collision示例项目通过实现此方法解决了这个问题:
- (CGFloat)shipOrientation
{
// The ship art is oriented so that it faces the top of the scene, but Sprite Kit's rotation default is to the right.
// This method calculates the ship orientation for use in other calculations.
return self.zRotation + M_PI_2;
}
然后,您可以通过调用以下内容来获取现有方向:
CGFloat shipDirection = [self shipOrientation];
然后从那里调整zRotation属性。
答案 1 :(得分:2)
从Sprite Kit Programming Guide(强调添加):
Sprite Kit还有一个标准的旋转约定。图4-2显示了极坐标约定。 0弧度的角度指定正x轴。正角度为逆时针方向。
在此坐标系中,指向右侧的零弧度角是正确的。如果要使用零角度直线向上(沿正y轴)并顺时针增加的系统,则需要在将角度转换为矢量之前转换角度。