我正在做一个包含场景(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_x
和angle_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 中使用我的游乐场应用程序尝试这个