OGeek|极客世界-中国程序员成长平台

标题: ios - 在保持速度的同时沿 X 轴拖动 SceneKit 节点? swift 3 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:35
标题: ios - 在保持速度的同时沿 X 轴拖动 SceneKit 节点? swift 3

Swift 3,SceneKit:在我的游戏中,我在屏幕中央有一个 SCNSphere 节点。球体在重力作用下落到 SCNBox 节点上,一旦与盒子碰撞,就会应用 SCNVector3(0,6,0) 的速度。

一个新的盒子被创建并向前 (z+) 向我的相机和球体移动。球体上升,达到顶峰,然后(通过重力)回落到新盒子,当它与新盒子碰撞时,SCNVector(0,6,0) 的速度被应用到它。这个过程不断重复。基本上,一个在新接近的盒子上反复反弹的球体。

但是,不是只有一个盒子,而是连续三个盒子。所有盒子都从球体节点的前面开始,并在创建时朝它移动,盒子排成一排,一个在球体的左侧,一个在球体的正前方(中间),第三个到球体的右边。

我希望能够在屏幕上拖动手指并移动我的球体,以便它可以落在左右框上。 在拖动时,我根本不想改变 y 速度或 y 位置。我只想让我的球体节点的 x 位置反射(reflect)我的手指相对于屏幕的真实 x 位置。 我也不希望球体节点仅根据触摸来改变位置

例如,如果球体的位置在 SCNVector3(2,0,0),并且如果用户在 SCNVector3(-2,0,0) 附近点击,我不希望球体“传送”到用户点击。我希望用户将球体从其最后一个位置拖动。

func handlePan(recognizer: UIPanGestureRecognizer) {

    let sceneView = self.view as! SCNView
    sceneView.delegate = self
    sceneView.scene = scene


    let trans:SCNVector3 = sceneView.unprojectPoint(SCNVector3Zero)
    let pos:SCNVector3 = player.presentation.position
    let newPos = (trans.x) + (pos.x)
    player.position.x = newPos
}



Best Answer-推荐答案


I just want the x-position of my sphere node to mirror the real-world x-position of my finger relative to the screen

您可以使用 UIPanGestureRecognizer 来做到这一点并获得 translation在 View 的坐标系中。

let myPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
let trans2D:CGPoint = myPanGestureRecognizer.translation(in:self.view)
let transPoint3D:SCNVector3 = SCNVector3Make(trans2D.x, trans2D.y, <<z>>)

关于 z 值,请引用 unProjectPoint讨论,其中说 z 应该是指相对于视锥体的近剪裁平面和远剪裁平面您想要取消投影的深度。

然后您可以将平移取消投影到场景的 3D 世界坐标系,这将为您提供球体节点的平移。部分示例代码:

let trans:SCNVector3 = sceneView.unProjectPoint(transPoint3D)
let pos:SCNVector3 = sphereNode.presentationNode.position
let newPos:SCNVector3 = // trans + pos
sphereNode.position = newPosition

关于ios - 在保持速度的同时沿 X 轴拖动 SceneKit 节点? swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41291615/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4