在保持速度的同时沿 X 轴拖动 SceneKit 节点?斯威夫特 3 [英] Drag SceneKit Node Along X-Axis while maintaining velocity? Swift 3

查看:53
本文介绍了在保持速度的同时沿 X 轴拖动 SceneKit 节点?斯威夫特 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Swift 3, SceneKit: In my game, I have an SCNSphere node in the center of the screen. The sphere drops by gravity onto an SCNBox node, and a velocity of SCNVector3(0,6,0) is applied to it once it collides with the box.

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

A new box is created and moves forward (z+) towards my camera and towards the sphere as well. The sphere rises, peaks, and then falls back down (by gravity) towards the new box, and when it collides with the new box, a velocity of SCNVector(0,6,0) is applied to it. This process repeats continuously. A sphere that repeatedly bounces on a new approaching box, basically.

然而,不是只有一个盒子,而是一排三个盒子.所有的盒子都从球体节点的前面开始,当它们被创建时向它移动,盒子排成一排,一个在球体的左边,一个在球体的正前方(中间),第三个到球体的右侧.

Instead of just one box, however, there will be three boxes in a row. All boxes begin in front of the sphere node and move towards it when they are created, the boxes are placed in a row, one to the left of the sphere, one directly in front of the sphere (the middle), and the third to the right of the sphere.

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

I want to be able to drag my finger across the screen and move my sphere so that it can land on the left and right boxes. While I'm dragging, I do not want the y-velocity or y-position to be changed at all. I just want the x-position of my sphere node to mirror the real-world x-position of my finger relative to the screen. I also do not want the sphere node to change location based on a touch alone.

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

For example, if the sphere's position is at SCNVector3(2,0,0), and if the user taps near SCNVector3(-2,0,0), I do not want the sphere to "teleport" to where the user tapped. I want the user to drag the sphere from its last position.

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
}

推荐答案

我只想让我的球体节点的 x 位置反映我手指相对于屏幕的真实世界 x 位置

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 在视图的坐标系中.

You can do this by using UIPanGestureRecognizer and getting the translation in the coordinate system of the 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 应该是指相对于您的视锥体的近剪裁平面和远剪裁平面,您要取消投影的深度.

For z value, refer to the unProjectPoint Discussion, which says that z should refer to the depth at which you want to un-project relative to the near and far clipping planes of your view frustum.

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

You can then un-project the translation to the 3D world coordinate system of the scene, which will give you the translation for the sphere node. Some partial sample code:

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

这篇关于在保持速度的同时沿 X 轴拖动 SceneKit 节点?斯威夫特 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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