使用 ARWorldTrackingConfiguration 从 ARKit 获取 6DOF 矢量

Get 6DOF vectors from ARKit with ARWorldTrackingConfiguration

在使用带有 ARWorldTrackingConfiguration 的 ARKit 时,是否可以读取当前的 6 个自由度运动值(例如平移和旋转向量)?

我指的是具有 6 个自由度的 ARWorldTrackingConfiguration,如 https://developer.apple.com/documentation/arkit/arworldtrackingconfiguration

中所述

我想获取设备移动的当前值,例如相对于原点(例如 AR 会话的起点)的平移和旋转矢量。

let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal
arSceneView.session.run(configuration)

这将为您提供 6DOF。请确保在四处移动之前检测到飞机。

您使用您的触摸位置在 ARKit 场景中移动您的对象。您可以通过光线追踪来实现这一点。这一切都基于您通过相机检测到的水平面,仅此而已。

let hitResult = sceneView.hitTest(touchLocation, types: .existingPlane)

这个 hitResult 数组将帮助您放置对象。 例如。

let velocity :CGPoint = recognizer.velocity(in: self.arSceneView)
self.objectModel.node.position.y = (self.objectModel.node.position.y + Float(velocity.y * -0.0001))

这就是你的翻译。 让翻译 = recognizer.translation(in: recognizer.view!)

    let x = Float(translation.x)
    let y = Float(-translation.y)

    let anglePan = (sqrt(pow(GLKMathDegreesToRadians(x),2)+pow(GLKMathDegreesToRadians(y),2)))
    var rotationVector = SCNVector4()
    rotationVector.x = -y
    rotationVector.y = x
    rotationVector.z = 0
    rotationVector.w = anglePan

    self.objectModel.node.rotation = rotationVector
    self.sphereNode.rotation = rotationVector

这就是您在 SceneKit 中的模型上的旋转。这些只是如何在 ARScene 中进行平移和旋转的示例。根据需要进行更改。

arSceneView.pointOfView 是你的相机。这个节点的旋转和位置变换应该给你设备的位置和旋转。

arSceneView.pointOfView?.transform // Gives you your camera's/device's SCN4Matrix transform
arSceneView.pointOfView?.eulerAngles // Gives you the SCNVector3 rotation matrix.
arSceneView.pointOfView?.position // Gives you the camera's SCNVector3 position matrix.

ARCamera 表示任何 ARKit 会话中的设备姿势。如果您是 运行 世界跟踪会话,相机的 transform 矩阵是旋转和平移变换的串联。 (并且该变换是相对于世界坐标原点的,它基于您开始会话时所在的位置。)如果您没有世界跟踪会话,则没有平移(变换只是一个旋转矩阵)。

如果您需要帮助将变换矩阵分解为 rotation/translation 向量,这不是 ARKit 特有的 — 如果您想了解它是如何工作的,请查看 common 3D graphics question。不过有一些捷径:

  • 平移向量是矩阵的最后一列(例如transform.columns.3
  • 您可以通过 ARCamera.eulerAngles 属性.
  • 获得表示为 pitch/roll/yaw 角度的旋转
  • 您可以通过将整个矩阵传递给 simd_quatf 初始值设定项来将旋转作为四元数。