admin管理员组

文章数量:1122832

I’m new to ARKit and I’ve been stuck on this problem for days. I want to create an AR app like Ikea, where I can place an object in the environment and move it around. When I use the pan gesture recognizer to drag the object, it moves fine, but it always starts from its original position.

So, I thought I’d save the last position every time I move the object, but that didn’t work. Now, the object keeps moving back to its origin position and it sticks and flickers. I can’t move it anymore!

here is my code:

@objc private func handleMovement(_ gesture: UIPanGestureRecognizer, selectedNode: SCNNode) {
    let touchLocation = gesture.location(in: sceneView)
    let hitTestResults = sceneView.hitTest(touchLocation, types: .existingPlaneUsingExtent)
    if let lastPosition, let planeHitResult = hitTestResults.first {
        selectedNode.position = SCNVector3(
            x: planeHitResult.worldTransform.columns.3.x,
            y: planeHitResult.worldTransform.columns.3.y,
            z: planeHitResult.worldTransform.columns.3.z)
        let deltaX = selectedNode.position.x - lastPosition.x
        let deltaY = selectedNode.position.y - lastPosition.y
        let deltaZ = selectedNode.position.z - lastPosition.z
        selectedNode.position.x += deltaX
        selectedNode.position.y += deltaY
        selectedNode.position.z += deltaZ
    } else {
        if let planeHitResult = hitTestResults.first {
            selectedNode.position = SCNVector3(
                x: planeHitResult.worldTransform.columns.3.x,
                y: occlusionPlaneVerticalOffset,
                z: planeHitResult.worldTransform.columns.3.z)
            lastPosition = selectedNode.position
        }
    }
}

Could you please point out where I’m going wrong?

本文标签: swiftCan39t drag the SCNNode on the scene smoothlyStack Overflow