SceneKit基础知识

时间:2014-10-03 00:46:21

标签: swift scenekit

我刚开始尝试使用SceneKit。尝试一个非常简单的代码来创建一个对象,为它分配物理,给它一个点击速度。到目前为止,我能够创建它,应用物理学。我能够给它速度,但无法用水龙头实现这一点。我究竟做错了什么?我使用的是我在SpriteKit中使用的相同技术,但它在SceneKit中不起作用。请帮忙。

class GameViewController: UIViewController, SCNSceneRendererDelegate, SCNPhysicsContactDelegate {
let scene = SCNScene()

let shipNode = SCNNode()

let sceneView = SCNScene()

var cameraNode = SCNNode()
var floor = SCNFloor()
var floornod = SCNNode()

override func viewDidLoad() {
    super.viewDidLoad()

    let sceneView = self.view as SCNView
    sceneView.scene = scene
    sceneView.backgroundColor = SKColor.blueColor()
    sceneView.showsStatistics = true


    sceneView.scene?.physicsWorld.gravity = SCNVector3Make(0, -70, 0)
    sceneView.scene?.physicsWorld.speed = 1.0
    sceneView.delegate = self
    sceneView.jitteringEnabled = true
    sceneSetup()
    sceneView.pointOfView = cameraNode


    let ship = SCNScene(named: "art.scnassets/sphere.dae")!

    let shipNode = ship.rootNode.childNodeWithName("Sphere", recursively: true)!

    shipNode.position = SCNVector3Make(0, 50, 300)
    shipNode.scale = SCNVector3Make(10, 10, 10)
    shipNode.geometry?.firstMaterial?.diffuse.contents = UIImage(named: "ball.jpg")
    shipNode.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Dynamic, shape: nil)
    shipNode.physicsBody?.restitution = 0.9
    shipNode.physicsBody?.velocity = SCNVector3Make(0, 100, 10)
    scene.rootNode.addChildNode(shipNode)
    println(shipNode.physicsBody?.velocity.y)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    shipNode.physicsBody?.velocity = SCNVector3Make(0, 200, 20) // code not working
    println(shipNode.physicsBody?.velocity.y) // prints nil instead of 200.00
}

    func sceneSetup() {

    cameraNode.camera = SCNCamera()
    cameraNode.camera?.zNear = 0.01
    cameraNode.camera?.zFar = 800
    cameraNode.camera?.xFov = 45
    cameraNode.camera?.yFov = 75


    cameraNode.rotation = SCNVector4Make(1, 0, 0, -25 * 0.017453293)
    cameraNode.position = SCNVector3Make(0, 150, 500)
    scene.rootNode.addChildNode(cameraNode)

    floornod.geometry = floor
    floornod.geometry?.firstMaterial?.diffuse.contents = "wood1.png"
    floornod.geometry?.firstMaterial?.locksAmbientWithDiffuse = true
    floornod.geometry?.firstMaterial?.diffuse.wrapS = SCNWrapMode.Repeat
    floornod.geometry?.firstMaterial?.diffuse.wrapT = SCNWrapMode.Repeat
    floornod.geometry?.firstMaterial?.diffuse.mipFilter = SCNFilterMode.Linear
    floornod.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Static, shape: nil)
    floornod.position = SCNVector3Make(0, 0, 0)

    scene.rootNode.addChildNode(floornod)

}

}

第一个println给出100.00的结果。 每次点击时,touchesBegan中的println都会得到nil的结果。

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:2)

您可以使用viewDidLoadlet shipNode = ...方法中定义新的局部变量。这个获得物理身体并被放入场景。

这与您定义为类的一部分的实例变量shipNode是分开的。那个从来没有得到任何进一步的配置,所以当你在touchesBegan中访问它时,它没有物理体,并且可选的链接导致查询其速度的整个表达式返回nil。

据推测,您希望摆脱let中的viewDidLoad,以便您配置实例变量而不是遮蔽它们。