我在SceneKit场景的中心位置(0,0,0)有一个相机 我围绕y轴旋转相机以显示场景的不同部分。
我想在屏幕的左/右侧显示箭头,以显示用户移动以查看目标对象的方向。
如何计算ScnNode是否位于视图方向右侧的左侧?
我知道:
答案 0 :(得分:6)
E.g。
let v = CGPoint(x: nodePosition.x - cameraPosition.x, y: nodePosition.y - cameraPosition.y);
let a = atan2(v.y, v.x) // Note: order of arguments
角度以弧度为单位,范围介于-π和π(-180到180度)之间。如果值为负,则节点位于摄像机矢量的左侧,正值表示节点位于右侧,零值表示节点位于正前方。
方便的是,您可以根据需要使用此角度实际执行旋转。只需将摄像机角度增加一定角度到节点即可。
E.g。
cameraRotation += a * 0.1
注意事项:
答案 1 :(得分:0)
使用projectPoint funcion有一种更简单的方法,可以在视图中将图像显示为2d元素,并根据它在哪里找到用户应该看到的位置
let sceneWidth:CGFloat = self.sceneView.frame.width
getIfHeIsLookingNew(sceneWidth: sceneWidth, nodePosition: self.sceneView.projectPoint(targetNode.position))
func getIfHeIsLookingNew(sceneWidth: CGFloat, nodePosition: SCNVector3){
if(nodePosition.z < 1){
if(nodePosition.x > (Float(sceneWidth))){
print("Look Right")
}else if(nodePosition.x < 0){
print("Look Left")
}else{
print("Correct")
return
}
}else if(nodePosition.x < 0){
print("Look Right")
}else{
print("Look Left")
}
}
}