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

查看:34
本文介绍了如何获取相机相对于其方向的 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天全站免登陆