我的场景是设置场景>世界:sknode
>相机:SKSpriteNode
&gt;发货:override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touchLocation = touch.locationInNode(self)
let location = cameraNode.convertPoint(touchLocation, toNode: world)
moveCameraNode(CGPointsToVector(location, b: cameraNode.position))
let shipTouchPosition = scene?.convertPoint(touchLocation, fromNode: world)
let shipPosition = scene?.convertPoint(ship.position, fromNode: world)
let angle = atan2(shipTouchPosition!.y - shipPosition!.y, shipTouchPosition!.x - shipPosition!.x)
let magicNumber = CGFloat(M_PI_2)
ship.runAction(SKAction.rotateToAngle(angle - magicNumber, duration: 0.1, shortestUnitArc: true))
}
override func didFinishUpdate() {
centerOnNode(cameraNode)
}
func centerOnNode(node: SKNode) {
let cameraPositionInScene = node.scene?.convertPoint(node.position, fromNode: node.parent!)
node.parent!.position = CGPointsSubtract(node.parent!.position, b: cameraPositionInScene!)
}
我希望船只指向触摸的位置:
magicNumber.
所以,它现在可行但我不确定为什么我需要{{1}}
编辑:感谢@mundi在我身上发现了一个愚蠢的错误,这个错误掩盖了我对convertPoint做错的真正问题,我之后必须调整它。答案 0 :(得分:2)
角度以弧度计算。 360度是常数PI的2倍,或2 * M_PI
。在SpriteKit中,角度计算从右中心开始(相当于3 o&#39;时钟),对应于90度或一半PI,M_PI_2
。
80
读取弧度评估为1.46018
,非常接近M_PI_2
或1.57079
。这就是为什么这个数字看起来很神奇&#34;修复这种差异,即使它有点偏差。真正的魔法&#34;号码是CGFloat(M_PI_2)
。