【问题标题】:Pointing to right direction in SceneKit在 SceneKit 中指向正确的方向
【发布时间】:2017-07-03 18:58:54
【问题描述】:

目前我在 iOS 平台上遇到问题。基本上,我在二维环境中加载了一个指向东方的箭头(上面是北方,左边是西方,右边是东方,下面是南方)。我希望它可以在 3D 环境中指向真正的东方,以便它会自动旋转到正确的方向。我画了一幅画来准确地描述我的情况。 (虚线箭头是我加载的箭头,如果我使用核心运动数据,实线箭头是我想要的)

现在我做到了

let motionManager = CMMotionManager()
    motionManager.deviceMotionUpdateInterval = 1.0 / 60.0
    if motionManager.isDeviceMotionAvailable {
        motionManager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: { (devMotion, error) -> Void in
                parent_arrows[0].orientation = SCNQuaternion(-CGFloat((motionManager.deviceMotion?.attitude.quaternion.x)!), -CGFloat((motionManager.deviceMotion?.attitude.quaternion.y)!), -CGFloat((motionManager.deviceMotion?.attitude.quaternion.z)!), CGFloat((motionManager.deviceMotion?.attitude.quaternion.w)!))

        })}

sn-p 不能自动使箭头向右旋转。我的想法是获取设备与北方之间的角度,然后将此角度应用于箭头的方向。但是如何将角度添加到四元数?还有其他想法可以实现吗?

【问题讨论】:

  • 实际问题是什么? parent_arrows[0] 是否正在更新,但未显示更新? parent_arrows[0] 没有更新吗?是否正在更新和显示但显示方向错误?
  • 让问题变得简单,我加载了一个 3D 箭头,我希望这个箭头指向方向(例如:东)。我回答了下面的问题,但似乎万向节锁出现了。

标签: ios swift scenekit quaternions core-motion


【解决方案1】:

这个thread启发了我。

 let motionManager = CMMotionManager()
    motionManager.deviceMotionUpdateInterval = 1.0 / 60.0
    if motionManager.isDeviceMotionAvailable {
        motionManager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: { (devMotion, error) -> Void in
            parent_arrows[0].orientation = self.orient(q: (motionManager.deviceMotion?.attitude.quaternion)!)

        })}
}

func orient(q:CMQuaternion) -> SCNQuaternion{
    let gq1: GLKQuaternion = GLKQuaternionMakeWithAngleAndAxis(GLKMathDegreesToRadians(-heading), 0, 0, 1)
    // add a rotation of the yaw and the heading relative to true north
    let gq2: GLKQuaternion = GLKQuaternionMake(Float(q.x), Float(q.y), Float(q.z), Float(q.w))
    // the current orientation
    let qp: GLKQuaternion = GLKQuaternionMultiply(gq1, gq2)
    // get the "new" orientation
    var rq = CMQuaternion()
    rq.x = Double(qp.x)
    rq.y = Double(qp.y)
    rq.z = Double(qp.z)
    rq.w = Double(qp.w)
    return SCNVector4Make(-Float(rq.x), -Float(rq.y), -Float(rq.z), Float(rq.w))
}

执行此操作后,箭头将在开始时指向正确的方向。但是,它带来了我在另一个thread 中提出的另一个问题。最终答案在this

【讨论】:

  • 这个 sn-p 不能完美地工作。当我举起 iPhone 时,箭头只能在 X 和 Y 轴上旋转。 Z轴旋转消失。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-29
  • 2020-06-06
  • 2015-05-13
  • 1970-01-01
  • 2013-01-17
  • 2019-05-16
  • 1970-01-01
相关资源
最近更新 更多