在XZ平面上拖动对象 [英] Drag object on XZ plane
问题描述
我正在开发增强现实应用程序,我希望能够在空间中拖动对象.我在SO中找到的解决方案的问题(建议使用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.
我试图用手指在屏幕上的移动作为节点的 x 和 z 坐标的偏移量.问题是要考虑很多东西(相机的位置,节点的位置,节点的旋转等).
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..)
有更简单的方法吗?
推荐答案
首先,您需要在原点以下几米(我有10米)的地方创建地板或非常大的平面.这样可以确保您的命中测试始终返回值.然后使用平移手势:
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屋!