我正在研究一个基于命令行的SceneKit物理项目。这是我编写的所有代码:
import Foundation
import SceneKit
let level = SCNScene(named: "levels.scnassets/plains.scn")!
let character = SCNScene(named: "characters.scnassets/sphere.scn")!.rootNode.childNode(withName: "Character", recursively: false)!
character.position = SCNVector3(0, 100, 0)
level.rootNode.addChildNode(character)
class RendererDelegate: NSObject, SCNSceneRendererDelegate {
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
print("Position:", character.position, "Velocity:", character.physicsBody!.velocity)
}
}
let rendererDelegate = RendererDelegate()
let sceneView = SCNView()
sceneView.scene = level
sceneView.delegate = rendererDelegate
let window = NSWindow() // A window is required to start the scene, not displayed
window.contentView = sceneView
RunLoop.main.run()
由于某些原因,character
节点的速度正在按照预期的方式根据物理进行更新。但是,position
属性没有更新。这是一些示例输出:
Position: SCNVector3(x: 0.0, y: 100.0, z: 0.0) Velocity: SCNVector3(x: 0.0, y: 0.0, z: 0.0)
Position: SCNVector3(x: 0.0, y: 100.0, z: 0.0) Velocity: SCNVector3(x: 0.0, y: 0.0, z: 0.0)
Position: SCNVector3(x: 0.0, y: 100.0, z: 0.0) Velocity: SCNVector3(x: 0.0, y: -0.16333334147930145, z: 0.0)
Position: SCNVector3(x: 0.0, y: 100.0, z: 0.0) Velocity: SCNVector3(x: 0.0, y: -0.3263801336288452, z: 0.0)
Position: SCNVector3(x: 0.0, y: 100.0, z: 0.0) Velocity: SCNVector3(x: 0.0, y: -0.4891408681869507, z: 0.0)
Position: SCNVector3(x: 0.0, y: 100.0, z: 0.0) Velocity: SCNVector3(x: 0.0, y: -0.6516160368919373, z: 0.0)
Position: SCNVector3(x: 0.0, y: 100.0, z: 0.0) Velocity: SCNVector3(x: 0.0, y: -0.8138061761856079, z: 0.0)
我不确定如何为character
节点获得正确的位置。我看不到物理物体上的任何位置属性,并且position
,worldPosition
和simdPosition
都提供相同的静态位置。
如何获得该节点的正确位置?