【问题标题】:SCNNode Look At Current Position and Always Orient Relative UpSCNNode 查看当前位置并始终相对向上
【发布时间】:2019-06-29 19:05:00
【问题描述】:

问题:

我试图让一个节点查看手机,无论手机的方向如何,节点的顶部都始终指向手机的北极。让我们假设节点位于中心并且电话从(0, 0, 10) 开始,并假设节点具有类似于电话的矩形几何形状。我希望该矩形的顶部始终相对于我所在的位置指向“向上”。

所以我们有

let node = SCNNode()
node.position = SCNVector3(0, 0, 0)

self.sceneView.pointOfView.position = (0, 0, 10)

//The top of the node should be pointing towards (0, 100, 0) and 
//should be facing the phone.

现在让我们转到self.sceneView.pointOfView.position = (10, 0, 0),因此我们希望节点跟随并围绕 Y 轴旋转 90 度。

node.eulerAngles.y = Float(90*Double.pi/180)

现在让我们将手机移到self.sceneView.pointOfView.position = (0, 10, 0)的位置

我们最初围绕 Y 轴旋转了 90 度,现在我们从当前位置开始围绕原始 X 轴或当前 Z 轴添加一个旋转。 (我这么说是因为我愿意接受有关如何解决这个问题的建议,并且我希望尽可能多地理解这一点)。

现在我们有了

node.eulerAngles.x = Float(90*Double.pi/180)

并且我们的节点相对于我们的位置指向“向上”;也就是说,我们在节点的前面,并且节点根据我们过去的变换指向“向上”。

我的努力:

我尝试使用node.look(at:, up:, localFront:) 无济于事。我试过了

node.look(at: self.sceneView.pointOfView!.position, up: self.sceneView.pointOfView.worldUp, localFront: SCNNode.localFront)

但这会导致node 在我旋转手机时移动,我不希望这种情况发生。当我从横向旋转到纵向时,我不希望 node 移动,也不希望 node 旋转。

问题:

如何使节点始终从我的位置指向上方并始终看着我,而在我旋转手机时不移动、旋转或转动?我只想要 X 和 Y 运动。我愿意接受任何建议。

【问题讨论】:

  • 嗨,你解决了吗?
  • 我做到了!让我今晚某个时候回到这个话题。目前上午 9:30 在这里
  • 回答了下面的问题!

标签: ios swift rotation arkit scnnode


【解决方案1】:
func carouselLookAtMe() {
    //Here we use tangent lines on a sphere, relating to the POSITIVE Y Axis, to find the point at which the up direction is.
    let currentPos = self.sceneView.pointOfView!.position
    let D:Float = pow(currentPos.x, 2) + pow(currentPos.y, 2) + pow(currentPos.z, 2)

    let newPos = SCNVector3(0, abs(Float(D/Float(currentPos.y))), 0)
    let xDiff = currentPos.x-self.carousel.position.x
    let yDiff = currentPos.y-self.carousel.position.y
    let zDiff = currentPos.z-self.carousel.position.z

    let upVectorPos = SCNVector3(newPos.x-xDiff, newPos.y-yDiff, newPos.z-zDiff)
    self.carousel.look(at: self.sceneView.pointOfView!.position, up: upVectorPos, localFront: SCNVector3(0, 0, 1))
}

注意:self.carousel 是我要查看的节点。相对而言,它在向上定向时一直看着我。

我在里面叫这个func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor)

【讨论】:

    猜你喜欢
    • 2019-10-29
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多