ARKit-最初将对象放置在ARSKView中,且与摄像头成特定的方向/角度

时间:2018-06-20 18:34:02

标签: ios swift arkit

我正在ARSKView中创建锚点和2d节点,如下所示:

func displayToken(distance: Float) {
        print("token dropped at: \(distance)")
        guard let sceneView = self.view as? ARSKView else {
            return
        }

        // Create anchor using the camera's current position
        if let currentFrame = sceneView.session.currentFrame {
            removeToken()

            // Create a transform with a translation of x meters in front of the camera
            var translation = matrix_identity_float4x4
            translation.columns.3.z = -distance
            let transform = simd_mul(currentFrame.camera.transform, translation)

            // Add a new anchor to the session
            let anchor = ARAnchor(transform: transform)
            sceneView.session.add(anchor: anchor)
        }
    }

func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
        // Create and configure a node for the anchor added to the view's session.
        if let image = tokenImage {
            let texture = SKTexture(image: image)
            let tokenImageNode = SKSpriteNode(texture: texture)
            tokenImageNode.name = "token"
            return tokenImageNode
        } else {
            return nil
        }
    }

这会将其恰好位于给定距离(z)的相机前面。我还想做的是获取一个对象的纬度/经度,计算一个角度或航向,以度为单位,然后从相机开始以该角度放置锚点/节点。我目前正在使用GMSGeometryHeading方法获取标题,该方法将获取用户当前的位置以及目标位置来确定标题。因此,当放下锚点时,我想将其朝着目标位置的经/纬向正确的方向放置。如何使用SpriteKit / ARKit实现此目标?

1 个答案:

答案 0 :(得分:0)

能请您澄清一下问题吗?

也许以下几行可以为您提供帮助。在那里,cameraNode使用基本的几何公式​​移动,并根据x和y坐标中的角度(在Euler中)倾斜移动

var angleEuler = 0.1
let rotateX = Double(cameraNode.presentation.position.x) - sin(degrees(radians: Double(angleEuler)))*10
let rotateZ = Double(cameraNode.presentation.position.z) - abs(cos(degrees(radians: Double(angleEuler))))*10
cameraNode.position = SCNVector3(x:Float(rotateX), y:0, z:Float(rotateX))

如果您希望物体落在镜头前,并且跌落的长度取决于度数,只需在几何公式“ Tan A = a / b”中计算“ a”的值并更新节点的“ presentation.position.y”

我希望这对您有帮助