Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Category Bit And Mask Bit Filter

    • 0
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 475
    Comment on it

    In Cocos2d,when you define a world for a scene by default all object heaving body in scene will collide with each other.In order to make object collidable with specific one,simply set body mask and category bit at the time of defining fixtures,and now you are able to do this.

    Let me explain with an example :

    Suppose we have weapons , asteroid, frog and insects in our game.All heaving different behaviour . Frog will collide with asteroid,but not collide with weapon .Weapon will collide with insects,but not collide with frog and asteroid.Insects will collide with weapon,but not collide with asteroid and frog. And the last one Asteroid,will collide with frog but not collide with weapon and insects.

    Now Time for setting category and mask bit for all.

    First create a separate class naming "CategoryClass" that extends CCSprite.

    Take short type variable one for defining category bit and one for mask bit.In this way:-

    short frog;
    short asteroid;
    short weapon;
    short insects;
    
    short frogMask;
    short asteroidMask;
    short weaponMask;
    short insectsMask;
    
     frog = 0x0001;
     asteroid = 0x0002;
     insects = 0x0004;
     weapon = 0x0008;
    
    // Here we are simply defining,that object heaving that particular mask will collide with the 
      defined one only.
    
    // Frog will collide with asteroid 
      frogMask = asteroid;
    
    // Asteroid will collide with frog
     asteroidMask=frog;
    
    // Weapon will collide with insects
     weaponMask=insects;
    
    // Insects will collide with weapon
     insectsMask =weapon;
    
    // Create its getter 
    
    -(short)getFrog
    {
        return frog;
    }
    -(short)getFrogMask
    {
        return frogMask;
    }
    
    -(short) getWeapon
    {
        return weapon;
    }
    -(short) getWeaponsMask
    {
        return weaponMask;
    }
    
    -(short)getAsteroid
    {
        return asteroid;
    }
    -(short)getAsteroidMask
    {
        return asteroidMask;
    }
    
    -(short)getInsects
    {
        return insects;
    }
    -(short)getInsectsMask
    {
        return insectsMask; 
    }
    

    Then finally,In layer or in a scene wherever your are creating fixtures for body ,set these bits.Like:-

    // Create category class Object first,
    
     CategoryClass* categoryObj = [CategoryClass category];
    
    // Then at the time of creating fixture,just sets body category and mask bit.
    
    
    // For Frog
    
      b2FixtureDef fixtureDef;
    
      fixtureDef.filter.categoryBits=categoryObj.getFrog;
      fixtureDef.filter.maskBits=categoryObj.getFrogMask;
    
    // For Insects
    
     b2FixtureDef fixtureDef;
     fixtureDef.filter.categoryBits=categoryObj.getInsects;
     fixtureDef.filter.maskBits=categoryObj.getInsectsMask;
    
    
    // For Asteroid
    
     b2FixtureDef fixtureDef;
    
     fixtureDef.filter.categoryBits=categoryObj.getAsteroid;
     fixtureDef.filter.maskBits =categoryObj.getAsteroidMask;
    
    // For Weapons
    
     b2FixtureDef fixtureDef;
     fixtureDef.filter.categoryBits=categoryObj.getWeapon;
     fixtureDef.filter.maskBits=categoryObj.getWeaponsMask;
    

    In this way one can achieve effective collision by simply setting 1-16 category bit and mask bit for different object.

    00001 Default
    00002 1
    00004 2
    00008 3
    00010 4
    00020 5
    00040 6
    00080 7
    00100 8
    00200 9
    00400 10
    00800 11
    01000 12
    02000 13
    04000 14
    08000 15
    

    So be ready for happy coding,using this :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: