Counting number of fixtures or b2bodies in a particular rectangular area in cocos2dx | Techbirds

In order to make the b2body and attach it to the world,fixtures are added to the b2body(either one fixture or more are attached to a single b2body).For counting the no. of fixtures in a particular defined area,we can use QueryAABB.

in .h file,write;

int count=0; //call back function counting num of fixtures bool ReportFixture(b2Fixture* fixture) { foundBodies.push_back( fixture->GetBody() ); return true;//keep going to find all fixtures in the query area }

int count=0;

//call back function counting num of fixtures

bool ReportFixture(b2Fixture* fixture)

{

foundBodies.push_back( fixture->GetBody() );

return true;//keep going to find all fixtures in the query area

}

in .cpp file,write the following code in the function where to count fixtures:

HelloWorld queryCallback; b2AABB aabb; aabb.lowerBound = b2Vec2(0,0);//define the lower boundary point of the area in which the fixtures to be count aabb.upperBound = b2Vec2(10,10);//upper boundary point of the rectangular area world->QueryAABB( &queryCallback, aabb );//calling the QueryAABB function which internally call the reportfixture call back function defined in the .h file for (int i = 0; i < queryCallback.foundBodies.size(); i++) { count++; } CCLOG(“count=%d”,count);

HelloWorld queryCallback;

b2AABB aabb;

aabb.lowerBound = b2Vec2(0,0);//define the lower boundary point of the area in which the fixtures to be count

aabb.upperBound =  b2Vec2(10,10);//upper boundary point of the rectangular area

world->QueryAABB( &queryCallback, aabb );//calling the QueryAABB function which internally call the reportfixture call back function defined in the .h file

for (int i = 0; i < queryCallback.foundBodies.size(); i++)

{

count++;

}

CCLOG(“count=%d”,count);

This count will return the number of fixtures in the rectangular area with for coordinates as following:
b2Vec2(0,0),b2Vec2(10,0),b2Vec2(10,10),b2Vec2(0,10)

400 total views, 2 views today

Share this Onfacebook-3839608twitter-2896570linkedin-5585609google-5340113