Search code examples
iosobjective-csprite-kitcollision-detectionskphysicsbody

Different contactTest bit masks on a single object?


Can one physics body object have two different contactTestBitMask? I'm making my first game in Objective-C but I have one object(player) that can collide with two different objects(object1 and object2) and do different things when colliding with one or another, so I have this line:

player.physicsBody.contactTestBitMask = object1Category | object2Category;

But the thing is that when I use the -didBeginContact method, when the player collides with the object1 does some code but when the player collides with the object2 does the same code that when it collides with object1 and I want the player do some different code when collides with object2.


Solution

  • In your didBeginContact method you should have something like this:

    if (collision == (CategoryObjectA | CategoryPlayer)) {
            // code...
    }
    
    if (collision == (CategoryObjectB | CategoryPlayer)) {
            // code...
    }
    

    Obviously you need to set a unique categoryBitMask for object A and for object B and also have their contactTestBitMask set to recognize the player object.