0

I have created a game which uses a color coded image to create different bodies/fixtures. So for example if the pixel is red it will get stored into an array as 7 and then the program will create a body called jewel. If there are 10 red pixels, 10 jewels will be created:

                    else if (array[w][h]==7)
            {
                b2BodyDef Jewel_BodyDef;
                Jewel_BodyDef.position.Set(x, y);
                m_collectableJewel = m_world->CreateBody(&Jewel_BodyDef);
                b2PolygonShape box;
                box.SetAsBox(0.5f, 0.5f);
                m_collectableJewelFixture=m_collectableJewel->CreateFixture(&box, 0.0f);
                collide.m_jewelFix.push_back(m_collectableJewelFixture);
                jewels a;
                a.jewel_dim.set(1.0f,1.0f);
                a.jewel_pos.set(x,y);
                m_jewels.push_back(a);
                x+=5.0f;
            }

My problem is when calculating the collision between the player and the jewels. The program knows when the player has collided with a jewel. However, I can't get it to delete the fixture. Or rather, it will only delete the last fixture placed, i.e. the last one to be created in the vector. Is there a way to name the fixtures individually? So that then the program can delete the one it has actually collided with, rather than the last one?

edit:

int Collision_with_Player::PickJewel(b2Fixture *player, b2Fixture *foot)
{
    int CollisionJewel=0;
    std::vector<MyContact>::iterator posi;
    for(posi = m_contactListener->m_contacts.begin(); posi != m_contactListener->m_contacts.end(); ++posi)
    {
        MyContact contact = *posi;
        for(std::vector<b2Body*>::iterator iterb = m_jewelBodyVec.begin(); iterb != m_jewelBodyVec.end(); ++iterb)
        {
             for(std::vector<b2Fixture*>::iterator iter = m_jewelFix.begin(); iter != m_jewelFix.end(); ++iter)
            {
                if ((contact.fixtureA == player && contact.fixtureB == *iter) ||
                   (contact.fixtureA ==*iter && contact.fixtureB == player))
                {
                    m_deleteJewels.clear();
                    std::cout<<"size"<<m_deleteJewels.size()<<std::endl;
                    CollisionJewel=1;
                    std::cout<<"Jewel"<<*iter<<std::endl;
                    deletejewel = *iter;
                    deletejewelbody= *iterb;

                }
                else
                {
                    CollisionJewel=0;
                }
            }
        }
    }
    return CollisionJewel;
}

and in another file

    if (collide.PickJewel(m_playerFixture, m_footSensorFixture)==1)
    {
        collide.deletejewelbody->DestroyFixture(collide.deletejewel);

    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MichaelN
  • 1
  • 1
  • Which container belongs to m_deleteJewels? I think you are deleting the object by calling `clear()` function – Steephen Apr 27 '15 at 18:11
  • You seems to be invalidating the iterator by deleting the objects from container. And solution is you have to implement erase_remove idiom http://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom – Steephen Apr 27 '15 at 18:15
  • 1
    @MichaelN `m_deleteJewels.clear();` You are clearing the entire container. I don't see where you're removing individual items from a container. – PaulMcKenzie Apr 27 '15 at 20:05

0 Answers0