3

İ am trying to move the SCNNode object which i placed on to a surface. İt moves but the scale changes and it becomes smaller, when i first start to move.

This is what i did;

@IBAction func dragBanana(_ sender: UIPanGestureRecognizer) {
    guard let _ = self.sceneView.session.currentFrame else {return}

    if(sender.state == .began) {
        let location = sender.location(in: self.sceneView)
        let hitTestResult = sceneView.hitTest(location, options: nil)
        if !hitTestResult.isEmpty {
            guard let hitResult = hitTestResult.first else {return}
            movedObject = hitResult.node
        }
    }
    if (sender.state == .changed) {
        if(movedObject != nil) {
            let location = sender.location(in: self.sceneView)
            let hitTestResult = sceneView.hitTest(location, types: .existingPlaneUsingExtent)
            guard let hitResult = hitTestResult.first else {return}
            let matrix = SCNMatrix4(hitResult.worldTransform)
            let vector = SCNVector3Make(matrix.m41, matrix.m42, matrix.m43)
            movedObject?.position = vector
        }
    }
    if (sender.state == .ended) {
        movedObject = nil
    }
}

2 Answers2

1

My answer is probably very late, but I faced this issue myself and it took me a while to kind of figure out why this might happen. I'll share my experience and maybe you can relate to it.

My problem was that I was trying to change the position of the node after changing its scale at runtime (most of my 3D assets were very large when added, I scale them down with a pinch gesture). I noticed changing the scale was the cause of the position change not working as expected.

I found a very simple solution to this. You simply need to change this line:

movedObject?.position = vector

to this:

movedObject?.worldPosition = vector

According to SCNNode documentation, the position property determines the position of the node relative to its parent. While worldPosition is the position of the node relative to the scene's root node (i.e. the world origin of ARSCNView)

I hope this answers your question.

alobaili
  • 761
  • 9
  • 23
-2

It's because you're moving the object on the 3 axis and Z changes that's why it feels like it scales but it's only getting closer to you.

Oscar Falmer
  • 1,771
  • 1
  • 24
  • 38