title: SceneKit - Rotating around a point causing strange rotation
body: <p>I want to be able to rotate a camera around a cube (a collection of cubes), but for some reason, when I rotate, the rotation goes in multiple directions.</p>
  
  <p>I have an object that I wish to rotate around and set this as a look at constraint. This object is in the middle of the cubes.</p>
  
  <p>I have a swipe gesture recogniser that does the rotation...but it's just off. I'm missing something...</p>
  
  <p><a href="https://i.stack.imgur.com/rd521.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rd521.jpg" alt="Example Result"></a>
  <a href="https://i.stack.imgur.com/gJU8j.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gJU8j.jpg" alt="Example Result 2"></a></p>
  
  <pre><code>import UIKit
  import QuartzCore
  import SceneKit
  import AVFoundation
  
  class GameViewController: UIViewController {
  
      var scene:SCNScene!
      var tiles:[SCNNode] = [SCNNode]()
      var cameraNode:SCNNode!
      var centerNode:SCNNode!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          // create a new scene
          scene = SCNScene()
  
          let sideMaterial  = SCNMaterial()
          sideMaterial.diffuse.contents = UIColor.greenColor()
          sideMaterial.locksAmbientWithDiffuse = true;
  
          let geom = SCNBox(width: 32, height: 32.0, length: 32, chamferRadius: 8.0)
          geom.widthSegmentCount = 16
          geom.heightSegmentCount = 16
          geom.lengthSegmentCount = 16
          geom.materials = [sideMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial]
  
  
          centerNode = SCNNode(geometry: geom)
          centerNode.position = SCNVector3(x: 4*32, y: -4*32, z: -4*32)
          scene.rootNode.addChildNode(centerNode)
  
          // create and add a camera to the scene
          cameraNode = SCNNode()
          cameraNode.camera = SCNCamera()
          scene.rootNode.addChildNode(cameraNode)
  
          // place the camera
  
          cameraNode.position = SCNVector3(x: -4*32, y: -4*32, z: 4*32)
          cameraNode.pivot = SCNMatrix4MakeTranslation(4*32, -4*32, -4*32)
          cameraNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: 1.360646)
  
          let lookAt = SCNLookAtConstraint(target: centerNode)
          //lookAt.gimbalLockEnabled = true
          cameraNode.constraints = [lookAt]
  
  
          cameraNode.camera?.xFov = 90
          cameraNode.camera?.yFov = 90
          cameraNode.camera?.zNear = 1
          cameraNode.camera?.zFar = 5000
  
          // create and add a light to the scene
          let lightNode = SCNNode()
          lightNode.light = SCNLight()
          lightNode.light!.type = SCNLightTypeOmni
          lightNode.position = SCNVector3(x: 4*32, y: -4*32, z: 10)
          scene.rootNode.addChildNode(lightNode)
  
          // create and add an ambient light to the scene
          let ambientLightNode = SCNNode()
          ambientLightNode.light = SCNLight()
          ambientLightNode.light!.type = SCNLightTypeAmbient
          ambientLightNode.light!.color = UIColor.darkGrayColor()
          scene.rootNode.addChildNode(ambientLightNode)
  
  
          // retrieve the SCNView
          let scnView = self.view as! SCNView
  
          setupTiles()
  
          // set the scene to the view
          scnView.scene = scene
  
          // allows the user to manipulate the camera
          //scnView.allowsCameraControl = true
  
          // show statistics such as fps and timing information
          scnView.showsStatistics = true
  
          // configure the view
          scnView.backgroundColor = UIColor.blackColor()
  
          // Add gestures
  
          var gestureRecognizers = [UIGestureRecognizer]()
  
          let swipeDownGesture = UISwipeGestureRecognizer(target: self, action: "swipe:")
          swipeDownGesture.direction = UISwipeGestureRecognizerDirection.Down
          gestureRecognizers.append(swipeDownGesture)
  
          let swipeUpGesture = UISwipeGestureRecognizer(target: self, action: "swipe:")
          swipeUpGesture.direction = UISwipeGestureRecognizerDirection.Up
          gestureRecognizers.append(swipeUpGesture)
  
          let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: "swipe:")
          swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.Left
          gestureRecognizers.append(swipeLeftGesture)
  
          let swipeRightGesture = UISwipeGestureRecognizer(target: self, action: "swipe:")
          swipeRightGesture.direction = UISwipeGestureRecognizerDirection.Right
          gestureRecognizers.append(swipeRightGesture)
  
          if let existingGestureRecognizers = scnView.gestureRecognizers as? [UIGestureRecognizer] {
              gestureRecognizers.extend(existingGestureRecognizers)
          }
          scnView.gestureRecognizers = gestureRecognizers
      }
  
      func swipe(gestureRecognize: UISwipeGestureRecognizer) {
  
          var leftRight:Float = 0
          var upDown:Float = 0
          var amount:Float = 1.5707
          switch gestureRecognize.direction
          {
          case UISwipeGestureRecognizerDirection.Up:
              upDown = 1
              break
          case UISwipeGestureRecognizerDirection.Down:
              upDown = -1
              break
          case UISwipeGestureRecognizerDirection.Left:
              leftRight = -1
              break
          case UISwipeGestureRecognizerDirection.Right:
              leftRight = 1
              break
          default:
              break
          }
  
          SCNTransaction.begin()
          SCNTransaction.setAnimationDuration(3.0)
  
          cameraNode.rotation = SCNVector4(x: leftRight, y: upDown, z: 0, w: amount)
  
          SCNTransaction.commit()
  
  
      }
  
      override func shouldAutorotate() -&gt; Bool {
          return true
      }
  
      override func prefersStatusBarHidden() -&gt; Bool {
          return true
      }
  
  //    override func supportedInterfaceOrientations() -&gt; UIInterfaceOrientationMask {
  //        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
  //            return UIInterfaceOrientationMask.AllButUpsideDown
  //        } else {
  //            return UIInterfaceOrientationMask.All
  //        }
  //    }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Release any cached data, images, etc that aren't in use.
      }
  
      func setupTiles()
      {
  
  
          for z in 0...7
          {
              for y in 0...7
              {
                  for x in 0...7
                  {
  
                      if y &gt; 0 &amp;&amp; y &lt; 7 &amp;&amp; z &gt; 0 &amp;&amp; z &lt; 7 &amp;&amp; x &gt; 0 &amp;&amp; x &lt; 7
                      {
                          continue
                      }
  
                      let topMaterial = SCNMaterial()
                      topMaterial.diffuse.contents = UIColor.whiteColor()
                      topMaterial.locksAmbientWithDiffuse = true;
  
                      let sideMaterial  = SCNMaterial()
                      sideMaterial.diffuse.contents = UIColor.whiteColor()
                      sideMaterial.locksAmbientWithDiffuse = true;
  
                      let geom = SCNBox(width: 32, height: 32.0, length: 32, chamferRadius: 8.0)
                      geom.widthSegmentCount = 16
                      geom.heightSegmentCount = 16
                      geom.lengthSegmentCount = 16
                      geom.materials = [topMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial]
  
  
                      let node = SCNNode(geometry: geom)
                      node.position = SCNVector3(x: Float(x) * 32.0, y: -(Float(y) * 32.0), z: -(Float(z) * 32.0))
  
                      scene.rootNode.addChildNode(node)
  
                      tiles.append(node)
  
                  }
              }
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>Setting the transform instead of the rotation seems to have better effects</p>
  
  <pre><code>    centerNode = SCNNode(geometry: geom)
      centerNode.transform = SCNMatrix4Identity
      scene.rootNode.addChildNode(centerNode)
  
      // create and add a camera to the scene
      cameraNode = SCNNode()
      cameraNode.camera = SCNCamera()
      scene.rootNode.addChildNode(cameraNode)
  
      // place the camera
  
      cameraNode.position = SCNVector3(x: 0, y: 0, z: 8*32)
      cameraNode.pivot = SCNMatrix4MakeTranslation(0, 0, -8*32)
  
      let lookAt = SCNLookAtConstraint(target: centerNode)
      lookAt.gimbalLockEnabled = true
      cameraNode.constraints = [lookAt]
  
      cameraNode.transform = SCNMatrix4Identity
  
      //In gesture
      cameraNode.transform = SCNMatrix4Rotate(cameraNode.transform, amount, upDown, leftRight, 0)
  </code></pre>