如何获取相机相对于其方向的SCNVector3位置ARKit Swift [英] How to get the SCNVector3 position of the camera in relation to it's direction ARKit Swift

查看:107
本文介绍了如何获取相机相对于其方向的SCNVector3位置ARKit Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在相机前面附加一个对象,但问题是它始终与相机的初始方向有关.即使相机的方向是向上还是向下,如何调整/获取SCNVector3位置以将对象放置在前面?

I am trying to attach an object in front of the camera, but the issue is that it is always in relation to the initial camera direction. How can I adjust/get the SCNVector3 position to place the object in front, even if the direction of the camera is up or down?

这就是我现在的做法:

let ballShape = SCNSphere(radius: 0.03)
let ballNode = SCNNode(geometry: ballShape)
let viewPosition = sceneView.pointOfView!.position
ballNode.position = SCNVector3Make(viewPosition.x, viewPosition.y, viewPosition.z - 0.4)
sceneView.scene.rootNode.addChildNode(ballNode)

推荐答案

修改为可更好地回答该问题,因为已在评论中对其进行了澄清

Edited to better answer the question now that it's clarified in a comment

您仅使用相机的位置,因此,如果旋转相机,则不会影响球.

You are using only the position of the camera, so if the camera is rotated, it doesn't affect the ball.

您可以做的是获取球的变换矩阵,然后将其乘以摄影机的变换矩阵,这样球的位置将相对于摄影机的整个变换,包括旋转.

What you can do is get the transform matrix of the ball and multiply it by the transform matrix of the camera, that way the ball position will be relative to the full transformation of the camera, including rotation.

例如

let ballShape = SCNSphere(radius: 0.03)
let ballNode = SCNNode(geometry: ballShape)
ballNode.position = SCNVector3Make(0.0, 0.0, -0.4)
let ballMatrix = ballNode.transform
let cameraMatrix = sceneView.pointOfView!.transform
let newBallMatrix = SCNMatrix4Mult(ballMatrix, cameraMatrix)
ballNode.transform = newBallMatrix
sceneView.scene.rootNode.addChildNode(ballNode)

或者,如果您只希望SCNVector3位置,则可以准确回答您的问题(这样球将不会旋转):

Or if you only want the SCNVector3 position, to answer exactly to your question (this way the ball will not rotate):

...
let newBallMatrix = SCNMatrix4Mult(ballMatrix, cameraMatrix)
let newBallPosition = SCNVector3Make(newBallMatrix.m41, newBallMatrix.m42, newBallMatrix.m43)
ballNode.position = newBallPosition
sceneView.scene.rootNode.addChildNode(ballNode)


旧答案:

您仅使用相机的位置,因此当相机旋转时,它不会影响球.


Old Answer:

You are using only the position of the camera, so when the camera rotates, it doesn't affect the ball.

SceneKit使用节点的层次结构,因此,当一个节点是另一个节点的子"时,它遵循其父"节点的位置,旋转和比例.将一个物体附着到另一个物体(在本例中为相机)的正确方法是使其成为相机的子代".

SceneKit uses a hierarchy of nodes, so when a node is "child" of another node, it follows the position, rotation and scale of its "parent". The proper way of attaching an object to another object, in this case the camera, is to make it "child" of the camera.

然后,当您设置子"节点的变换的位置,旋转度或任何其他方面时,要相对于其父项进行设置.因此,如果将位置设置为SCNVector3Make(0.0, 0.0, -0.4),它将在其父级"翻译的顶部以Z为单位翻译-0.4单位.

Then, when you set the position, rotation or any other aspect of the transform of the "child" node, you are setting it relative to its parent. So if you set the position to SCNVector3Make(0.0, 0.0, -0.4), it's translated -0.4 units in Z on top of its "parent" translation.

因此,要满足您的需求,应该是:

So to make what you want, it should be:

let ballShape = SCNSphere(radius: 0.03)
let ballNode = SCNNode(geometry: ballShape)
ballNode.position = SCNVector3Make(0.0, 0.0, -0.4)
let cameraNode = sceneView.pointOfView
cameraNode?.addChildNode(ballNode)

这样,当摄​​像机旋转时,球正好跟随其旋转,但是与摄像机分开了-0.4个单位.

This way, when the camera rotates, the ball follows exactly its rotation, but separated -0.4 units from the camera.

这篇关于如何获取相机相对于其方向的SCNVector3位置ARKit Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆