我正在尝试实现一个场景,该场景由一个从高处掉下并从地板上弹起的球组成,我也希望摄像机在整个旅程中都能跟踪球。波纹管是一种尝试,但是球似乎不受重力影响,因为靠近代码底部的println始终输出100。但是实际上,球在屏幕上一会儿很快就掉出了屏幕。我做错了什么?

class obj {
    var sn_node: SCNNode
    init() {
        let geom = SCNSphere(radius: 2.0)
        sn_node = SCNNode(geometry: geom)
        sn_node.position = SCNVector3Make(0, 100, 0)
        sn_node.physicsBody = SCNPhysicsBody.dynamicBody()
    }
}

var obj = obj()

class SceneKitController: NSViewController,SCNSceneRendererDelegate {

    var _scene:SCNScene!
    var _cameraNode:SCNNode!
    var _floorNode:SCNNode!

    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }

    func setup() {
        let sceneView = view as SCNView

        sceneView.backgroundColor = NSColor.blackColor()

        setupScene()
        sceneView.scene = _scene
        sceneView.delegate = self

        sceneView.jitteringEnabled = true
        sceneView.autoenablesDefaultLighting = true

        var newPos = obj.sn_node.position
        newPos.z += 10
        _cameraNode.position = newPos
        sceneView.pointOfView = _cameraNode
    }

    func setupScene() {
        _scene = SCNScene()
        setupEnviroment()
        setupSceneElements()
        setupInitial()
    }

    func setupEnviroment() {
        //create main camera
        let camera = SCNCamera()
        _cameraNode = SCNNode()
        _cameraNode.camera = camera

        //floor
        var floor = SCNFloor()
        floor.reflectivity = 0;
        _floorNode = SCNNode(geometry: floor)
        _floorNode.physicsBody = SCNPhysicsBody.staticBody()
        _scene.rootNode.addChildNode(_floorNode)
    }

    func setupSceneElements() {

    }

    func setupInitial() {
        _scene.rootNode.addChildNode(obj.sn_node)
    }

    func renderer(aRenderer: SCNSceneRenderer, didSimulatePhysicsAtTime time: NSTimeInterval){
        var newPos = obj.sn_node.position
        println(newPos.y)
    }
}

最佳答案

obj.sn_node.presentatioNode.position将为您提供所需的内容。

关于swift - 相机跟随落球,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28330231/

10-12 05:42