[英]How to determine if an ScnNode is left or right of the view direction of a camera node in SceneKit
例如
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 度)之间。 如果值为负,则节点在相机矢量的左侧,正值表示节点在右侧,零表示节点在正前方。
方便的是,如果需要,您可以按原样使用此角度来实际执行旋转。 只需将摄像机角度增加到节点的角度的一部分。
例如
cameraRotation += a * 0.1
注意事项:
projectPoint 函数有一种更简单的方法,可以在您的视图中将图像显示为 2d 元素,您可以根据它的位置找到用户应该看的位置
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")
}
}
并像下面一样使用它
let sceneWidth:CGFloat = self.sceneView.frame.width
getIfHeIsLookingNew(sceneWidth: sceneWidth, nodePosition: self.sceneView.projectPoint(targetNode.position))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.