I know, I know, the change from world->Query to world->QueryAABB in Box2D has been discussed here before, but no one seems to have an example of how to handle it. Here's what I've put together from the Box2DTestBed example. Is it something simple that I'm missing?
MyGameLayer has this:
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->IsStatic() == false)
{
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;
};
-(void)MouseDownAtPos:(b2Vec2)pos {
NSLog(@"Detected mouse down event");
if (self.m_mouseJoint)
{
NSLog(@"m_mousejoint already set");
return;
}
// Make a small box.
b2AABB aabb;
b2Vec2 d;
d.Set(0.001f, 0.001f);
aabb.lowerBound = pos - d;
aabb.upperBound = pos + d;
// Query the world for overlapping shapes.
QueryCallback callback(pos);
world->QueryAABB(&callback, aabb);
if (callback.m_fixture)
{
NSLog(@"m_mousejoint was found");
b2Body* body = callback.m_fixture->GetBody();
b2MouseJointDef md;
md.body1 = groundBody;
md.body2 = body;
md.target = pos;
#ifdef TARGET_FLOAT32_IS_FIXED
md.maxForce = (body->GetMass() < 16.0)?
(1000.0f * body->GetMass()) : float32(16000.0);
#else
md.maxForce = 1000.0f * body->GetMass();
#endif
m_mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
body->WakeUp();
return;
}
}
-(void)MouseMoveAtPos:(b2Vec2)pos {
if (self.m_mouseJoint)
{
m_mouseJoint->SetTarget(pos);
}
}
- (void) MouseUp {
if (m_mouseJoint)
{
world->DestroyJoint(m_mouseJoint);
m_mouseJoint = NULL;
}
}
and in my ccTouchesBegan, I call:
// stuff to get a converted CGPoint
[self MouseDownAtPos:(b2Vec2(convertedTouch.x, convertedTouch.y))];
The debugger shows that the the ccTouch is actually initiated (I get the NSLog message), but I never get either of the NSLog messages associated with the m_mousejoint. Obviously the callback isn't finding a fixture and returning it I'm struggling a bit with the QueryCallback and since it's not documented anywhere on the Box2D site, I was hoping some very kind person on this forum could take a look and see what I'm doing wrong.
Thanks in advance!