2
votes

Collision detection is not calling the NSLog in the didBeginContact function. The physics are working fine... Can someone show me what I've done wrong? Everything I've tried is giving me the same result.

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

let category0: UInt32 = 0x1 << 0
let category1: UInt32 = 0x1 << 1
let category2: UInt32 = 0x1 << 2
let category3: UInt32 = 0x1 << 4
let position1:CGFloat = 100

override func didMoveToView(view: SKView) {

    self.physicsBody = SKPhysicsBody (edgeLoopFromRect:self.frame)
    self.physicsWorld.gravity = CGVectorMake(0, -9)
    physicsWorld.contactDelegate = self

    let ground = SKSpriteNode(color: UIColor .redColor(), size: CGSizeMake(self.size.width, self.size.height/16))
    ground.position = CGPointMake(self.size.width/2, self.size.height - self.size.height + ground.size.height)
    ground.zPosition = position1
    self.addChild(ground)
    ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size)
    ground.physicsBody?.categoryBitMask = category2
    ground.physicsBody?.contactTestBitMask = category1
    ground.physicsBody?.collisionBitMask = category1
    ground.physicsBody?.dynamic = false
    ground.physicsBody?.affectedByGravity = false
    ground.name = "Ground"

    let launchingPad = SKSpriteNode(color: UIColor .redColor(), size: CGSizeMake(self.size.width/6, self.size.height/16))
    launchingPad.position = CGPointMake(self.size.width - launchingPad.size.width/2 - 5, self.size.height - self.size.height + launchingPad.size.height * 4)
    launchingPad.zPosition = position1
    self.addChild(launchingPad)
    launchingPad.physicsBody = SKPhysicsBody(rectangleOfSize: launchingPad.size)
    launchingPad.physicsBody?.categoryBitMask = category1
    launchingPad.physicsBody?.contactTestBitMask = category2
    launchingPad.physicsBody?.collisionBitMask = category2
    launchingPad.physicsBody?.dynamic = true
    launchingPad.physicsBody?.affectedByGravity = true
    launchingPad.name = "LaunchingPad"

    func didBeginContact(contact: SKPhysicsContact) { 

        let firstBody = contact.bodyA.node as! SKSpriteNode

        let secondBody = contact.bodyB.node as! SKSpriteNode

        if ((firstBody.name == "LaunchingPad") && (secondBody.name == "Ground")){

NSLog("Contact")

}}}}
1
How do you know firstBody is LaunchingPad? what if firstBody is Ground and secondBody is LaunchingPad, need to handle for this caseKnight0fDragon
Also your fun didBeginContact is INSIDE your viewDidLoad, this will not do you any good in thereKnight0fDragon
I guess by setting ground dynamic to false, this will guarantee that firstBody is always LaunchingPad, but if you try to compare 2 dynamic objects colliding, order would not be preservedKnight0fDragon
@Knight0fDragon You have helped me greatly. Ive posted the answer to my question based on your comments. Its now working perfect. Thank you :)Jeff

1 Answers

2
votes

Answering my own question based on KnightOfDragon comments. Here is the working method.

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 == category1) && (secondBody.categoryBitMask == category2)){
    NSLog("contact")
}
}