I am trying to move a node in SceneKit by touch. I am using the code here: Move a specific node in SceneKit using touch
The issue that I am having is that every time I start a panGesture, the object I touch goes to the left corner of the scene to start the move. Moving it from that position and releasing are ok, just the issue that at every start of panning, the object resets from the left corner. If I zoom out, it resets itself from the corner of this new zoom level.
My code is:
func CGPointToSCNVector3(view: SCNView, depth: Float, point: CGPoint) -> SCNVector3 {
let projectedOrigin = view.projectPoint(SCNVector3Make(0, 0, depth))
let locationWithz = SCNVector3Make(Float(point.x), Float(point.y), projectedOrigin.z)
return view.unprojectPoint(locationWithz)
}
func dragObject(sender: UIPanGestureRecognizer){
if(movingNow){
let translation = sender.translationInView(sender.view!)
var result : SCNVector3 = CGPointToSCNVector3(objectView, depth: tappedObjectNode.position.z, point: translation)
tappedObjectNode.position = result
}
else{
let hitResults = objectView.hitTest(sender.locationInView(objectView), options: nil)
if hitResults.count > 0 {
movingNow = true
}
}
if(sender.state == UIGestureRecognizerState.Ended) {
}
}
and
override func viewDidLoad() {
super.viewDidLoad()
let scnView = objectView
scnView.backgroundColor = UIColor.whiteColor()
scnView.autoenablesDefaultLighting = true
scnView.allowsCameraControl = true
i am temporarily disabling the allowsCameraControl's panning functions before dragObject is called by doing this:
globalPanRecognizer = UIPanGestureRecognizer(target: self,
action:#selector(ViewController.dragObject(_:)))
objectView.addGestureRecognizer(globalPanRecognizer)
These are the values inside the first call to CGPointToSCNVector3 :
- initial value of tappedObjectNode: SCNVector3(x: 0.100000001, y: 0.100000001, z: 3.0)
- projectedOrigin : SCNVector3(x: 261.613159, y: 285.530396, z: 0.949569583) - this is abnormally big
- value returned by CGPointToSCNVector3 : SCNVector3(x: 1.03418088, y: 1.9734658, z: 4.64346933)
I have played with different variations of CGPointToSCNVector3 but no luck. What is the cause of this behavior? Thanks,