#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) {}
当两具尸体发生碰撞时,我需要发出声音.然而,该实现检测到连续碰撞,因此当身体接触时播放声音.我对Box2d和C的了解已经非常有限,有没有一种简单的方法来检测新的碰撞而不会检测到连续的碰撞?
解决方法 你有正确的基本想法,但它需要一些改进.在您的BeginContact(…)调用中,您有:
PLAYSOUND(ColliSION);
你应该做的不是在这里播放声音,而是将一些其他系统排入队列以播放这一特定对的声音.将您的body的userdata标记设置为指向该类的指针(或一些其他ID以跟踪实体).像这样:
class EntityContactListener : public ContactListener{private: GameWorld* _gameWorld; EntityContactListener() {} typedef struct { Entity* entA; Entity* entB; } CONTACT_PAIR_T; vector<CONTACT_PAIR_T> _contactPairs;public: virtual ~EntityContactListener() {} EntityContactListener(GameWorld* gameWorld) : _gameWorld(gameWorld) { _contactPairs.reserve(128); } voID NotifyCollisions() { Message* msg; MessageManager& mm = GameManager::Instance().GetMessageMgr(); for(uint32 IDx = 0; IDx < _contactPairs.size(); IDx++) { Entity* entA = _contactPairs[IDx].entA; Entity* entB = _contactPairs[IDx].entB; //DeBUGLogCPP("Contact Notification %s<->%s",entA->ToString().c_@R_@R_301_6941@_5742@,entB->ToString().c_@R_@R_301_6941@_5742@); msg = mm.CreateMessage(); msg->Init(entA->GetID(),entB->GetID(),Message::MESSAGE_ColliSION); mm.EnqueueMessge(msg,0); msg = mm.CreateMessage(); msg->Init(entB->GetID(),entA->GetID(),0); } _contactPairs.clear(); } voID PreSolve(b2Contact* contact,const b2Manifold* oldManifold) { } // BEWARE: You may get multiple calls for the same event. voID BeginContact(b2Contact* contact) { Entity* entA = (Entity*)contact->GetFixtureA()->Getbody()->GetUserData(); Entity* entB = (Entity*)contact->GetFixtureB()->Getbody()->GetUserData(); //DeBUGLogCPP("Begin Contact %s->%s",entB->ToString().c_@R_@R_301_6941@_5742@); if(entA->GetGroupID() == entB->GetGroupID()) { // Can't collIDe if they are in the same group. return; } assert(entA != NulL); assert(entB != NulL); for(uint32 IDx = 0; IDx < _contactPairs.size(); IDx++) { if(_contactPairs[IDx].entA == entA && _contactPairs[IDx].entB == entB) return; // Not sure if this is needed... if(_contactPairs[IDx].entA == entB && _contactPairs[IDx].entA == entB) return; } CONTACT_PAIR_T pair; pair.entA = entA; pair.entB = entB; _contactPairs.push_back(pair); } // BEWARE: You may get multiple calls for the same event. voID EndContact(b2Contact* contact) { /* Entity* entA = (Entity*)contact->GetFixtureA()->Getbody()->GetUserData(); Entity* entB = (Entity*)contact->GetFixtureB()->Getbody()->GetUserData(); DeBUGLogCPP("End Contact %s->%s",entB->ToString().c_@R_@R_301_6941@_5742@); */ }};
最后一部分是即使发生碰撞也不会在短时间内再次播放声音.您可以通过创建秒表或从实体更新周期的固定时间倒计时来完成此 *** 作.
这个有帮助吗?
总结以上是内存溢出为你收集整理的cocos2d-iphone – 检测两个box2d体的初始碰撞,没有连续碰撞全部内容,希望文章能够帮你解决cocos2d-iphone – 检测两个box2d体的初始碰撞,没有连续碰撞所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)