如何在检测到的平面上移动虚拟对象

时间:2019-02-06 07:56:43

标签: swift virtual arkit

我正在尝试在检测到的平面上移动具有动态物理物体的虚拟对象,但是移动之后它开始跳跃和摆动 我已使用Kinematic physicBody在检测到的表面上添加了floorNode 这是我的代码     func didPan(_手势:ThresholdPanGesture){         切换手势状态{         案例开始。

        if let object = objectNodeInteracting(with: gesture, in: sceneView) {
            if object.name == "plane"{
                return
            }
            selectedCube = object
        }
        let holdPoint = gesture.location(in: self.sceneView)
        let result = self.sceneView.hitTest(holdPoint, types: .estimatedHorizontalPlane)
        if (result.count == 0) {
            return;
        }
        let  hitResult = result.first
        let transform = hitResult?.worldTransform
        let dist = SCNVector3.init((transform?.columns.3.x)!, (transform?.columns.3.y)!, (transform?.columns.3.z)!)
        prevusPonit = dist
        PCoordx = (result.first?.worldTransform.columns.3.x)!
        PCoordz = (result.first?.worldTransform.columns.3.z)!

    case .changed:
        if selectedCube == nil  {
            return
        }
        let holdPoint = gesture.location(in: self.sceneView)
        let result = self.sceneView.hitTest(holdPoint, types: .estimatedHorizontalPlane)
        if (result.count == 0) {
            return;
        }
        let  hitResult = result.first
        let transform = hitResult?.worldTransform

        if let coordx = result.first?.worldTransform.columns.3.x{
            if let coordz = result.first?.worldTransform.columns.3.z{

                let action = SCNAction.moveBy(x: CGFloat(coordx-PCoordx), y: 0.0, z: CGFloat(coordz-PCoordz), duration: 0.01)
                SCNTransaction.begin()
                selectedCube!.parent!.runAction(action)
                SCNTransaction.commit()
                PCoordx = coordx
                PCoordz = coordz
            }
        }
        gesture.setTranslation(CGPoint.zero, in: sceneView)
        break

    case .ended:
        break
    default:
    }
}

0 个答案:

没有答案