zezenzo
zezenzo

Reputation: 21

Converting "SCNScene" to "SCNNode"

I am doing an ARKit app for my IT assignment and I followed a guide to bit-masking and collision and I got it to work however it only works with a simple box and not my 3D model so is there a way to convert this code to the one at the bottom or am I doing something wrong? because the top code doesn't appear however the bottom one does:

class Monster: SCNNode {

    override init () {
        super.init()

    guard let virtualObjectScene = SCNScene(named: "Monster.scn", inDirectory: "art.scnassets" ) else {
        return
    }

    let wrapperNode = SCNNode()
    for child in virtualObjectScene.rootNode.childNodes {
        child.geometry?.firstMaterial?.diffuse.contents = UIColor.black
        wrapperNode.addChildNode(child)
    }
    wrapperNode.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.dynamic, shape: nil)
    wrapperNode.physicsBody?.isAffectedByGravity = false
    wrapperNode.physicsBody?.categoryBitMask = CollisionCategory.ship.rawValue
    wrapperNode.physicsBody?.collisionBitMask = CollisionCategory.bullet.rawValue
    wrapperNode.physicsBody?.contactTestBitMask = CollisionCategory.bullet.rawValue
    wrapperNode.pivot = SCNMatrix4MakeRotation(Float(CGFloat(Double.pi/2)), 1, 0, 0)

    print("Placing Monster")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

To something like this because for some reason this code works and the top one doesn't, Am i doing something wrong?

class Bullet: SCNNode {
    override init () {
        super.init()
        let sphere = SCNSphere(radius: 0.025)
        self.geometry = sphere
        let shape = SCNPhysicsShape(geometry: sphere, options: nil)
        self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: shape)
        self.physicsBody?.isAffectedByGravity = false
        self.physicsBody?.categoryBitMask = CollisionCategory.bullet.rawValue
        self.physicsBody?.collisionBitMask = CollisionCategory.ship.rawValue

        // add texture
        let material = SCNMaterial()
        material.diffuse.contents = UIImage(named: "bullet_texture.jpg")
        self.geometry?.materials  = [material]
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Thanks

Upvotes: 1

Views: 1139

Answers (1)

zezenzo
zezenzo

Reputation: 21

Use: SCNReferenceNode

Website: [1]: https://developer.apple.com/documentation/scenekit/scnreferencenode/

Usage: Define the Path and use that as the reference Node

Upvotes: 1

Related Questions