尝试使用联系侦听器。监听“contact.End()”不工作。 (Box2d、iPhone)

发布于 2024-12-24 15:51:23 字数 2706 浏览 0 评论 0原文

MyContactListener 类: .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) {
}

现在的问题是:在我的更新方法中,我有这段代码来检查接触是否发生碰撞*结束*:

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

    }

但我收到错误。 (在代码中标记为注释)

要检查接触开始是否发生碰撞,我有以下有效代码:

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

    }

注意“for”语句,我将 .begin() 替换为 .end()。这就是区别。

但为什么不起作用呢?如果“接触结束”,我在检查夹具时是否做错了什么?

我想要这样做的原因是,如果固定装置接触到任何东西/什么也没有接触,我现在就想这样做。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦旅人picnic 2024-12-31 15:51:23
for(pos3 = contactListener->_contacts.end(); 
        pos3 != contactListener->_contacts.begin(); ++pos3)

我认为你应该将其替换为:

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

I think you should replace it with:

for(pos3 = contactListener->_contacts.begin(); 
        pos3 != contactListener->_contacts.end(); ++pos3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文