std::vector 的 Box2D 怪异行为
Box2D weird behavior with std::vector
最近我开始玩 box2d 并尝试将其抽象为 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);
}
但是 vector 的行为真的很奇怪,我知道这可能是因为复制构造函数或析构函数,但我想不通
std::vector<hv::RigidBody> m_Bodies;
当我调用 m_Bodies.erase(m_Bodies.begin()) 时,问题出在矢量擦除上,由于某种原因它删除了最后一个对象
m_Bodies.erase(m_Bodies.begin());
在我第二次调用 m_Bodies.erase(m_Bodies.begin()) 之后,我得到了这个
此外,如果我调用 m_Bodies.erase(m_Bodies.begin() + 3),向量中有多少对象并不重要,它总是会删除最后一个对象
*编辑更正的问题
问题是我没有定义运算符=
解决了
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;
}
最近我开始玩 box2d 并尝试将其抽象为 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);
}
但是 vector 的行为真的很奇怪,我知道这可能是因为复制构造函数或析构函数,但我想不通
std::vector<hv::RigidBody> m_Bodies;
当我调用 m_Bodies.erase(m_Bodies.begin()) 时,问题出在矢量擦除上,由于某种原因它删除了最后一个对象
m_Bodies.erase(m_Bodies.begin());
在我第二次调用 m_Bodies.erase(m_Bodies.begin()) 之后,我得到了这个
此外,如果我调用 m_Bodies.erase(m_Bodies.begin() + 3),向量中有多少对象并不重要,它总是会删除最后一个对象
*编辑更正的问题
问题是我没有定义运算符=
解决了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;
}