0

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
Community
  • 1
  • 1
dropcode
  • 15
  • 4

1 Answers1

0

thingsThatTouched is a typedef. It is just synonymous to a type but not an identifier/variable. In other words its not an object by itself. I think you are supposed to use g_touchingFixtures where you are using thingsThatTouched.

When you say,

typedef int myOwnInt;

Now you can't use,

myOwnInt = 10;   // Wrong. myOwnInt is just a type like how int is.

myOwnInt i = 10; 

Hope it helps !

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • Ok. What is `_touchingBodies` which linker is complaining about ? If it is has `extern` linkage, are you compiling that file too along with the current source file ? – Mahesh Jul 13 '12 at 19:42
  • `extern thingsThatTouched touchingBodies; `Yes I am compiling both together. – dropcode Jul 13 '12 at 19:49
  • `touchingBodies` is in the `contactListerner.h` file. – dropcode Jul 13 '12 at 19:53
  • Where is `g_touchingFixtures` defined ? – Mahesh Jul 13 '12 at 19:57
  • sorry for not mentioning. I replaced it with `touchingBodies` since I am want to create a `weldJoint`. I also changed the `typedef's `to take `b2Body`instead of `b2Fixture` – dropcode Jul 13 '12 at 20:05