【问题标题】:ARKit – Is there a way to know where the object is located in space related to ARCamera?ARKit – 有没有办法知道物体在与 ARCamera 相关的空间中的位置?
【发布时间】:2019-12-12 19:23:05
【问题描述】:

我在 ARKit 的屏幕中有一个对象,我想知道它与相机方向之间的水平角度,或者至少是屏幕左侧、中间或右侧的对象?

【问题讨论】:

    标签: swift scenekit augmented-reality arkit triangulation


    【解决方案1】:

    肯定(很好)

    首先,我们需要了解 ARKit。

    1. ARKit 对象不在actual 空间中,而是基于相机所见。

    2. 相机并不完美;您会注意到“静止的”物体由于照明、环境等原因四处移动。

    3. 没有人的手是稳定的;不仅物体会移动,手机也会做一些小的调整。

    现在,提供这些信息,我们可以使用我们知道的变量来计算 3 个已知位置之间的角度(当然是在 3D 空间中!)。

    使用this answer,我们可以看到,通过大量数学运算,我们可以从 3 个 3D 点得出一个角度


    现在,进入它的代码。

    让我们得到我们的变量

    1. 您可以使用SCNSceneRendererDelegate 提供的函数获取您的phone 职位
    func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {
        guard let pointOfView = sceneView.pointOfView else { return }
        let transform = pointOfView.transform
        let orientation = SCNVector3(-transform.m31, -transform.m32, transform.m33)
        let location = SCNVector3(transform.m41, transform.m42, transform.m43)
        let currentPositionOfCamera = orientation + location
        print(currentPositionOfCamera)
    }
    
    func +(lhv:SCNVector3, rhv:SCNVector3) -> SCNVector3 {
         return SCNVector3(lhv.x + rhv.x, lhv.y + rhv.y, lhv.z + rhv.z)
    }
    
    1. 您可以通过以下方式获取您的object
    let anObject = SCNNode()
    let pos = anObject.position
    
    1. 您可以通过稍微有创意的方式获取您的userViewDirection——我能想到的最好方法是创建一个新的SCNNode,并将其放置在您面前一段距离。我们可以通过使用this link 来做到这一点
    func updatePositionAndOrientationOf(_ node: SCNNode, withPosition position: SCNVector3, relativeTo referenceNode: SCNNode) {
        let referenceNodeTransform = matrix_float4x4(referenceNode.transform)
    
        // Setup a translation matrix with the desired position
        var translationMatrix = matrix_identity_float4x4
        translationMatrix.columns.3.x = position.x
        translationMatrix.columns.3.y = position.y
        translationMatrix.columns.3.z = position.z
    
        // Combine the configured translation matrix with the referenceNode's transform to get the desired position AND orientation
        let updatedTransform = matrix_multiply(referenceNodeTransform, translationMatrix)
        node.transform = SCNMatrix4(updatedTransform)
    }
    
    //Create a global node 
    let lookingNode:SCNNode = SCNNode()
    
    //Now update node say `2` away in the Z (looking direction)
    let position = SCNVector3(x: 0, y: 0, z: -2)
    updatePositionAndOrientationOf(lookingNode, withPosition: position, relativeTo: cameraNode)
    

    现在我们有了 3 个变量。

    我们可以简单地对它们进行数学运算:)

    //Vertex is pos1
    func calculateAngleBetween3Positions(pos1:SCNVector3, pos2:SCNVector3, pos3:SCNVector3) -> Float {
        let v1 = SCNVector3(x: pos2.x-pos1.x, y: pos2.y-pos1.y, z: pos2.z-pos1.z)
        let v2 = SCNVector3(x: pos3.x-pos1.x, y: pos3.y-pos1.y, z: pos3.z-pos1.z)
    
        let v1Magnitude = sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z)
        let v1Normal = SCNVector3(x: v1.x/v1Magnitude, y: v1.y/v1Magnitude, v1.z/v1Magnitude)
      
        let v2Magnitude = sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z)
        let v2Normal = SCNVector3(x: v2.x/v2Magnitude, y: v2.y/v2Magnitude, v2.z/v2Magnitude)
    
        let result = v1Normal.x * v2Normal.x + v1Normal.y * v2Normal.y + v1Normal.z * v2Normal.z
        let angle = acos(result)
    
        return angle
    }
    

    【讨论】:

    • 什么是cameraNode?​​span>
    • 没关系,我想是sceneView.pointOfView
    猜你喜欢
    • 2017-12-20
    • 1970-01-01
    • 2016-11-16
    • 2017-12-18
    • 2012-12-14
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    相关资源
    最近更新 更多