在 XZ 平面上拖动对象 [英] Drag object on XZ plane

查看:20
本文介绍了在 XZ 平面上拖动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个增强现实应用程序,我希望能够在空间中拖动一个对象.我在这里找到的解决方案的问题是,建议使用 projectPoint/unprojectPoint 的解决方案是它们产生沿 XY 平面的运动.

I am working on an augmented reality app and I would like to be able to drag an object in the space. The problem with the solutions I find here in SO, the ones that suggest using projectPoint/unprojectPoint, is that they produce movement along the XY plane.

我试图将手指在屏幕上的移动用作节点的 xz 坐标的偏移量.问题是有很多东西需要考虑(相机的位置、节点的位置、节点的旋转等.)

I was trying to use the fingers movement on the screen as an offset for x and z coordinates of the node. The problem is that there is a lot of stuff to take in consideration (camera's position, node's position, node's rotation, etc..)

有没有更简单的方法来做到这一点?

Is there a simpler way of doing this?

推荐答案

首先您需要在原点下方创建地板或非常大的平面几米(我有 10 个).这可以确保您的 hittest 始终返回值.然后使用平移手势:

first you need to create floor or very large plane few meters (i have 10) below origin. This makes sure your hittest always returns value. Then using pan gesture :

//store previous coordinates from hittest to compare with current ones
var PCoordx: Float = 0.0
var PCoordz: Float = 0.0

@objc func move(_ gestureRecognizer: UIPanGestureRecognizer){
    if gestureRecognizer.state == .began{
        let hitNode = sceneView.hitTest(gestureRecognizer.location(in: sceneView), options: nil)
        PCoordx = (hitNode.first?.worldCoordinates.x)!
        PCoordz = (hitNode.first?.worldCoordinates.z)!
    }

    // when you start to pan in screen with your finger
    // hittest gives new coordinates of touched location in sceneView
    // coord-pcoord gives distance to move or distance paned in sceneview 
    if gestureRecognizer.state == .changed {
        let hitNode = sceneView.hitTest(gestureRecognizer.location(in: sceneView), options: nil)
        if let coordx = hitNode.first?.worldCoordinates.x{
            if let coordz = hitNode.first?.worldCoordinates.z{

            let action = SCNAction.moveBy(x: CGFloat(coordx-PCoordx), y: 0, z: CGFloat(coordz-PCoordz), duration: 0.1)
            node.runAction(action)

            PCoordx = coordx
            PCoordz = coordz
            }
        }

        gestureRecognizer.setTranslation(CGPoint.zero, in: sceneView)
    }
    if gestureRecognizer.state == .ended{
        PCoordx = 0
        PCoordz = 0
    }
}

在我的例子中只有一个节点,所以我没有检查所需的节点是否被录音.如果你有很多节点,你可以随时检查它.

In my case there is only one node so i have not checked if required node is taped or not. You can always check for it if you have many nodes.

这篇关于在 XZ 平面上拖动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆