I'm not quite understanding how to use the b2ContactListener. I'm basically trying to detect a contact and have the contacted object call a method, could be a sound, but in this case its just a log.
Box.mm
-(id) initWithWorld:(b2World*)pWorld
{
if((self = [super init]))
{
world = pWorld;
b2PolygonShape boxShape;
boxShape.SetAsBox(1.0f, 0.1f);
b2FixtureDef boxFixture;
boxFixture.shape = &boxShape;
boxFixture.density = 1.0f;
b2BodyDef boxBody;
boxBody.position.Set(150.0/PTM_RATIO, 250.0/PTM_RATIO);
box = world->CreateBody(&boxBody);
box->CreateFixture(&boxFixture);
}
return self;
}
-(void) saySomething
{
NSLog(@"Boing!");
}
I've been searching threads here and google for contact listener stuff, but I can't find something that I can wrap my head around. There are a couple of complex (to me) examples from badawe and others. And I just switched over from chipmunk (which I also had trouble doing the collision stuff with).
XrissKenn's blog post here is what I'm currently implementing: http://blog.xyris.ca/?p=40
It was very helpful. And the logging is occurring.
ContactListener.mm
void ContactListener::BeginContact(b2Contact* contact)
{
b2Fixture* fixtureA = contact->GetFixtureA();
b2Fixture* fixtureB = contact->GetFixtureB();
if (contact->IsSolid()) {
NSLog(@"Contact is solid");
}
}
void ContactListener::EndContact(b2Contact* contact)
{
NSLog(@"end contact");
}
Instead of having the log created in the if statement there, I'd like it to be fired from my contacted objects method (saySomething).
But I don't think I'm close to that yet, I've read about including the SetUserData, into my fixture, but I still don't know how I can call [box saySomething]; into the contact method.