1
votes

I once wrote a function in which I added geometrical cones to the scene. It looked like this:

func addZombies(i:Int, x: Float, y: Float, z: Float)
{
    let node = SCNNode()
    node.geometry = SCNCone(topRadius: 0.0, bottomRadius: 1, height: 2)
    node.position = SCNVector3(x, y, z)
    sceneView.scene.rootNode.addChildNode(node)

    nodes.append(node)
}

where nodes was an array storing SCNNodes. Everything worked fine. If I had moved, the cones would have the exact positions as they had, a true "reality".

However, I purchased a nice 3D model of a zombie and I tried to change cones to the models. I found a solution of some sort and tried to implement it myself. This is what I came up with:

func addZombies(i:Int, x: Float, y: Float, z: Float)
{
    guard let zombieScene = SCNScene(named: "art.scnassets/StrongZombie.DAE") else { return }
    let zombieNode = SCNNode()
    let zombieSceneChildNodes = zombieScene.rootNode.childNodes

    for childNode in zombieSceneChildNodes
    {
        zombieNode.addChildNode(childNode)
    }

    zombieNode.position = SCNVector3(x, y, z)
    zombieNode.scale = SCNVector3(0.5, 0.5, 0.5)
    //zombieNode.look(at: (sceneView.pointOfView?.position)!)
    sceneView.scene.rootNode.addChildNode(zombieNode)

    nodes.append(zombieNode)
} 

Unfortunately, even though it creates the zombies at the specific positions, if I move, the zombies "move" with me. They are displayed in relation to the moving camera which is undesirable for me. Can anyone help me out? I want them like the cones. Thx in advance

2

2 Answers

0
votes

With only seeing the limited code snippet....

I'm thinking it might have something to do with the default physicShape that is being approximated by the original DAE file. The shape can sometimes be overly complex and cause problems with the tracking.

Try setting the zombieNode physicBody to a standard box shape

let box = SCNBox(width:0.01, height: 0.01, length: 0.05, chamferRadius: 0.001)

zombieNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(geometry:box))

Also set debugOptions to show the Physics shapes if you have not already done that. This will help with debugging wants going on with collisions as well.

sceneView.debugOptions = .showPhysicsShapes
0
votes

Instead of loading the dae file and looping through childnodes everytime you add a zombie, store the zombie node in a property/variable earlier on, for example in viewdidload. Then everytime you add a zombie, use the .clone property of that zombie node and add that to the rootnode childnodes instead.

If there is only one object in the dae file, you don’t have to loop through its childnodes, just use childnodes.firstObject.