I am trying to make a game with a player block and a few obstacle blocks. The player can collide with the obstacle but the obstacles should not collide with each other but instead "pass through" each other. I have the following enumeration for my bit mask:
enum bitMask: UInt32 {
case player = 1
case obstacle = 2
case frame = 4
}
This is how I define my player block:
let player = SKSpriteNode(imageNamed: "yellow-square")
player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.frame.size)
player.physicsBody!.dynamic = false
player.physicsBody!.categoryBitMask = bitMask.player.rawValue
player.physicsBody!.contactTestBitMask = bitMask.obstacle.rawValue
player.physicsBody!.collisionBitMask = 0
addChild(player)
player.physicsBody!.contactTestBitMask = bitMask.obstacle.rawValue
player.physicsBody!.collisionBitMask = bitMask.frame.rawValue
my obstacle blocks (in a for loop):
let obstacle = SKSpriteNode(imageNamed: "black-square")
obstacle.physicsBody = SKPhysicsBody(texture: obstacle.texture!, size: obstacle.frame.size)
boundaryNode.addChild(obstacle)
obstacle.physicsBody!.applyImpulse(CGVectorMake(-15, -10))
obstacle.physicsBody!.categoryBitMask = bitMask.obstacle.rawValue
obstacle.physicsBody!.contactTestBitMask = bitMask.player.rawValue
and my contact function:
func didBeginContact(contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch(contactMask) {
case bitMask.player.rawValue | bitMask.obstacle.rawValue:
let yourTimeNode = contact.bodyB.node
yourTimeNode?.physicsBody?.allowsRotation = false
let firstNode = contact.bodyA.node
firstNode?.physicsBody?.allowsRotation = false
print("Collision")
default:
return
}
}
Currently every block collides with each other and I do not know how to turn off collision between obstacles and keep the collision between the obstacle and player block.