2017-02-10 75 views
1

我正在一个包含场景(SceneKit)的iOS/Swift项目。 我想要做的是在用户旋转手机时旋转SCNCameraSCNCamera运动同步到iPhone加速计

这里是我做了什么:

motionManager = CMMotionManager() 
    motionManager.accelerometerUpdateInterval = 0.1 

    motionManager.startAccelerometerUpdates(to: OperationQueue.main) { [weak self] (data: CMAccelerometerData?, error: Error?) in self?.update_rotation(p: (data?.acceleration)!) } 

,并在update_rotation功能:

camera_node.eulerAngles = SCNVector3Make(angle_x,angle_y,angle_z) 

camera_node是我SCNNode相机对象。根据加速度计数据计算angle_xangle_z。 (我只是将加速度计值乘以90,然后将其转换为弧度)。从指南针(北角)计算出angle_y

我的问题来自angle_z。这很奇怪,因为eulerAngles.z属性的行为取决于angle_y的值。换句话说,如果朝北方向看,一切都很好。 当我向南看时,angle_z需要倒置。 我不明白为什么。

回答

0
let motionManager = CMMotionManager() 
motionManager.deviceMotionUpdateInterval = 1.0/60.0 
if motionManager.isDeviceMotionAvailable { 
    motionManager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: { (devMotion, error) -> Void in 
     cameraNode.eulerAngles = SCNVector3(
      -Float((devMotion?.attitude.roll)!) - Float(M_PI_2), 
      Float((motionManager.deviceMotion?.attitude.yaw)!), 
      -Float((motionManager.deviceMotion?.attitude.pitch)!) 
     ) 
    })} 

我尝试这与iPad的

我的操场应用
+0

有完全相同的问题。我删除了SCNVector3中的两个减号,因为我正在使用Landscape - home left。如果我将手机向左/向右倾斜,当我向后看时,这不起作用 – Bob5421