Skip to content

Instantly share code, notes, and snippets.

@MLukman
Created April 16, 2011 03:08
Show Gist options
  • Save MLukman/922824 to your computer and use it in GitHub Desktop.
Save MLukman/922824 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import "Box2D.h"
class GamePhysicsContactListener : public b2ContactListener
{
protected:
NSInvocation *invocation;
public:
GamePhysicsContactListener(id target, SEL action);
~GamePhysicsContactListener();
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
};
#import "GamePhysicsContactListener.h"
GamePhysicsContactListener::GamePhysicsContactListener(id target, SEL action): b2ContactListener() {
NSMethodSignature *aMethodSignature = [[target class] instanceMethodSignatureForSelector:action];
invocation = [[NSInvocation invocationWithMethodSignature:aMethodSignature] retain];
[invocation setTarget:target];
[invocation setSelector:action];
}
GamePhysicsContactListener::~GamePhysicsContactListener() {
[invocation release];
}
void GamePhysicsContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
[invocation setArgument:&contact atIndex:2];
[invocation setArgument:&impulse atIndex:3];
[invocation invoke];
}
- (id)init {
self = [super init];
if (self) {
world = new b2World(gravity, true);
world->SetContactListener(new GamePhysicsContactListener(self, @selector(checkCollisionContact:withImpulse:)));
}
return self;
}
- (void)checkCollisionContact:(b2Contact *)contact withImpulse:(b2ContactImpulse *)impulse {
b2Body *bodyA = contact->GetFixtureA()->GetBody();
b2Body *bodyB = contact->GetFixtureB()->GetBody();
if (bodyA == ballBody && bodyB == targetBody) {
// show the score here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment