菜鸟教程小白 发表于 2022-12-12 12:35:32

ios - 在保持速度的同时沿 X 轴拖动 SceneKit 节点? swift 3


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

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

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

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

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

<pre><code>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
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><blockquote>
<p>I just want the x-position of my sphere node to mirror the real-world x-position of my finger relative to the screen</p>
</blockquote>

<p>您可以使用 <a href="https://developer.apple.com/reference/uikit/uipangesturerecognizer" rel="noreferrer noopener nofollow">UIPanGestureRecognizer</a> 来做到这一点并获得 <a href="https://developer.apple.com/reference/uikit/uipangesturerecognizer/1621207-translation" rel="noreferrer noopener nofollow">translation</a>在 View 的坐标系中。 </p>

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

<p>关于 z 值,请引用 <a href="https://developer.apple.com/reference/scenekit/scnscenerenderer/1522631-unprojectpoint" rel="noreferrer noopener nofollow">unProjectPoint</a>讨论,其中说 z 应该是指相对于视锥体的近剪裁平面和远剪裁平面您想要取消投影的深度。 </p>

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

<pre><code>let trans:SCNVector3 = sceneView.unProjectPoint(transPoint3D)
let pos:SCNVector3 = sphereNode.presentationNode.position
let newPos:SCNVector3 = // trans + pos
sphereNode.position = newPosition
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在保持速度的同时沿 X 轴拖动 SceneKit 节点? swift3,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41291615/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41291615/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在保持速度的同时沿 X 轴拖动 SceneKit 节点? swift 3