如何在Arkit 1.5中施加力后跟踪节点的位置 [英] how to track the position of a node after applying force in Arkit 1.5

查看:27
本文介绍了如何在Arkit 1.5中施加力后跟踪节点的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦节点开始移动,出于某种原因,它就会停止跟踪节点的位置.这就是我的意思:

As soon as a node starts moving, for some reason, it stops tracking the position of the node. and here's what I mean:

 internal func launchNode(force:Float) {
    let force = force * 0.004

    currNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
    currNode.physicsBody?.mass = 0.05

    let direction = currNode.worldFront + SCNVector3(force, -force, -force)
    currNode.physicsBody?.applyForce(direction, asImpulse: true)

}

节点正在移动,这很好,但我想在节点移动时跟踪它的当前位置:

The Node is moving, which is great, but I'd like to track the current position of the node while it's moving:

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

guard currNode != nil else { return }

    print("simdWorldPosition: \(currNode.simdWorldPosition.y)  position: \(currNode.position.y) simWorldPosition: \(currNode.simdWorldPosition.y)  simPosition: \(currNode.simdPosition.y)")
}

这些属性都没有更新位置,所以我知道我遗漏了一些东西.当它到达某个位置(在 y 坐标中)时,我想阻止它移动.如果有人在 .applyForce 之后成功跟踪节点的位置,如果您能指出我做错了什么,也许对您有用,我将不胜感激.谢谢.

None of these properties is updating the location, so I know I'm missing something. I'd like to stop it from moving when it gets to a certain position (in y coordinate). If anyone had a success tracking the location of a node after .applyForce, I'd much appreciate it if you could point out what I did wrong, and perhaps what worked for you. thanks.

更新

这是我用来测试它的一些代码.

Here's some code I'm using to test it.

我在这个 ViewController 的开头声明了节点:

I'm declaring the node at the beginning of this ViewController:

var currNode = SCNNode()

您可以尝试以下测试以跟踪具有 ARKit 默认项目的节点的位置:

You can try the following for testing to track the location of a node with the ARKit default project:

//call this once your scene is set
func addTapGestureToSceneView() {
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.didTap(withGestureRecognizer:)))
    sceneView.addGestureRecognizer(tapGestureRecognizer)
}

@objc func didTap(withGestureRecognizer recognizer: UIGestureRecognizer) {
    let tapLocation = recognizer.location(in: sceneView)
    let hitTestResults = sceneView.hitTest(tapLocation)
    guard let node = hitTestResults.first?.node else { return }
    if node.name == "shipMesh" {
        currNode = node
        moveShip()
    }
}

  internal func moveShip() {
    let force = 2 * 0.004
    currNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
    currNode.physicsBody?.mass = 0.05
    let direction = currNode.worldFront + SCNVector3(force, -force, -force)
    currNode.physicsBody?.applyForce(direction, asImpulse: true)

}

  func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

    print("simdWorldPosition: \(currNode.simdWorldPosition.y)  position: \(currNode.position.y) simWorldPosition: \(currNode.simdWorldPosition.y)  simPosition: \(currNode.simdPosition.y)")
}

推荐答案

所以你的问题的答案在一行中

So Answer of your question is in one line

使用 node.presentation 属性

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

    print("simdWorldPosition: \(currNode.presentation.simdWorldPosition.y)  position: \(currNode.presentation.position.y) simWorldPosition: \(currNode.presentation.simdWorldPosition.y)  simPosition: \(currNode.presentation.simdPosition.y)")
}

来自 Apple 文档

当您使用隐式动画(请参阅 SCNTransaction)更改节点的属性时,这些节点属性会立即设置为其目标值,即使动画节点内容似乎从旧属性值转换为新属性值.在动画过程中,SceneKit 维护节点的副本,称为呈现节点,其属性反映了由当前影响节点的任何动态动画确定的瞬态值.

When you use implicit animation (see SCNTransaction) to change a node’s properties, those node properties are set immediately to their target values, even though the animated node content appears to transition from the old property values to the new. During the animation SceneKit maintains a copy of the node, called the presentation node, whose properties reflect the transitory values determined by any in-flight animations currently affecting the node.

希望对你有帮助

这篇关于如何在Arkit 1.5中施加力后跟踪节点的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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