简体   繁体   English

在保持速度的同时沿X轴拖动SceneKit节点? 迅捷3

[英]Drag SceneKit Node Along X-Axis while maintaining velocity? Swift 3

Swift 3, SceneKit: In my game, I have an SCNSphere node in the center of the screen. Swift 3,SceneKit:在我的游戏中,我在屏幕中央有一个SCNSphere节点。 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. 球体由于重力而下落到SCNBox节点上,并且一旦与框碰撞,就会对其施加SCNVector3(0,6,0)的速度。

A new box is created and moves forward (z+) towards my camera and towards the sphere as well. 创建一个新框,并将其向前(z +)移向我的相机以及球体。 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. 球体上升,达到峰值,然后再下降(靠重力)朝着新盒子移动,当球体与新盒子碰撞时,会对其应用SCNVector(0,6,0)的速度。 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. 所有盒子都始于球体节点的前面,并在创建时向其移动。盒子被放置在一行中,一个在球体的左侧,一个在球体的正前方(中间),第三个球体的权利。

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 . 在拖动时,我根本不希望更改y速度或y位置 I just want the x-position of my sphere node to mirror the real-world x-position of my finger relative to the screen. 我只希望球体节点的x位置反映我手指相对于屏幕的真实x位置。 I also do not want the sphere node to change location based on a touch alone . 我也不希望球体节点仅根据触摸来更改位置

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. 例如,如果球的位置在SCNVector3(2,0,0),并且如果用户在SCNVector3(-2,0,0)附近点击,则我不希望球“传送”到用户点击的位置。 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
}

I just want the x-position of my sphere node to mirror the real-world x-position of my finger relative to the screen 我只希望球体节点的x位置反映手指相对于屏幕的真实x位置

You can do this by using UIPanGestureRecognizer and getting the translation in the coordinate system of the view. 您可以通过使用UIPanGestureRecognizer并在视图的坐标系中获取平移来做到这一点。

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>>)

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. 对于z值,请参考unProjectPoint讨论,其中说z应该指的是您要取消投影的深度(相对于视锥的近和远剪切平面)。

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. 然后,您可以将平移取消投影到场景的3D世界坐标系,这将为您提供球体节点的平移。 Some partial sample code: 一些部分示例代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM