Nicoara Talpes
Nicoara Talpes

Reputation: 720

Scenekit rotate camera around a scene

I have a simple SceneKit project in Swift where I add two objects to a scene: a ball and a box.

I want to be able to:

  1. pan with 1 finger to rotate the camera around the scene
  2. move an object by clicking on a ‘Move’ button, selecting an object, and dragging it to a new position (via a PanGesture)

I want this with allowsCameraControl = false, because I cannot do gesture 2) with it enabled, and I want to add further panGestures down the line.

I am stuck because I am unable to get the camera to rotate around the scene. I have the camera looking at one of the objects with a constraint:

let constraint = SCNLookAtConstraint(target: globalSCNNode)
constraint.gimbalLockEnabled = true
globalCameraSCNNode.constraints = [constraint]

but a panning gesture like this does not do anything:

func panGesture(sender: UIPanGestureRecognizer) {
let translation = sender.translationInView(sender.view!)
var action = SCNAction.rotateByX(0, y: 0.5, z: 0, duration: 0.1)
globalCameraSCNNode.runAction(action)

Can someone help? Thank you,

Upvotes: 2

Views: 3541

Answers (2)

bitemybyte
bitemybyte

Reputation: 1089

I'm guessing your goal is to rotate the camera around the center of the scene, otherwise, it wouldn't be a rotation, but a movement.

Please have a look at my answer here which is based on this answer

Upvotes: 0

Hal Mueller
Hal Mueller

Reputation: 7655

The LookAt constraint will override your rotation commanded in panGesture. Try removing that constraint.

Upvotes: 1

Related Questions