1

I am trying to not get contact with caveman and other images with collision bitmasks but my caveman hits everything.

func addCaveManBitMasks(){

    caveManNode.physicsBody?.categoryBitMask = PhysicsCategory.caveman
    caveManNode.physicsBody?.contactTestBitMask = PhysicsCategory.tri | PhysicsCategory.trex | PhysicsCategory.newblock |  PhysicsCategory.fireBall | PhysicsCategory.waterblock | PhysicsCategory.secondwaterblock | PhysicsCategory.star
    caveManNode.physicsBody?.collisionBitMask = PhysicsCategory.newblock
}
func addTriBitMasks(){

    triImage.physicsBody?.categoryBitMask = PhysicsCategory.tri
    triImage.physicsBody?.contactTestBitMask =  PhysicsCategory.caveman
    triImage.physicsBody?.collisionBitMask = 0
}
func addRexBitMasks(){
    tRexImage.physicsBody?.categoryBitMask = PhysicsCategory.trex
    tRexImage.physicsBody?.contactTestBitMask =  PhysicsCategory.caveman
    tRexImage.physicsBody?.collisionBitMask = 0
}
func addNewBlockManBitMasks(){
    newBlockImageNode.physicsBody?.categoryBitMask = PhysicsCategory.newblock
    newBlockImageNode.physicsBody?.contactTestBitMask = PhysicsCategory.caveman | PhysicsCategory.steg
    newBlockImageNode.physicsBody?.collisionBitMask = PhysicsCategory.caveman
}
func addFireBallBitMasks(){
    fireBall.physicsBody?.categoryBitMask = PhysicsCategory.fireBall
    fireBall.physicsBody?.contactTestBitMask = PhysicsCategory.caveman
    fireBall.physicsBody?.collisionBitMask = 0
}
func addStarBitMasks(){
    onScreenStar.physicsBody?.categoryBitMask = PhysicsCategory.star
    onScreenStar.physicsBody?.contactTestBitMask = PhysicsCategory.caveman
    onScreenStar.physicsBody?.collisionBitMask = 0
}


struct PhysicsCategory {

    static let caveman: UInt32 = 0x1 << 0
    static let tri: UInt32 = 0x1 << 1
    static let trex: UInt32 = 0x1 << 2
    static let fireBall: UInt32 = 0x1 << 3
    static let steg: UInt32 = 0x1 << 4
    static let ptero: UInt32 = 0x1 << 5
    static let waterblock: UInt32 = 0x1 << 6
    static let secondwaterblock: UInt32 = 0x1 << 7
    static let newblock: UInt32 = 0x1 << 8
    static let star: UInt32 = 0x1 << 9
    static let food: UInt32 = 0x1 << 10


}

the tri, rex, fireball, and newblock don't contact with one another which is perfect, but I also need the caveman to do the same. Caveman should only collide with the new block which it does. I am very confused and an explanation would be much appreciated

  • When you say "Caveman should only collide with the new block which it does" does you mean that it does only collide with new block or that it does collide with new block but also collides with everything else? And you mean collide, not contact? – Steve Ives Apr 14 '17 at 21:55
  • only the caveman collides with the new block, everything else passes through. The problem only lies with the Tri, Rex, and Fireball with the caveman – Aditya Vadrevu Apr 17 '17 at 20:04

2 Answers2

0

It's because you have all the other objects collisionBitMask set to the same as the caveman. Change the collision bit masks of those other objects to a bit mask you haven't already used on another node for it's category and you'll be good.

TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30
  • So change the collisionBitMask from 0 to a different number like 11 or so? I have tried that and it does the same thing. – Aditya Vadrevu Apr 14 '17 at 20:25
  • Change it to an actual UInt32 address – TheValyreanGroup Apr 14 '17 at 20:30
  • Changing it to an address didn't work either I added this and set the collision bitMask = physicscategory.temp static let temp: UInt32 = 0x1 << 11 – Aditya Vadrevu Apr 14 '17 at 20:46
  • Are you talking about contacts or collisions? – TheValyreanGroup Apr 14 '17 at 20:48
  • Collisions sorry, I typed contact when it should have been colliding. The names are a bit tricky – Aditya Vadrevu Apr 14 '17 at 20:50
  • To clarify, I want the other objects to go through the caveman. I understand the contact triggers the didbegin and thats not what I am trying to do. – Aditya Vadrevu Apr 14 '17 at 20:56
  • Setting the collisionBitMask of a physics body to 0 should make that body not collide with anything. Is that what you are seeing? – Steve Ives Apr 14 '17 at 21:47
  • Take for example the Trex and the caveman, The caveman is not moving and the TRex is moving left and right across the screen. If the Trex collides with the caveman it will push him along making the caveman move through the objects the trex can pass through without collision. It looks like the caveman is glitching out, I don't know how else to describe it. The caveman's isDynamic is set to true and everything else the isDynamic is set to false. – Aditya Vadrevu Apr 14 '17 at 22:02
0

To have the tRex, the Tri and the fireball collide with the caveman (and nothing but the caveman), you will need:

tRexImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman
triImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman
firBallImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman

If the caveMan is supposed to collide with the three objects above AND the newBlock you will need:

let enemyCategory = PhysicsCategory.trex |PhysicsCategory.tri |PhysicsCategory.fireBall
caveManNode.physicsBody?.collisionBitMask = PhysicsCategory.newblock | enemyCategory

If the trex, tri and fireBall are also supposed to collide with each other, then you'll need:

tRexImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman | enemyCategory
triImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman | enemyCategory
firBallImage.physicsBody?.collisionBitMask = PhysicsCategory.caveman | enemyCategory

Here is a sample Xcode Sprite-kit project that attempts to demonstrate their use - Attack button in SpriteKit

Use the checkPhysics() function from the above project - it will list what object collide and what objects generate contacts. Call it once you have created all the physics bodies and contactTest/collision bit masks.

Graham
  • 7,431
  • 18
  • 59
  • 84
Steve Ives
  • 7,894
  • 3
  • 24
  • 55