Search code examples
scenekitgame-physicsscnnode

In scene kit, adding a physics body manually (in code) to a node, seems to not work? You have to add the physics body in the editor?


It seems you just can't add a physics body in code.

Not much more can be said than this, has anyone found this issue?

Some example code ...

class SomeSceneView:  SCNView {
    public var box: SCNNode!
    
    .. 

    var physBody = SCNPhysicsBody()
    
    private func bringup() {
        box = scene?.rootNode.childNode(withName: "box", recursively: true)
        
        physBody.type = .dynamic
        physBody.isAffectedByGravity = false
        physBody.friction = 0

        box.physicsBody = physBody

        rendersContinuously = true
        play(nil)
    }
}

Any experience with this?


Solution

  • You need also to file a SCNPhysicsShape.

    Try something like this:

    func makeNode() {
        let node = SCNNode(geometry: SCNSphere(radius: 1.0))
        ...
        node.physicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(geometry: node.geometry, options: nil))
        ...
        scene.rootnode.addChildNode(node)
    }