J'ai un problème avec b2contactListener.La collision Cocos2d fonctionne mais ne peut pas être détectée par b2contactListener
i ont ces deux classes:
class Terrain
class Hero
Chaque classe a une méthode appelée: getHeroBody
-> renvoie le b2body
du héros. getTerrainBody
-> renvoie le b2body
du Terrain.
Chaque classe a une méthode appelée (id)initWithWorld:(b2World *)world;
qui prend un b2World
Lorsque je crée 2 objets sur ces classes i utiliser le même b2World
.
Maintenant le problème est: Les collisions fonctionnent mais toute la méthode pour détecter ces collisions retourne toujours un tableau nul, 0, autrement dit aucune collision.
ici est la déclaration
MyContactListener *_contactListener;
_contactListener = new MyContactListener();
_world->SetContactListener(_contactListener);
ici est de 2 cycles le jeu entre jamais:
// PREMIER MOYEN NE FONCTIONNE PAS
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();pos != _contactListener->_contacts.end(); ++pos) { //never enters
MyContact contact = *pos;
for(b2Fixture *terrain_fixture = [_terrain getTerrainBody]->GetFixtureList(); terrain_fixture; terrain_fixture = terrain_fixture->GetNext()){
for (b2Fixture *char_fixture = [_hero getHeroBody]->GetFixtureList(); char_fixture; char_fixture = char_fixture->GetNext()){
if ((contact.fixtureA == terrain_fixture && contact.fixtureB == char_fixture) ||
(contact.fixtureA == char_fixture && contact.fixtureB == terrain_fixture)) {
isontheground = YES;
}
}
}
}
// CHEMIN SECOND NE FONCTIONNE PAS
for (b2Contact* contact = _world->GetContactList(); contact; contact = contact->GetNext()){ //never enters
isontheground = YES;
}
PREMIÈRE UTILISATION MANIÈRE CE CLASSE
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) {
}
Que voulez-vous dire par collision? Pouvez-vous publier le code de collision et d'initialisation pertinent? – giorashc
en quelques mots je n'entrerai jamais dans ce std :: vector :: itérateur pos; pour (pos = _contactListener -> _ contacts.begin(); pos! = _contactListener -> _ contacts.end(); ++ pos) –
Qu'est-ce que _contacts dans votre écouteur de contacts et comment le remplissez-vous? – giorashc