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.
0×0001 Default
0×0002 1
0×0004 2
0×0008 3
0×0010 4
0×0020 5
0×0040 6
0×0080 7
0×0100 8
0×0200 9
0×0400 10
0×0800 11
0×1000 12
0×2000 13
0×4000 14
0×8000 15
So be ready for happy coding,using this :)
0 Comment(s)