2
votes

I'm trying to create a test 3D game for the iPhone but I'm stuck with the physics. When the car and the ambulance contact each other, the car doesn't disappear? I've worked with SpriteKit before and I know that I had to set the PhysicsWorld ContactDelegate to self but I don't know how to do this in SceneKit, maybe this is the problem?

Code:

ambulance.physicsBody?.categoryBitMask = PhysicsCategory.ambulance
ambulance.physicsBody?.contactTestBitMask = PhysicsCategory.car

car.physicsBody?.categoryBitMask = PhysicsCategory.car


func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
    let firstNode = contact.nodeA
    let secondNode = contact.nodeB

    if(firstNode.physicsBody?.categoryBitMask == PhysicsCategory.ambulance && secondNode.physicsBody?.categoryBitMask == PhysicsCategory.car ||
        firstNode.physicsBody?.categoryBitMask == PhysicsCategory.car && secondNode.physicsBody?.categoryBitMask == PhysicsCategory.ambulance){

        firstNode.physicsBody?.categoryBitMask = PhysicsCategory.ambulance
        secondNode.physicsBody?.categoryBitMask = PhysicsCategory.car

        secondNode.removeFromParentNode()
    }
}

I hope someone can help me! Thanks!

EDIT: I've found how to set the contactDelegate of the scene to self:

scene.physicsWorld.contactDelegate = self
1

1 Answers

1
votes

For everyone who has the same problem: I forgot to set the PhysicsBody of my Node.

Code that I had to add:

ambulance.physicsBody = SCNPhysicsBody.dynamic()
ambulance.physicsBody?.isAffectedByGravity = false

car.physicsBody = SCNPhysicsBody.static()