I have 4 physics bodies that are all detecting collisions well. However, there is two physics bodies that will not detect when they collide with each other. They do detect when they collide with other physics bodies though. I have contacttestbitmasks for all the them so I do not understand why there is a problem. Here is some code: Here is where I setup my physics bodies:
struct PhysicsCategory{
static let None : UInt32 = 0
static let All : UInt32 = UInt32.max
static let player : UInt32 = 0b1
static let bounce : UInt32 = 0b10
static let blueball : UInt32 = 0b100
static let redball : UInt32 = 0b1000
static let coin : UInt32 = 0b10000
}
Here is the code where I used for setting up the player physics body which is one of the problem physic bodies:
player.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: player.size.width-40, height: player.size.height-40))
player.physicsBody?.isDynamic = true
player.physicsBody?.categoryBitMask = PhysicsCategory.player
player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball
player.physicsBody?.contactTestBitMask = PhysicsCategory.redball
player.physicsBody?.collisionBitMask = PhysicsCategory.None
Here is the func for detecting collisions:
func didBegin(_ 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.player != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.redball != 0)) {
RedballDidCollideWithPlayer(player: firstBody.node as! SKSpriteNode, redball: secondBody.node as! SKSpriteNode)
}
if ((firstBody.categoryBitMask & PhysicsCategory.player != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.blueball != 0)) {
BlueballDidCollideWithPlayer(player: firstBody.node as! SKSpriteNode, blueball: secondBody.node as! SKSpriteNode)
}
if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.redball != 0)){
RedballDidCollideWithBounce(bounce: firstBody.node as! SKSpriteNode, redball: secondBody.node as! SKSpriteNode)
}
if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.blueball != 0)) {
BlueballDidCollideWithBounce(bounce: firstBody.node as! SKSpriteNode, blueball: secondBody.node as! SKSpriteNode)
}
if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.coin != 0)) {
coinDidCollideWithplayer(player: firstBody.node as! SKSpriteNode, coin: secondBody.node as! SKSpriteNode)
}
if ((firstBody.categoryBitMask & PhysicsCategory.player != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.coin != 0)) {
coinDidCollideWithplayer(player: firstBody.node as! SKSpriteNode, coin: secondBody.node as! SKSpriteNode)
}
}
Here is the code I used for setting up the blueball. This is the other physics body having problems:
let blueball = SKSpriteNode(imageNamed: "blueball")
blueball.position = enemyb.position
blueball.physicsBody = SKPhysicsBody(circleOfRadius: blueball.size.width/2)
blueball.physicsBody?.isDynamic = true
blueball.physicsBody?.categoryBitMask = PhysicsCategory.blueball
blueball.physicsBody?.contactTestBitMask = PhysicsCategory.player
blueball.physicsBody?.contactTestBitMask = PhysicsCategory.bounce
blueball.physicsBody?.collisionBitMask = PhysicsCategory.None
blueball.physicsBody?.usesPreciseCollisionDetection = true
addChild(blueball)
let actionMove = SKAction.move(to: player.position, duration: 2)
Any ideas here would be helpful. I have been trying to find the problem for a few days with no luck.