【发布时间】:2013-09-22 01:39:25
【问题描述】:
我一直在使用 sfml 2.0 库开发 oop 版本的蛇,并且我有一个单独的类来处理碰撞。代码如下:
包括“collision.hpp”
bool sfc::Sprite::collision(sfc::Sprite sprite2) {
this->setBounds();
sprite2.setBounds();
if (top > sprite2.bottom || bottom < sprite2.top || left > sprite2.right || right < sprite2.left) {
return false;
}
return true;
}
void sfc::Sprite::colMove(sf::Vector2f &movement, sfc::Sprite sprite2) {
if (!this->collision(sprite2)) {
this->move(movement);
}
}
void sfc::Sprite::colMove(float x, float y, sfc::Sprite sprite2) {
if (!this->collision(sprite2)) {
this->move(x, y);
}
}
void sfc::Sprite::setBounds() {
top = this->getPosition().y;
bottom = this->getPosition().y + this->getTexture()->getSize().y;
left = this->getPosition().x;
right = this->getPosition().x + this->getTexture()->getSize().y;
}
唯一的问题是一旦发生碰撞事件,精灵就会在窗口的剩余生命周期中被卡住。我怎样才能得到它,以便在碰撞时它不会粘在那里。谢谢! 〜迈克尔
编辑:我知道在碰撞发生后不允许精灵移动,但我不知道在碰撞后阻止精灵移动的任何其他方法。
【问题讨论】:
标签: c++ collision-detection sfml