SCNNode 查看当前位置并始终相对向上
SCNNode Look At Current Position and Always Orient Relative Up
问题:
我正在尝试让节点查看 phone,节点的顶部始终指向 phone 的北极,无论 phone 是什么面向。假设节点位于中心并且 phone 从 (0, 0, 10)
开始,并假设节点具有类似于 phone 的矩形几何形状。我希望该矩形的顶部始终指向 "up" 相对于我所在的位置。
所以我们有
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)
现在让我们将 phone 移动到位置 self.sceneView.pointOfView.position = (0, 10, 0)
我们初始围绕 Y 轴旋转 90 度,现在我们从当前位置添加围绕原始 X 轴或当前 Z 轴的旋转。 (我这么说是因为我乐于接受有关如何解决这个问题的建议,我希望尽可能多地理解这一点)。
现在我们有
node.eulerAngles.x = Float(90*Double.pi/180)
并且我们的节点指向 "UP" 相对于我们的位置;也就是说,我们在节点的前面,并且节点根据我们过去的变换指向 "UP"。
我的努力:
我尝试使用 node.look(at:, up:, localFront:)
但无济于事。我试过
node.look(at: self.sceneView.pointOfView!.position, up: self.sceneView.pointOfView.worldUp, localFront: SCNNode.localFront)
但这会导致 node
在我旋转 phone 时移动,我不希望这种情况发生。当我从横向旋转到纵向时,我不希望 node
移动,也不希望 node
旋转。
问题:
怎么才能让它在我旋转phone时,节点始终从我的位置指向上方,并且一直看着我,不移动,不旋转,不转动?我只想要 X 和 Y 运动。我愿意接受任何建议。
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)
问题:
我正在尝试让节点查看 phone,节点的顶部始终指向 phone 的北极,无论 phone 是什么面向。假设节点位于中心并且 phone 从 (0, 0, 10)
开始,并假设节点具有类似于 phone 的矩形几何形状。我希望该矩形的顶部始终指向 "up" 相对于我所在的位置。
所以我们有
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)
现在让我们将 phone 移动到位置 self.sceneView.pointOfView.position = (0, 10, 0)
我们初始围绕 Y 轴旋转 90 度,现在我们从当前位置添加围绕原始 X 轴或当前 Z 轴的旋转。 (我这么说是因为我乐于接受有关如何解决这个问题的建议,我希望尽可能多地理解这一点)。
现在我们有
node.eulerAngles.x = Float(90*Double.pi/180)
并且我们的节点指向 "UP" 相对于我们的位置;也就是说,我们在节点的前面,并且节点根据我们过去的变换指向 "UP"。
我的努力:
我尝试使用 node.look(at:, up:, localFront:)
但无济于事。我试过
node.look(at: self.sceneView.pointOfView!.position, up: self.sceneView.pointOfView.worldUp, localFront: SCNNode.localFront)
但这会导致 node
在我旋转 phone 时移动,我不希望这种情况发生。当我从横向旋转到纵向时,我不希望 node
移动,也不希望 node
旋转。
问题:
怎么才能让它在我旋转phone时,节点始终从我的位置指向上方,并且一直看着我,不移动,不旋转,不转动?我只想要 X 和 Y 运动。我愿意接受任何建议。
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)