SCNCamera运动与iPhone加速度计同步



我正在做一个包含场景(SceneKit(的iOS/Swift项目。我想做的是在用户旋转手机时旋转SCNCamera

这是我所做的:

    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需要颠倒过来。我不明白为什么。

    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 中使用我的游乐场应用程序尝试这个

最新更新