0

I want to detect in my app a contact between two objects. I have an enum to define the different types:

enum ColliderType: UInt32 {

    case Ball = 0b010
    case Object = 0b001
    case Gap = 0b100

}

I also added SKPhysicsContactDelegate to my SKView:

In didMoveToView, I have

self.physicsWorld.contactDelegate = self

So here are my objects that I defined

ball = SKSpriteNode(texture: ballTexture)
    ball.position = CGPoint(x: CGRectGetMidX(self.frame) - self.frame.width/12, y: self.frame.size.height - (self.frame.size.height - 100))
    ball.physicsBody = SKPhysicsBody(circleOfRadius: ballTexture.size().height)
    ball.physicsBody!.dynamic = false
    ball.physicsBody?.allowsRotation = false
    ball.physicsBody!.categoryBitMask = ColliderType.Ball.rawValue
    ball.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
    ball.physicsBody!.collisionBitMask = ColliderType.Object.rawValue


let boxTexture = SKTexture(imageNamed: "Obstacle.png")
    let box1 = SKSpriteNode(texture: boxTexture)
    box1.size = CGSizeMake(self.frame.width/4, self.frame.width/8)
    box1.runAction(moveAndRemoveBoxes)
    box1.physicsBody = SKPhysicsBody(rectangleOfSize: box1.size)
    box1.physicsBody!.dynamic = false
    box1.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
    box1.physicsBody!.contactTestBitMask = ColliderType.Ball.rawValue
    box1.physicsBody!.collisionBitMask = ColliderType.Object.rawValue

let gap = SKSpriteNode()
    gap.size = box1.size
    gap.physicsBody = SKPhysicsBody(rectangleOfSize: box1.size)
    gap.physicsBody!.dynamic = false
    gap.physicsBody!.categoryBitMask = ColliderType.Gap.rawValue
    gap.physicsBody!.contactTestBitMask = ColliderType.Ball.rawValue
    gap.physicsBody!.collisionBitMask = ColliderType.Gap.rawValue

In the didBeginContact function I have:

func didBeginContact(contact: SKPhysicsContact) {

    if contact.bodyA.categoryBitMask == ColliderType.Gap.rawValue || contact.bodyB.categoryBitMask == ColliderType.Gap.rawValue {

        print("score")
        score++

        scoreLabel.text = String(score)

    } else {

        if gameOver == false {

            gameOver = true

            self.speed = 0


        }

    }
}

If you tap the screen the ball objects jumps from half of the screen to other, and the object called box1 is "falling down" on one half of the screen and the "invisible" gap to detect if the player passed without colliding with the box is on the other half of the screen. So I want to detect if the ball touches the gap, so that the players score, otherwise the ball collides with the box

henrik-dmg
  • 1,448
  • 16
  • 23

1 Answers1

1

This is because your obstacles contactTestBitMask is looking for Object, not Ball. You want this: box1.physicsBody!.contactTestBitMask = ColliderType.Ball.rawValue

In light of the new changes:

ball.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue | ColliderType.Gap.rawValue

You need to be checking for gap value if you want contact to work

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • This doesn't change anything unfortunately, I now set ball.dynamic to false, but I still don't get any output from didBeganContact. First I thought it worked but then I realized it was just the ball colliding with the view – henrik-dmg Dec 22 '15 at 02:41
  • well that is because you are not declaring your ball property correctly, you can't have a value of 0 as a bit mask, make it 0b010 – Knight0fDragon Dec 22 '15 at 03:37
  • What item is suppose to be colliding here? – Knight0fDragon Dec 22 '15 at 03:47
  • Sorry, forgot to add the gap object in the question – henrik-dmg Dec 22 '15 at 03:50
  • You really need to read up on sprite kit more before asking on SO, you have zero idea on how your code works. The only thing you are checking for in your contact is Gap, but none of your SpriteNodes are looking to contact Gap, so yes, it is always going to fail – Knight0fDragon Dec 22 '15 at 03:53
  • It's basically the same code as in an example app that I downloaded, and this works. But thanks – henrik-dmg Dec 22 '15 at 04:06
  • on SO, if you do not show that you are doing the research on your own, people will down vote your questions, down voted questions tend to not get answered or are scrutinized by the community. There are lots of tutorials out there – Knight0fDragon Dec 22 '15 at 04:10
  • I'll delete this question – henrik-dmg Dec 22 '15 at 04:11