Search code examples
swiftios9scenekit

How can i translate my camera node position after rotation in required direction in SceneKit?


I am developing a 3D application using SceneKit in iOS, I need to translate my camera position after rotation. Can anybody help me to do this task?


Solution

  • I usually do this by stringing together a few SCNMatrix4 operations.

    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    
    //start off with an identity matrix
    var cameraTransform = SCMMatrix4Identity
    
    //rotate by 90degrees about Z axis
    cameraTransform = SCNMatrix4Rotate(cameraTransform, Float(M_PI_2), 0, 0, 1)
    
    //translate by 2 units in x direction
    cameraTransform = SCNMatrix4Translate(cameraTransform, 2, 0, 0)
    
    //set the camera's transform
    cameraNode.transform = cameraTransform