0

MyContactListener class: .h

#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);

};

.mm

#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) {
}

Now the problem: In my update method I have this code to check the collision if the contact *ends*:

std::vector<MyContact>::iterator pos3;
    for(pos3 = contactListener->_contacts.end(); 
        pos3 != contactListener->_contacts.begin(); ++pos3) {
        MyContact contact = *pos3; // here I get "EXE_BAD_ACCESS"

        if (contact.fixtureA == detectorFixture || contact.fixtureB == detectorFixture) {
        }

    }

But I get an error. (marked in the code as comment)

To check the collision if the contact begins I have this code which works:

std::vector<MyContact>::iterator pos;
    for(pos = contactListener->_contacts.begin(); 
        pos != contactListener->_contacts.end(); ++pos) {
        MyContact contact = *pos;

        if (contact.fixtureA == detectorFixture || contact.fixtureB == detectorFixture) {
        }

    }

Notice the "for" statement, I swapped the .begin() with .end(). That's the difference.

But why doesn't it work? Did I do something wrong for checking the fixture if the "contact ends".

The reason why I want to do this is that I want to now if a fixture is touching anything/nothing.

4

1 に答える 1

1
for(pos3 = contactListener->_contacts.end(); 
        pos3 != contactListener->_contacts.begin(); ++pos3)

次のものに置き換える必要があると思います。

for(pos3 = contactListener->_contacts.begin(); 
        pos3 != contactListener->_contacts.end(); ++pos3)
于 2012-01-03T08:31:42.857 に答える