Home > AI > Uncategorized

Scenekit – static floor & dynamic box

dynamic box 会停在static floor上,不会穿过。

let scene1 = SCNScene()
        gameView.scene = scene1
        
        let box = SCNNode(geometry: SCNBox(width: 10, height: 10, length: 10, chamferRadius: 0))
        scene1.rootNode.addChildNode(box)
        box.physicsBody = SCNPhysicsBody.dynamic()
        box.geometry?.firstMaterial?.diffuse.contents = "WoodCubeA.jpg"
        
        let floor = SCNNode(geometry: SCNFloor())
        scene1.rootNode.addChildNode(floor)
        floor.physicsBody = SCNPhysicsBody.static()
        floor.geometry?.firstMaterial?.diffuse.contents = "wood.png"
        
        let camera = SCNNode()
        camera.camera = SCNCamera()
        camera.position = SCNVector3(0, 20, 20)
        camera.rotation = SCNVector4(1, 0, 0, -Double.pi / 4)
        scene1.rootNode.addChildNode(camera)

一个项目中box是dynamic,floor是static,设置正常,可box总是穿过floor,经过艰辛的回到最简单状态,再一一比较,发现原来问题在这:

原来错误的:
floor.physicsBody? = SCNPhysicsBody.static()
正确的:
floor.physicsBody = SCNPhysicsBody.static()

发现细节没?physicsBody后面不能有问号!!有了问号,这句话其实没走。

Related posts:

Leave a Reply