相机跟随落球

时间:2015-02-04 19:54:03

标签: swift scenekit

我正在尝试实现一个场景,其中包括一个从高处落下并从地板上弹起的球,我还希望相机在整个行程中跟随球。贝娄是一种尝试,但球似乎不受重力影响,因为代码底部附近的打印输出总是输出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)
    }
}

1 个答案:

答案 0 :(得分:1)

obj.sn_node.presentatioNode.position会给你你想要的东西。