我有一个 SceneKit SCNBox 立方体,它应该跟随我的 iphone 的所有 Action 。因此,如果我的 iPhone 横向旋转 90 或 360 度,立方体应该也旋转 90 或 360 度,如果我向前旋转我的手机,立方体应该像手机一样旋转。
我已经尝试使用 startDeviceMotionUpdatesUsingReferenceFrame,但它与我手机的移动不匹配 :(
motionManager.deviceMotionUpdateInterval = 1.0 / 60.0
motionManager.startDeviceMotionUpdatesUsingReferenceFrame(
CMAttitudeReferenceFrame.XArbitraryZVertical,
toQueue: NSOperationQueue.mainQueue(),
withHandler: { (motion: CMDeviceMotion!, error: NSError!) -> Void in
let currentAttitude = motion.attitude
let roll = Float(currentAttitude.roll)
let pitch = Float(currentAttitude.pitch)
let yaw = Float(currentAttitude.yaw)
self.arrRoll.append(roll)
self.arrPitch.append(pitch)
self.arrYaw.append(yaw)
cubeNode.eulerAngles = SCNVector3Make(roll, 0.0, 0.0)
cubeNode.eulerAngles = SCNVector3Make(0.0, 0.0, pitch)
cubeNode.eulerAngles = SCNVector3Make(0.0, yaw, 0.0)
})
最佳答案
您的代码片段有点断章取义,但让我震惊的是您(很可能是无意中)覆盖了节点的 eulerAngles
属性。
为什么你有 3 个单独的分配(只有最后一个会产生影响),而不是:
cubeNode.eulerAngles = SCNVector3Make(roll, yaw, pitch)
?
关于Swift - 使用 motionManager 加速度计、陀螺仪和磁力计来移动 SceneKit 节点,就像 iPhone 的移动一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31162286/