Jacob Cafiero
Jacob Cafiero

Reputation: 145

How to detect when two objects touch in SpriteKit

I have found nothing on the internet about how to do this. Im simply trying to run a line of code when to physics body touch. In this case I have an SKSpriteNode with a physics body and another for the ground. When they touch it should run the line of code this is all I have found so far.

    let catGroup:UInt32 = 0x1 << 0
let groundGroup:UInt32 = 0x2 << 1
    cat.physicsBody?.categoryBitMask = catGroup
    cat.physicsBody?.contactTestBitMask = groundGroup
    ground.physicsBody?.categoryBitMask = groundGroup
    ground.physicsBody?.contactTestBitMask = catGroup

and here is where I'm confused

    func didBeginContact(contact: SKPhysicsContact) {

    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
    {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    }
    else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    if firstBody.categoryBitMask==0 && secondBody.categoryBitMask==1 {
        print("contact")
    }

}

so should i be replacing first body and second body with catGroup and groundGroup? Im not sure how to do this

Upvotes: 2

Views: 1480

Answers (1)

crashoverride777
crashoverride777

Reputation: 10674

Not to be a stickler but you shouldn't start of a question with "I couldn't find anything on the internet" when thats blatantly not true. There is a million tutorials on collision detection around as its one of the basics in SpriteKit.

Now to your question. You did not give your sprites an actual physics body and your physics categories are set up weird. Change your code to this

 struct PhysicsCategory {
       static let cat:UInt32 = 0x1 << 0
       static let ground:UInt32 = 0x1 << 1
 }

 class GameScene: SKScene, SKPhysicsContactDelegate {


     override func didMoveToView(view: SKView) {
     /* Setup your scene here */

         physicsWorld.contactDelegate = self


         cat.physicsBody = SKPhysicsBody(rectangleOfSize: cat.size) // FORGOT THIS
         cat.physicsBody?.categoryBitMask = PhysicsCategory.cat
         cat.physicsBody?.contactTestBitMask = PhysicsCategory.ground

         ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size) // FORGOT THIS
         ground.physicsBody?.categoryBitMask = PhysicsCategory.ground
         ground.physicsBody?.contactTestBitMask = PhysicsCategory.cat // You dont really need this line as long as you have set it on the other body.
    }

    func didBeginContact(contact: SKPhysicsContact) {
         var firstBody: SKPhysicsBody
         var secondBody: SKPhysicsBody

        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            firstBody = contact.bodyA
            secondBody = contact.bodyB
        } else {
            firstBody = contact.bodyB
            secondBody = contact.bodyA
        }

         if firstBody.categoryBitMask == PhysicsCategory.cat && secondBody.categoryBitMask == PhysicsCategory.ground {
             print("contact")
         }
     } 
 }

If you dont want your objects to fall you have to turn gravity off, which is on by default.

 cat.physicsBody?.affectedByGravity = false
 ground.physicsBody?.affectedByGravity = false

Hope this helps

Upvotes: 2

Related Questions