简体   繁体   中英

SceneKit – pan gesture is moving node too quickly

I'm currently trying to use a pan gesture recognizer to move a node in SceneKit. I'm only moving it along the X -axis, however my gesture moves the object a lot further/faster then it should even when only using small gestures. I'm not 100% sure what I'm doing wrong here but here's the code for my gesture recognizer:

@objc func handlePan(_ pan:UIPanGestureRecognizer) {

    if pan.state == .changed {
        let translation = pan.translation(in: pan.view!)

        node!.position = SCNVector3(x:node!.position.x + Float(translation.x), y:node!.position.y, z:node!.position.z)      
        pan.setTranslation(CGPoint.zero, in: pan.view!)
    } 
}

As I say the object is being moved it's just being launched at incredible speed and distance. The effect almost appears cumulative.

I thought this could be the case if I didn't reset the translation of my pan gesture recognizer, but I am doing that here

pan.setTranslation(CGPoint.zero, in: pan.view!)

I'm actually trying to get this work in an ARKit scenario, but I've stripped all that out to just get a node moving correctly but I'm still having issues.

The pan is added to an ARSCNView whereas the node I'm trying to manipulate is added as a childNode to the ARSCNView.scene.rootNode so I'm wondering if it's the positions/coordinates of these that are the problem.

let translation = pan.translation(in: pan.view!)

This code returns CGPoint with gesture position in the view in points (which is could be pixels). But SCNNode position (in real world) is position in meters. So, when you're adding one point for X position in SCNVector, you're actually adding one meter for that.

To convert screen point into 3D world coordinates use unprojectPoint method of ARSCNView . You probably will need to save previous gesture position to be able to find position changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM