【问题标题】:How to get current SCNnode position after apply force施加力后如何获取当前的 SCNnode 位置
【发布时间】:2021-05-22 08:45:45
【问题描述】:

我使用下面的方法对场景中我的SCNnode施加一个恒定的力,

 func applyForceToDrone(powerApply: Double, isHovering: Bool, gravity : SCNVector3) {
   
        let nodo = arrayDrone[0] // load the first drone in the scene
        guard let lhBlade = nodo.childNode(withName: "Rotor_L_2", recursively: true) else {fatalError()}
        guard let rhBlade = nodo.childNode(withName: "Rotor_R_2", recursively: true) else {fatalError()}
        if isHovering == true {
            print("x gravity = \(gravity.x), \(gravity.y) \(gravity.z)")
            let force = SCNVector3(gravity.x, abs(gravity.y), gravity.z)

            nodo.physicsBody?.applyForce(force, asImpulse: false)
        } else {
            print("apply power  \(powerApply)")
            let positionBladeLH = lhBlade.position
            let positionBladeRH = rhBlade.position
            let force = SCNVector3(0, powerApply, 0)
            nodo.physicsBody?.applyForce(force, at: positionBladeLH, asImpulse: false)
            nodo.physicsBody?.applyForce(force, at: positionBladeRH, asImpulse: false)
            
            let alt = nodo.position.y // position is wrong ... is always the starting position
            print(alt)
        }
    }

我在渲染方法中使用了这个函数来驾驶我的无人机。

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

我想在每次施加力时读取节点的当前位置。

如何在渲染施加力时获取更新位置。

我试过了:

let alt = nodo.position.y

但它总是给我节点的起始位置,而不是施加力后的位置。

谢谢

【问题讨论】:

    标签: swift xcode sprite-kit scenekit arkit


    【解决方案1】:

    像这样使用节点的表示对象:

    而不是

    nodo.position.y
    

    使用

    nodo.presentation.position.y // local space
    

    nodo.presentation.worldPosition.y // world space
    

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 2017-12-22
      • 2021-05-20
      • 1970-01-01
      • 2019-10-29
      • 2014-07-24
      • 1970-01-01
      • 2016-03-06
      相关资源
      最近更新 更多