2

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,

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
Nicoara Talpes
  • 710
  • 1
  • 13
  • 28

2 Answers2

1

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

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • Thank you! I took out the constraint and also added the camera as a child to the ball and now I am able to rotate the scene. I find it very curious that there are no tutorials or serious code on the web on how PanGestures physics - I will have to spend more hours debugging different parametrizations. Do you know of any good ones? Also, for example if I want to see the code behind allowsCameraControl = true, where can I find this? Thanks – Nicoara Talpes May 14 '16 at 16:42
  • 1
    SceneKit is not open source, so you're unlikely to see that code. There's some hints to achieving it yourself in [this answer](http://stackoverflow.com/a/25674762/957768), though. – rickster May 14 '16 at 18:04
  • http://infinitered.com/2015/02/10/a-seven-foot-globe-running-on-os-x-and-an-ipad-app-created-using-rubymotion-and-scenekit/ is worth a read. Also remember that there's no requirement that a node have any attached SCNGeometry. You can put your camera on an invisible "mounting arm" that pivots as you like. – Hal Mueller May 14 '16 at 20:44
  • @rickster Thanks for the advice. I cannot comment an that thread because apparently I don't have 50 reputation, but I can here. I am stuck on calling the func scrollViewDidScroll(scrollView: UIScrollView). I believe I should make the viewController to inherit from the UIScrollViewDelegate and add the ScrollView on top of the view and into the controller code , but how is this function called? – Nicoara Talpes May 15 '16 at 20:33
  • @HalMueller Thanks for the link. But it is for a Ruby and not an Xcode project? and there is no commenting for the code? – Nicoara Talpes May 15 '16 at 20:34
  • Basically I need to convert between the 2D moves on the ipad to the SceneKit 3D coordinate system. How do I do this? I have no success so far in going from let translation = sender.translationInView(sender.view!) which has 2D coordinates to 3D coordinates. I assume pivoting involves a z coordinate as well. – Nicoara Talpes May 16 '16 at 08:52
0

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

bitemybyte
  • 971
  • 1
  • 10
  • 24