在没有连续碰撞的情况下检测两个box2d物体的初始碰撞

Ale*_*lex 5 physics box2d cocos2d-iphone

我有一些简单的box2d机构设置与一个联系人监听器,如下所示:

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


b2Body *A =  contact->GetFixtureA()->GetBody();
b2Body *B =  contact->GetFixtureA()->GetBody();

NSLog(@"Collision detected!");
PLAYSOUND(COLLISION);

}

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

}
Run Code Online (Sandbox Code Playgroud)

当两具尸体发生碰撞时,我需要发出声音.然而,该实现检测到连续碰撞,因此当身体接触时播放声音.我对box2d和C++的了解已经非常有限,有没有一种简单的方法来检测新的碰撞而不会检测到连续的碰撞?

小智 -1

首先设置一个这样的计时器..

   [self schedule:@selector(check collision:)];
Run Code Online (Sandbox Code Playgroud)

在这个方法中

 - (void)tick:(ccTime) dt
   {

       for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) 
          { 
             //--------------My contact Listener Start-------------------------


                std::vector<MyContact>::iterator pos;

                for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos) 
                  {
                      MyContact contact = *pos;

          // Here get your sprite and make their fixture and check...

                      if ((contact.fixtureA == tempballFixture && contact.fixtureB == _mainballFixture) ||
                          (contact.fixtureA == _mainballFixture && contact.fixtureB == tempballFixture))
                       {
                          if(mainDelegate.music_playing == TRUE)
                          {
                            [[SimpleAudioEngine sharedEngine] playEffect:@"Rock impact.mp3"]; 
                          }
                           //-------------collision count for update score value--------
                  }
          }
Run Code Online (Sandbox Code Playgroud)