我正在使用SceneKit和物理模拟编写App,并希望在每个渲染循环中获得节点的精确位置。 但物理模拟计算的位置之间似乎存在奇怪的差距。
在func renderer(_ renderer: SCNSceneRenderer, didSimulatePhysicsAtTime time: TimeInterval)
中,我已经使用这些代码来查看节点移动的数量和physicsBody的速度。
(我只对水平位置感兴趣所以只计算x / z位置)
let distance = sqrtf(powf(position.x - previous.x, 2) + powf(position.z - previous.z, 2))
let velocity = sqrtf(powf(node.physicsBody!.velocity.x, 2) + powf(node.physicsBody!.velocity.z, 2))
print("distance: \(distance), velocity: \(velocity), fps: \(1 / timeDelta)")
输出
distance: 4.0241585, velocity: 120.8824, fps: 59.969265810462744
distance: 4.0241585, velocity: 120.882034, fps: 59.96926570579626
distance: 4.0241585, velocity: 120.881676, fps: 59.969265810462744
distance: 4.0257597, velocity: 120.88132, fps: 59.96926570579626
distance: 4.0257597, velocity: 120.88095, fps: 59.969265810462744 <--
distance: 2.0128798, velocity: 120.88059, fps: 59.96926570579626 <--
distance: 2.0144815, velocity: 120.880226, fps: 59.96926570579626
distance: 2.0144815, velocity: 120.879875, fps: 59.969265810462744
distance: 2.0167186, velocity: 120.87951, fps: 59.96926570579626
<--
表示前一个渲染循环与下一个渲染循环之间的距离突然一半,即使物理速度仍然几乎相同。 (而fps也保持不变)
任何人都可以解释为什么会发生这种情况以及如何解决? 或者如何通过物理模拟精确地获得节点的位置?
更新
当我改变scene.physicsWorld.speed = 0.9
时,它变得更加平滑。
并不意味着它变得一致,但移动量变化周期变短,如下所示。
distance: 3.182204, velocity: 118.95072, fps: 59.969265810462744
distance: 2.9860368, velocity: 119.00602, fps: 59.96926570579626
distance: 2.7921088, velocity: 119.06052, fps: 59.969265810462744
distance: 2.5959413, velocity: 119.11419, fps: 59.96926570579626
distance: 2.4004152, velocity: 119.167046, fps: 59.96926570579626
distance: 2.202648, velocity: 119.219086, fps: 59.969265810462744
distance: 2.0064793, velocity: 119.270325, fps: 59.96926570579626
distance: 1.8064706, velocity: 119.270325, fps: 59.969265810462744
distance: 3.5972602, velocity: 119.3206, fps: 59.96926570579626
distance: 3.4010928, velocity: 119.37006, fps: 59.969265810462744
distance: 3.2049258, velocity: 119.41873, fps: 59.96926570579626
distance: 3.007158, velocity: 119.466606, fps: 59.96926570579626
似乎不是最好的工作,但更好。