2

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

Ari
  • 1,819
  • 14
  • 22
Jacob Cafiero
  • 135
  • 1
  • 1
  • 9
  • Possible duplicate of [beginner swift sprite kit - node collision detection help (SKPhysicsContact)](http://stackoverflow.com/questions/26270504/beginner-swift-sprite-kit-node-collision-detection-help-skphysicscontact) – Zane Helton Mar 27 '16 at 01:04
  • Have you set the class in which you have didBeginContact To have to Physivs Contact Delegate protocol and have you set the 'delegate' property? After doing that, put a print("Contact detected") in your dudBeginContact to see if it's getting called (or, more properly, set a breakpoint). – Steve Ives Mar 27 '16 at 07:26
  • You can't replace firstBody and secondBody yet as you need to examine their categoryBitMasks to find out which is the cat and which is the ground. – Steve Ives Mar 27 '16 at 07:51

1 Answers1

2

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

crashoverride777
  • 10,581
  • 2
  • 32
  • 56