I don't know much C++ and I am trying to implement a solution I found - (Getting the world's contactListener in Box2D) using Cocos2d-box2d. Here is the Contact Listener.
SubcContactListener.h:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import <vector>
typedef std::pair<b2Fixture*, b2Fixture*> fixturePair;
typedef std::vector<fixturePair> thingsThatTouched;
extern thingsThatTouched g_touchingFixtures;
class SubcContactListener : public b2ContactListener {
public:
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
};
SubcContactListener.mm:
#import "SubcContactListener.h"
void SubcContactListener:: BeginContact(b2Contact *contact) {
thingsThatTouched->push_back( make_pair(contact->GetFixtureA(), contact->GetFixtureB()) );
}
void SubcContactListener:: EndContact(b2Contact *contact) {
}
I get an
Expected unqualified-id
error at the line
thingsThatTouched.push_back( make_pair(contact->GetFixtureA(), contact->GetFixtureB()) );
in the BeginContact method of SubcContactListener.mm. I also get an
Unexpected type name 'thingsThatTouched': expected expression
error at the lines
b2Fixture* fixtureA = thingsThatTouched[i].first;
b2Fixture* fixtureB = thingsThatTouched[i].second;
in the tick method of the HelloWorldLayer class.
UPDATED:
I am trying to create a weldjoint when two sprites (which have their own classes) collide. Here is what I did in the tick method of the HelloWorld.mm:
b2WeldJoint *weldJoint;
b2WeldJointDef weldJointDef;
for (int i = 0; i < touchingBodies.size(); i++) {
b2Body* bodyA = touchingBodies[i].first;
b2Body* bodyB = touchingBodies[i].second;
weldJointDef.Initialize(bodyA, bodyB, bodyA->GetWorldCenter());
weldJointDef.collideConnected = false;
weldJoint = (b2WeldJoint*)world->CreateJoint(&weldJointDef);
}
touchingBodies.clear();
But I get the following error:
Apple Mach-O Linker (Id) Error
"_touchingBodies", referenced from:
SubcContactListener::BeginContact(b2Contact*) in SubcContactListener.o