Hi, thanks for this link. I'd seen that in the testbed, but this example seems clearer. I'll try it for the sensor use after integrating ContactListener.
Also, I looked at the source kindly provided by the 'Luke' at Metal Fish Eggs, in the first thread link: http://blog.zincroe.com/2009/05/iphone-and-box2d/
..which gave me an understanding of the 'Actors' approach being used by others in thread http://www.cocos2d-iphone.org/forum/topic/2057#post-12939.
However Luke's example uses the regular UIKit framework and I'd like to use Cocos2D and OpenGLes.
So I started synthesizing the two approaches, integrating Luke's Actor.m and ContactListener.m(m) with pabloruiz55's/Rolando's notes, and updating to what I understand of the newest Box2d API in Cocos2d 0.8.02 .
ContactListener isn't throwing compile errors which is a success, but the simulator is freezing on first contact.
I think it's because I'm only half-way integrated right now.
Here are the changes I made so far, using the cocos2d-iphone-0.8.2-rc0 source. Next I'll continue integrating the physics setup and timing.
Caution, the GameLayer code is incomplete and shows only relevant excerpts from my work, built up from the Cocos Box2d template.
1. Actor.h and .m are the same as in Luke's example on blog.zincroe.com.
--------------------------------
2a. ContactListener.h
#include "Box2D.h"
class ContactListener : public b2ContactListener {
public:
void BeginContact(b2Contact *contact);
void EndContact(b2Contact *contact);
};
--------------------------------------
2b. ContactListener.mm
#import "ContactListener.h"
#import "Actor.h"
void ContactListener::BeginContact(b2Contact *contact) {
Actor *actor1 = (Actor *)contact->GetFixtureA()->GetBody()->GetUserData();
Actor *actor2 = (Actor *)contact->GetFixtureB()->GetBody()->GetUserData();
if(actor1 && actor2) {
[actor1 addContact:actor2];
[actor2 addContact:actor1];
}
}
void ContactListener::EndContact(b2Contact *contact) {
Actor *actor1 = (Actor *)contact->GetFixtureA()->GetBody()->GetUserData();
Actor *actor2 = (Actor *)contact->GetFixtureB()->GetBody()->GetUserData();
if(actor1 && actor2) {
[actor1 removeContact:actor2];
[actor2 removeContact:actor1];
}
}
----------------------
3a. GameLayer.h (derived from HelloWorld.h / Cocos2d 0.8.2 Box2D template and instead of Luke's MyGameViewController.h)
...
@class Actor;
@interface GameLayer : Layer {
NSMutableSet *actorSet;
b2ContactListener *contactListener;
b2World *world;
...
}
-(void)addActor:(Actor *)anActor;
-(void)removeActor:(Actor *)anActor;
-(void)removeAllActors;
...
// returns a Scene
+(id) scene;
@end
-----------------------
3b. GameLayer.mm
#import "GameLayer.h"
#import "Actor.h"
#import "ContactListener.h"
@interface GameLayer()
#pragma mark Actor Properties
@property (nonatomic, retain) NSMutableSet *actorSet;
#pragma mark Physics Properties
@property (nonatomic, readwrite) b2ContactListener *contactListener;
@property (nonatomic, readwrite) b2World *world;
@end
...
#pragma mark Actor Accessors
@synthesize actorSet;
#pragma mark Actor Methods
-(void)addActor:(Actor *)anActor {
if (anActor && ![[self actorSet] containsObject:anActor]) {
[anActor setGame:self];
[[self actorSet] addObject:anActor];
[anActor actorDidAppear];
}
}
-(void)removeActor:(Actor *)anActor {
if(anActor && [[self actorSet] containsObject:anActor]) {
[anActor retain];
[anActor actorWillDisappear];
[[self actorSet] removeObject:anActor];
[anActor setGame:nil];
[anActor release];
}
}
-(void)removeAllActors {
for(Actor *anActor in [NSSet setWithSet:[self actorSet]]) {
[self removeActor:anActor];
}
}
#pragma mark Physics Accessors
@synthesize contactListener;
@synthesize world;
-(id) init
{
if( (self=[super init])) {
mainLayer = self;
...
// set contactListener
contactListener = new ContactListener;
world->SetContactListener(contactListener);
...
}
return self;
}