xcode 你如何使用 ARKit Swift 将物体附加到你的相机位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44531963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you attach an object to your camera position with ARKit Swift?
提问by JoshKopen
I have moving objects which I want to have be able to collide with me the player. I have the ability to launch objects from me by getting my current position/direction at that time, but I do not understand how to attach an object to me which will follow my positioning at all times.
我有移动的物体,我希望它们能够与我的玩家发生碰撞。我有能力通过当时获取我当前的位置/方向来从我这里发射物体,但我不明白如何将一个物体附加到我身上,它会始终跟随我的位置。
回答by rickster
In SceneKit, everything that can have a position in the scene is (attached to) a node. That includes not just visible objects, but also light sources and cameras. When you use ARSCNView
, there's still a SceneKit camera, but ARKit controls its position/orientation.
在 SceneKit 中,所有可以在场景中拥有位置的东西都是(附加到)一个节点。这不仅包括可见物体,还包括光源和相机。当您使用 时ARSCNView
,仍然有一个 SceneKit 相机,但 ARKit 控制它的位置/方向。
SceneKit nodes create a hierarchy: every node's position (and orientation etc) are relative to its parent node. If the parent node moves within the scene, its children move along with it so that they keep the same parent-relative positions. So, if you want something to always keep the same position relative to the camera, you should make that content a child of the camera node.
SceneKit 节点创建一个层次结构:每个节点的位置(和方向等)都相对于其父节点。如果父节点在场景内移动,它的子节点也会随之移动,以便它们保持相同的父节点相对位置。因此,如果您希望某些内容始终相对于相机保持相同的位置,则应该将该内容设为相机节点的子节点。
Even in scenes where you don't create a camera yourself — such as when SceneKit and ARKit manage the camera for you — you can get the node containing the current camera with the view's pointOfView
property. (Note: ARSCNView
is a subclass of SCNView
, most of whose useful API is defined by the SCNSceneRenderer
protocol.)
即使在你自己不创建相机的场景中——比如当 SceneKit 和 ARKit 为你管理相机时——你可以使用视图的pointOfView
属性获取包含当前相机的节点。(注:ARSCNView
是 的子类SCNView
,其大部分有用的 API 由SCNSceneRenderer
协议定义。)
You may have to wait until the session starts running to access the ARKit-managed camera node.
您可能需要等到会话开始运行才能访问 ARKit 管理的相机节点。
回答by Jimi
ARSCNView has a property 'pointOfView'. You can attach a child node to it.
ARSCNView 有一个属性“pointOfView”。您可以将子节点附加到它。
let ball = SCNSphere(radius: 0.02)
ballNode = SCNNode(geometry: ball)
ballNode?.position = SCNVector3Make(0, 0, -0.2)
sceneView.pointOfView?.addChildNode(ballNode!)
The node will follow your camera anywhere you go.
无论您走到哪里,该节点都会跟随您的相机。
回答by Ummar Ahmed
Following code work for me to get the current position(x,y,z coordinates) of the camera.
以下代码对我来说有效以获取相机的当前位置(x,y,z 坐标)。
let pov = sceneView.pointOfView
let position = pov?.position
let x = position?.x
let y = position?.y
let z = position?.z
回答by jeffslofish
You need to take Jimi's answer and give the ball a color or texture to see it:
你需要根据 Jimi 的回答,给球一个颜色或纹理才能看到它:
let ball = SCNSphere(radius: 0.02)
ball.firstMaterial?.diffuse.contents = UIColor.red
let ballNode = SCNNode(geometry: ball)
ballNode.position = SCNVector3Make(0, 0, -0.2)
self.sceneView.pointOfView?.addChildNode(ballNode)