使用 Sprite Kit 我试图设置一个SKPhysicsBody
根据给定角度移动,例如,如果您希望精灵向右移动,您可以指定 1.571 弧度。为了将指定的角度转换为速度,我使用下面的方法将弧度转换为CGVector
。我根据记忆实现的原始版本具有将所有角度偏移 90 度的奇怪效果。 (即,如果使用 0 度,则精灵会向右移动(就像指定 90 度时一样)
问题:
我通过交换在新版本中修复了这个问题dx
and 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;
}
NOTE:同样在 Sprite Kit 中顺时针旋转也是负的,到目前为止convertAngleToVector
正在做正顺时针旋转(即 1.571 弧度是正确的,它应该在左边)我可以这样做cos(radians*-1)
and sin(radians*-1)
但基于我交换 dx 和 dy,这可能有一些根本原因。
Sprite 套件(SKView 坐标):
是的,SpriteKit 默认位于右侧。物理碰撞示例项目通过实现此方法解决了这个问题:
- (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 属性。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)