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 :
I have played with different variations of CGPointToSCNVector3 but no luck. What is the cause of this behavior? Thanks,
The solution was to change sender.translationInView(sender.view!)
to sender.translationInView(self.view!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With