J'essaye de détruire un corps Box2D, mais chaque fois que j'essaye de le détruire, mon jeu plante, à cause d'une erreur de violation d'accès en lecture.Détruire un Box2D Body - lire l'erreur de violation d'accès
code pour supprimer le corps:
if (event.mouseButton.button == sf::Mouse::Left) {
if (((chunk.getChunkPosition().x + MAX_CHUNK_WIDTH * 16 > block.x * 16)
&& (block.x * 16 >= chunk.getChunkPosition().x))
&& ((chunk.getChunkPosition().y + MAX_CHUNK_HEIGHT * 16 > block.y * 16)
&& (block.y * 16 >= chunk.getChunkPosition().y)))
{
chunk.blockPhysic[block.x][block.y]->setCreatedState(false);
world.getBWorld()->DestroyBody(chunk.blockPhysic[block.x][block.y]->getBody());
}
}
Code pour créer le corps/bloc:
BlockPhysic::BlockPhysic(b2World* world, const sf::Vector2f& _position, const int& _textureKeyID, const sf::IntRect& _textRect, sf::Vector2f _size, bool _dynamicBody)
{
b2BodyDef bBodyDef;
dynamicBody = _dynamicBody;
bBodyDef.type = _dynamicBody == true ? b2_dynamicBody : b2_staticBody;
bBodyDef.position.Set(_position.x, _position.y);
rBody = world->CreateBody(&bBodyDef);
b2PolygonShape bShape;
bShape.SetAsBox(_size.x/2, _size.y/2);
b2FixtureDef bFixtureDef;
bFixtureDef.shape = &bShape;
bFixtureDef.density = 0.0f;
bFixtureDef.friction = 0.0f;
rFixture = rBody->CreateFixture(&bFixtureDef);
sf::Texture *texture = &Global::txtManager.textures.at(_textureKeyID);
rSprite.setTexture(*texture);
rSprite.setOrigin(_size.x/2, _size.y/2);
rSprite.setTextureRect(sf::IntRect(_textRect.left, _textRect.top, _textRect.width, _textRect.height));
rSprite.setPosition(_position.x, _position.y);
created = true;
}