At the end I figured out how to solve this.
First I added the categoryBitMask
and the collisionBitMask
for both objects, the spaceship and the asteroids.
ship.physicsBody?.categoryBitMask = 2
asteroid.physicsBody?.categoryBitMask = 4
ship.physicsBody?.collisionBitMask = 4
ball.physicsBody?.collisionBitMask = 2
Then I gave a dynamicBody()
to both of them.
ship.physicsBody = SCNPhysicsBody.dynamicBody()
asteroid.physicsBody = SCNPhysicsBody.dynamicBody()
Then I added a NSTimer
to help me with the contact detection to update each 0.1 seconds to see if there is a contact.
override func viewDidLoad()
{
NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "checkContact", userInfo: nil, repeats: true)
}
func checkContact()
{
if (self.scene.physicsWorld.contactTestBetweenBody(ship.physicsBody, andBody: asteroid.physicsBody, options: nil) != nil)
{
println("CONTACT!")
}
}
I didn't needed to add the SCNPhysicsContactDelegate
to my view, but is better to add it to be sure there are no syntax problems or warnings.
And that's it!
NOTE: It's important to add a dynamicBody()
or staticBody()
to our 3D objects, otherwise it won't have a contact. And it's important to add the dynamicBody()
or staticBody()
to our 3D objects BEFORE we add them as a child to our scene.
Hope this helps someone!
SCNPhysicsBody
doesn't have acontactTestBitMask
. – rickstercategoryBitMask
forcontactTestBitMask
, I'll change it right now. – Charls Pico