ARKit 在应用后强制获取节点的位置



同样的问题 像这个问题一样的问题 如何在 Arkit 1.5 中施加力但未得到解答后跟踪节点的位置 所以我正在添加新问题

我有节点(SCNSphere(,我在其中施加力量。节点具有dynamic物理体。滑动时,我使用physicsBody?.applyForceSCNNode施加力 都很好用

问题是在施加力后,因为它dynamic身体不断下降,我想跟踪该位置

renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval)我打印了SCNNodeposition,但它每次都显示相同的位置,为什么Y位置在下降时没有减少?

喜欢

<SCNNode: 0x280f29500 'ball' pos(-0.165368 0.3 -0.809096) | geometry=<SCNSphere: 0x280619380 | radius=0.150> | no child><SCNNode: 0x280f29500 'ball' pos(-0.165368 0.2 -0.809096) | geometry=<SCNSphere: 0x280619380 | radius=0.150> | no child><SCNNode: 0x280f29500 'ball' pos(-0.165368 0.1 -0.809096) | geometry=<SCNSphere: 0x280619380 | radius=0.150> | no child>

等等?

如何添加节点

let ballWithOrientation = getNewBallNode() // return ball node + position
let ballNode = ballWithOrientation.ball // it is the SCNNode
self.sceneView.scene.rootNode.addChildNode(ballNode ?? SCNNode()) // Added to the scene 
let cameraPosition =  self.sceneView.pointOfView?.worldFront
let target =  SCNVector3(Double(cameraPosition?.x ?? 1) * forceVector,Double (cameraPosition?.y  ?? 1) * forceVector,Double(cameraPosition?.z ?? 1) * forceVector) // Vector of direction 
let throwSpeed = self.calculateBestThrowSpeed(origin: ballNode!.position, target:target , timeToTarget: 0.66)  Calculated vector with velocity 
ballNode?.physicsBody?.applyForce(throwSpeed, asImpulse: true) // apply force 

问题施用力后如何跟踪SCNNode的世界位置?

我找到了非常简单的解决方案

open var presentation: SCNNode { get }

SCNNode的此属性将为您提供当前节点的演示

所以最后ballNode.presentation.position将为您提供节点的当前位置以及所有动画(力(

来自苹果文档

使用隐式动画(请参阅 SCNTransaction(更改节点的属性时,这些节点属性将立即设置为其目标值,即使动画节点内容似乎从旧属性值转换到新属性值也是如此。在动画过程中,SceneKit 维护节点的副本(称为演示节点(,其属性反映由当前影响节点的任何动态动画确定的瞬态值。

希望对发现类似问题的人有所帮助

最新更新