c++vectorbox2dsfmlrigid-bodies

Box2D weird behavior with std::vector


lately I started playing with box2d and tried abstracting it to class

RigidBody::RigidBody() {

}

RigidBody::RigidBody(const RigidBody& other) {
    m_fixture = other.m_fixture;

    b2BodyDef bodyDef;
    bodyDef.position = other.m_body->GetPosition();
    bodyDef.type = other.m_body->GetType();

    m_body = Runtime::PhysicsWorld.CreateBody(&bodyDef);

    b2FixtureDef fixtureDef;
    fixtureDef.shape                = m_fixture->GetShape();
    fixtureDef.density              = m_fixture->GetDensity();
    fixtureDef.friction             = m_fixture->GetFriction();
    fixtureDef.restitution          = m_fixture->GetRestitution();
    fixtureDef.restitutionThreshold = m_fixture->GetRestitutionThreshold();

    m_fixture = m_body->CreateFixture(&fixtureDef);
}

RigidBody::RigidBody(sf::Vector2f pos, BodyType type) {
    pos /= Constants::PPM;

    b2BodyDef bodyDef;
    bodyDef.position = pos;
    bodyDef.type = (b2BodyType)type;

    m_body = Runtime::PhysicsWorld.CreateBody(&bodyDef);

    sf::Vector2f size(50.0f, 50.0f);

    size /= 2.0f;
    size /= Constants::PPM;

    b2PolygonShape shape;
    shape.SetAsBox(size.x, size.y);

    b2FixtureDef fixtureDef;

    fixtureDef.shape                = &shape;
    fixtureDef.density              = 1.0f;
    fixtureDef.friction             = 0.5f;
    fixtureDef.restitution          = 0.0f;
    fixtureDef.restitutionThreshold = 0.5f;

    m_body->CreateFixture(&fixtureDef);
}

RigidBody::~RigidBody() {
    Runtime::PhysicsWorld.DestroyBody(m_body);
}

but vector is behaving really weird with it i know it's probably becasue of copy constructor or destructor but I can't figure this out

std::vector<hv::RigidBody> m_Bodies;

the problem is with vector erase when I call m_Bodies.erase(m_Bodies.begin()) for some reason it deletes the last objects

m_Bodies.erase(m_Bodies.begin());

And after I call m_Bodies.erase(m_Bodies.begin()) second time I get this

god help

Also it doesn't matter how many objects is in vector if I call m_Bodies.erase(m_Bodies.begin() + 3) it will always delete the last one

*edit corrected question


Solution

  • The problem was I didn't define operator = resolved it by

    RigidBody* RigidBody::operator=(const RigidBody& other) {
        if(m_body)
            Runtime::PhysicsWorld.DestroyBody(m_body);
    
        m_fixture = other.m_fixture;
    
        b2BodyDef bodyDef;
        bodyDef.position = other.m_body->GetPosition();
        bodyDef.type = other.m_body->GetType();
    
        m_body = Runtime::PhysicsWorld.CreateBody(&bodyDef);
    
        b2FixtureDef fixtureDef;
        fixtureDef.shape = m_fixture->GetShape();
        fixtureDef.density = m_fixture->GetDensity();
        fixtureDef.friction = m_fixture->GetFriction();
        fixtureDef.restitution = m_fixture->GetRestitution();
        fixtureDef.restitutionThreshold = m_fixture->GetRestitutionThreshold();
    
        m_fixture = m_body->CreateFixture(&fixtureDef);
    
        return this;
    }