Hey guys!
I'm new for box2d! In box2d docs no description for queryAABB.. I've read a thread about mouse grab on this forum, but I need help with smth else)
How to grab all bodies in AABB area, for example to apply impulse to all these bodies?
I've tried smth like these, but it doesn't work( Please help me!)
b2AABB aabb;
aabb.lowerBound.Set(-1.0f+location.x, -1.0f+location.y);
aabb.upperBound.Set(1.0f+location.x, 1.0f+location.y);
b2Vec2 callPoint;
callPoint.Set (location.x,location.y);
QueryCallback callback(callPoint);
world->QueryAABB(&callback, aabb);
if (callback.m_fixture)
{
for(b2Fixture *fixture = callback.m_fixture; fixture; fixture = callback.m_fixture -> GetNext() ) {
b2Body *body = fixture -> GetBody();
b2Vec2 impulseV;
impulseV.Set(0.0,20.0);
body -> ApplyImpulse(impulseV, callPoint);
}
}
//and class
class QueryCallback : public b2QueryCallback
{
public:
QueryCallback(const b2Vec2& point)
{
m_point = point;
m_fixture = NULL;
}
bool ReportFixture(b2Fixture* fixture)
{
b2Body* body = fixture->GetBody();
if (body->GetType() == b2_dynamicBody)
{
bool inside = fixture->TestPoint(m_point);
if (inside)
{
m_fixture = fixture;
// We are done, terminate the query.
return false;
}
}
// Continue the query.
return true;
}
b2Vec2 m_point;
b2Fixture* m_fixture;
};