Box2D Forums

It is currently Thu May 23, 2013 5:19 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Jun 20, 2012 3:53 pm 
Offline

Joined: Wed May 30, 2012 11:05 am
Posts: 16
I have created two classes for a rope (made of revolute joints which returns a PhysicsSprite) and a sprite. I am a bit new to contact listeners (not box2d). I want to create a weldjoint on contact of the sprites. I got the contact listener code from Ray Wenderlich's code and modified it a bit. Here's the code below:

MyContactListener.h

Code:
 #import "Box2D.h"
#import <vector>
#import <algorithm>

struct MyContact {
b2Fixture *fixtureA;
b2Fixture *fixtureB;
bool operator==(const MyContact& other) const
{
    return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
}
};

class MyContactListener : public b2ContactListener {

public:
  std::vector<MyContact>_contacts;

MyContactListener();
~MyContactListener();

virtual void BeginContact(b2Contact* contact);
virtual void EndContact(b2Contact* contact);
virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);

 };


MyContactListener.mm

Code:
#import "MyContactListener.h"

MyContactListener::MyContactListener() : _contacts() {
}

MyContactListener::~MyContactListener() {
}

void MyContactListener::BeginContact(b2Contact* contact) {
// We need to copy out the data because the b2Contact passed in
// is reused.
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
}

void MyContactListener::EndContact(b2Contact* contact) {
    MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
    std::vector<MyContact>::iterator pos;
    pos = std::find(_contacts.begin(), _contacts.end(), myContact);
    if (pos != _contacts.end()) {
        _contacts.erase(pos);
    }
}

void MyContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
}

void MyContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
}
 


my contact listener in the init method of HelloworldLayer.mm:

Code:
 // Loop through all of the box2d bodies that are currently colliding, that we have
// gathered with our custom contact listener...
std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos) {
    MyContact contact = *pos;

    // Get the box2d bodies for each object
    b2Body *bodyA = contact.fixtureA->GetBody();
    b2Body *bodyB = contact.fixtureB->GetBody();
    if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
        PhysicsSprite *spriteA = (PhysicsSprite *) bodyA->GetUserData();
        PhysicsSprite *spriteB = (PhysicsSprite *) bodyB->GetUserData();

        if (spriteA == sprite  && spriteB == [rope objectAtIndex:0]) {
            b2WeldJointDef weldJointDef;
            weldJointDef.Initialize(bodyA, bodyB, bodyA->GetPosition());
            weldJointDef.collideConnected = false;

            weldJoint = (b2WeldJoint*)world->CreateJoint(&weldJointDef);               

        }
   }
}


I always get the error:

Code:
Thread 1:EXC_BAD_ACCESS(code=2, address=0x4)


pointing to the line:

Code:
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos)


Please help me out, I've been at it all day.


Top
 Profile  
 
PostPosted: Wed Jun 20, 2012 5:14 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Have you initialized the '_contactListener' variable somewhere? eg.:

_contactListener = new MyContactListener();


Top
 Profile  
 
PostPosted: Thu Jun 21, 2012 1:42 am 
Offline

Joined: Wed May 30, 2012 11:05 am
Posts: 16
I forgot that, but when I added it I got another error:

Code:
Apple Mach-O Linker (Id) Error
"MyContactListener::MyContactListener", referenced from:
-[HelloWorldLayer initPhysics] in HelloWorldLayer.o


Top
 Profile  
 
PostPosted: Thu Jun 21, 2012 2:49 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Seems like the MyContactListener class is not included in your project... these are not really Box2D questions dude!


Top
 Profile  
 
PostPosted: Fri Jun 22, 2012 2:44 am 
Offline

Joined: Wed May 30, 2012 11:05 am
Posts: 16
it's included.


Top
 Profile  
 
PostPosted: Fri Jun 22, 2012 11:44 am 
Offline

Joined: Wed Jan 11, 2012 9:16 am
Posts: 27
Location: Canada
I just went through Ray's tutorial for myself (second time) and had no problem getting the Contact Listener to work and (in my case) delete Box2d bodies. I recommend you do a separate project that follows his tutorial exactly so that you eliminate possible issues with your current project. For example, it looks like you're putting the contact listener code in the init section of your HelloWorld instead of the tick/update section...

Here's the tutorial: http://www.raywenderlich.com/505/how-to-create-a-simple-breakout-game-with-box2d-and-cocos2d-tutorial-part-22

Please download my iPhone app (uses Box2d and Cocos2d)! You can view it here:
http://itunes.com/apps/jrchemistryset
http://www.youtube.com/watch?v=7TtpjZhxxfs


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group