Swift - How to find direction of target node i.e. Left or Right from Current Camera Position

为君一笑 提交于 2020-01-16 19:35:08

问题


I am trying to achieve a scenario where i need to find a way to reach from one SCNNode to another SCNNode. I have added the node to sceneView on the click of screen i.e. tapEvent using following code.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let result = sceneView.hitTest(touch.location(in: sceneView), types: [.featurePoint])
    guard let hitResult = result.last else { return }
    let hitTransform = SCNMatrix4(hitResult.worldTransform)
    let hitVector = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43)
    createBall(hitVector)
}

func createBall(_ position:SCNVector3){
    let ballShape = SCNSphere(radius: 0.10)
    let ballNode = SCNNode(geometry: ballShape)
    ballNode.light = SCNLight()
    ballNode.scale = SCNVector3(1,1,1)
    ballNode.light?.type = .directional
    ballNode.light?.color = UIColor.red
    ballNode.position = position
    sceneView.scene.rootNode.addChildNode(ballNode)
}

Also i was able to find a path between two nodes and draw line between them,

func lineBetweenNodes(positionA: SCNVector3, positionB: SCNVector3, inScene: SCNScene) -> SCNNode {
    let vector = SCNVector3(positionA.x - positionB.x, positionA.y - positionB.y, positionA.z - positionB.z)
    let distance = sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z)
    let midPosition = SCNVector3 (x:(positionA.x + positionB.x) / 2, y:(positionA.y + positionB.y) / 2, z:(positionA.z + positionB.z) / 2)

    let lineGeometry = SCNCylinder()
    lineGeometry.radius = 0.01
    lineGeometry.height = CGFloat(distance)
    lineGeometry.radialSegmentCount = 5
    //lineGeometry.firstMaterial!.diffuse.contents = GREEN

    let lineNode = SCNNode(geometry: lineGeometry)
    lineNode.position = midPosition
    lineNode.look (at: positionB, up: inScene.rootNode.worldUp, localFront: lineNode.worldUp)
    return lineNode
}

The required thing here is i need to allow user to reach other node by guiding him the direction via left or right from the current camera position.

Note:- I have already referred to all related answers that are given here and they did not worked for me. Link


Update

I tried with following code to find the direction :

func getDirection(){
    guard let nodeToDetect = node1 else { return } // Node which needs to be detected

    if let pointOfView = sceneView.pointOfView{
        let isMaybeVisible = sceneView.isNode((nodeToDetect.presentation), insideFrustumOf: pointOfView)
        if isMaybeVisible{

            print("-- Destination is Visible --")

        }else{
            let transform = pointOfView.transform
            let orientation = SCNVector3(-transform.m31, -transform.m32, transform.m33)
            let location = SCNVector3(transform.m41, transform.m42, transform.m43)
            let cameraPosition = addTwo(lhv: orientation, rhv: location)

            guard let nodePosition = node1?.position else { return }

            let v = CGPoint(x: CGFloat(nodePosition.x - cameraPosition.x), y: CGFloat(nodePosition.y - cameraPosition.y))
            let a = atan2(v.y, v.x) // Note: order of arguments

            if a < 0{

                print("Move Left")

            }else if a > 0{

                print("Move Right")

            }else{

                print("Move Straight")

            }
        }
    }
}

Still not able to find accurate direction to reach node.

来源:https://stackoverflow.com/questions/59501725/swift-how-to-find-direction-of-target-node-i-e-left-or-right-from-current-cam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!