Created
April 16, 2011 03:08
-
-
Save MLukman/922824 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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