1

The goal is to simulate a camera in SceneKit and let users pan the camera's point of view by panning the screen.

The code below simulates the camera panning in the horizontal direction, but how would you account for simultaneous panning in the vertical direction?

Would you just concat another rotation, but altering the X-axis and based on translation.y (to get amount user panned vertically)? Or is there a better way?

func doPan(sender: UIPanGestureRecognizer) {
    // Get horizonal pan distance & convert to radians
    let translation = sender.translationInView(sender.view!)
    var xRadians = GLKMathDegreesToRadians(Float(translation.x))

    // Rotate camera
    xRadians = (xRadians / 20) + currentAngle
    cameraNode.transform = SCNMatrix4MakeRotation(xRadians, 0, 1, 0)

    // Update preview block
    setPreviewBlock(cameraNode.position)

    // Save value for next rotation
    if (sender.state == UIGestureRecognizerState.Ended) {
        currentAngle = xRadians
    }
}
Crashalot
  • 33,605
  • 61
  • 269
  • 439

1 Answers1

0

You can multiply the rotation matrices together to get a new transform.

Another approach would be to use the camera node's eulerAngles property. The transform, eulerAngles, and orientation all contgrol the node's rotation, but they use different parameterizations.

See Rotate SCNCamera node looking at an object around an imaginary sphere for a thorough answer/explanation of camera rotations.

Another approach would be to rotate the scene instead of moving the camera. Take a look at SCNAction, the rotateBy... and rotateTo... functions.

Community
  • 1
  • 1
Hal Mueller
  • 7,019
  • 2
  • 24
  • 42