2

I created an empty node and added a set of nodes to the empty node and finally adding that to my root node. After which, I create a camera node as shown below. I am trying to add a feature to my scenekit app where by when a user tap a node, the camera moves and focus on that node. In my handleTap function, I am trying to get the position of the node and then fix the camera to that position but it is not working. Any suggestions?

//snippet
    var emptyNode = SCNNode()
    emptyNode.addChildNode(Node)
    rootNode.addChildNode(emptyNode)

    cameraNode.camera = SCNCamera()
    cameraNode.position = SCNVector3(1,0,10)
    rootNode.addChildNode(cameraNode)


     func handleTap(recognizer: UITapGestureRecognizer) {
            let location = recognizer.locationInView(sceneView)
            let hits = sceneView!.hitTest(location, options: nil)

            if let tap = hits.first?.node {
                tappNode = tap
                myScene?.cameraNode.position = tappNode.position
            }

EDIT

So, I am making a little progress but I still cannot figure out the position of my node. The Updated code is given below. What I have done is to create a new node and attached a camera to it. That node is then added to the tap node as a child.I still cannot get the position of the tap node within the context of empty node.

let cameraNode = SCNNode()
cameraNode.name = "cameraNode"
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3Make(1,0,10)


tappNode.addChildNode(cameraNode)


SCNTransaction.begin()
SCNTransaction.setAnimationDuration(3.0)

SCNTransaction.setCompletionBlock() {
    print("done")
}
sceneView!.pointOfView = cameraNode

SCNTransaction.commit()
Vatsal Manot
  • 17,695
  • 9
  • 44
  • 80

2 Answers2

3

When you move the camera node to the tapped node (or create a new camera at the tapped node), you make it nearly impossible to see the tapped node from that camera.

How about

let lookAtConstraint = SCNLookAtConstraint.init(tappNode)
sceneView!.pointOfView.constraints = [lookAtConstraint]

Keep a reference to lookAtConstraint so that you can change its target on subsequent taps.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
0

In my own app, I have one "default" point of view as SCNNode, which I define at startup, e.g.

defaultPoint

By double-tapping, the users resets to this point of view, simply so

scnView.pointOfView = self.defaultPoint

where scnView is the main SCNView with all nodes. In your case, I guess you have to define by trial-and-error one or more node-specific points-of-view nodes (which obviously don't have the same coordinates as the nodes themselves), and then set the scene view point-of-view accordingly.

PS my main SCNView has allowsCameraControl set to true.

SuanMei
  • 51
  • 4