J.ドウ!
最初にちょっとしたコツがあります。デフォルトの位置として横になっている iPhone を使用する場合は、sceneKit で使用される軸が DeviceMotion で使用される軸と異なることに注意する必要があります。軸を確認します。

(出典:apple.com)
最初に設定する必要があるのは、カメラの位置です。SceneKit プロジェクトを開始すると、位置 (0、0、15) にカメラが作成されます。それには問題があります:
eulerAngles = (0,0,0) の値は、オブジェクトが平面 xz にあることを意味しますが、Z から見ている限り、横から見るだけです。iPhone が横になっているのと同じようにするには、カメラを上から見るように設定する必要があります。つまり、電話から見ているようなものになります(カメラのように、idk)
// create and add a camera to the scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
// place the camera
cameraNode.position = SCNVector3(x: 0, y: 15, z: 0)
// but then you need to make the cameraNode face the ship (the origin of the axis), rotating it
cameraNode.eulerAngles.x = -Float(M_PI)*0.5 //or Float(M_PI)*1.5
これで船を上から見ることができるので、最初の部分は完了です。次に、デバイスの回転で船を「静止」(地面に面した状態) に保つ必要があります。
//First we need to use SCNRendererDelegate
class GameViewController : UIViewController SCNSceneRendererDelegate{
private let motion = CMMotionManager();
...
次にviewDidLoadで:
//important if you remove the sceneKit initial action from the ship.
//The scene would be static, and static scenes do not trigger the renderer update, setting the playing property to true forces that:
scnView.playing = true;
if(motion.deviceMotionAvailable){
motion.startDeviceMotionUpdates();
motion.deviceMotionUpdateInterval = 1.0/60.0;
}
次に、更新メソッドに移動します
軸を見てください。sceneKit 軸と deviceMotion 軸を比較すると、Y 軸と Z 軸が「入れ替わっています」。Z は電話で上を向いていますが、 はシーンの横にあり、Y はシーンで上を向いていますが、電話では横になっています。したがって、それぞれ X 軸、Y 軸、Z 軸に関連付けられているピッチ、ロール、ヨーは、ピッチ、ヨー、ロールとして適用されます。
ロール値を正にしたことに注意してください。これは、「切り替えられた」何かがあるためです。視覚化するのはちょっと難しいです。デバイスの動きの Y 軸がシーンの Z 軸に相関していることを確認してください。この軸に沿って同じ方向 (たとえば時計回り) にオブジェクトを回転させると、軸の配置により反対方向に回転することになります。(ロールを負に設定して、それがどのようにうまくいかないかを確認できます)
func renderer(renderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {
if let rot = motion.deviceMotion?.attitude{
print("\(rot.pitch) \(rot.roll) \(rot.yaw)")
ship.eulerAngles.x = -Float(rot.pitch);
ship.eulerAngles.y = -Float(rot.yaw);
ship.eulerAngles.z = Float(rot.roll);
}
それが役立つことを願っています! じゃあ!