为什么我的碰撞系统不能正常工作?
Why my collision system isn't working properly?
我正在使用 SFML 和 Tile System 制作游戏。
我正在使用这个库来解析我的地图并处理碰撞:
https://github.com/fallahn/sfml-tmxloader
基本检测有效,但我的对象位置和我的精灵碰撞之间发生了变化。
当我向下和向右方向碰撞时,碰撞工作正常,但是当我向上和向左移动时,有 ~2 个方块的偏移。
所以我猜我做错了什么..
当我前进时,我会这样做:
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
movement.y -= demonist->getSpeed();
demonist->move(ACharacter::UP, movement, frameTime, map);
noKeyWasPressed = false;
}
然后在我的移动方法中,我会在执行任何操作之前检查我要去的点是否有效。
所以我输入我的碰撞方法
bool ACharacter::collision(const sf::Vector2f& coord, const tmx::MapLoader& map) const
{
const std::vector<tmx::MapLayer>& layers = map.GetLayers();
sf::Vector2f tmp;
std::cout << g_time.asSeconds() << std::endl;
tmp.x = this->_x + (coord.x * g_time.asSeconds());
tmp.y = this->_y + (coord.y * g_time.asSeconds());
std::vector<sf::Vector2f> player;
player.push_back(sf::Vector2f(0, 0));
player.push_back(sf::Vector2f(0 , 82));
player.push_back(sf::Vector2f(82, 0));
player.push_back(sf::Vector2f(82, 82));
std::cout << this->_x << " " << this->_y << std::endl;
for(auto& layer : layers)
if(layer.type == tmx::ObjectGroup)
for(auto& obj : layer.objects)
if(layer.name == "Objects")
for(auto& point : player)
if(obj.Contains(point + tmp))
return (true);
return (false);
}
this-_x
和 this->_y
是我的精灵左上坐标的位置。
frametime 是自我的主循环以来经过的秒数。
我的精灵大小是 82*82
我通过将精灵的原点设置为图像的中心解决了我的问题。于是我的盒子坐标就变成了
player.push_back(sf::Vector2f(-42, -42));
player.push_back(sf::Vector2f(42 , -42));
player.push_back(sf::Vector2f(42, 42));
player.push_back(sf::Vector2f(-42, 42));
我正在使用 SFML 和 Tile System 制作游戏。 我正在使用这个库来解析我的地图并处理碰撞: https://github.com/fallahn/sfml-tmxloader
基本检测有效,但我的对象位置和我的精灵碰撞之间发生了变化。
当我向下和向右方向碰撞时,碰撞工作正常,但是当我向上和向左移动时,有 ~2 个方块的偏移。
所以我猜我做错了什么.. 当我前进时,我会这样做:
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
movement.y -= demonist->getSpeed();
demonist->move(ACharacter::UP, movement, frameTime, map);
noKeyWasPressed = false;
}
然后在我的移动方法中,我会在执行任何操作之前检查我要去的点是否有效。 所以我输入我的碰撞方法
bool ACharacter::collision(const sf::Vector2f& coord, const tmx::MapLoader& map) const
{
const std::vector<tmx::MapLayer>& layers = map.GetLayers();
sf::Vector2f tmp;
std::cout << g_time.asSeconds() << std::endl;
tmp.x = this->_x + (coord.x * g_time.asSeconds());
tmp.y = this->_y + (coord.y * g_time.asSeconds());
std::vector<sf::Vector2f> player;
player.push_back(sf::Vector2f(0, 0));
player.push_back(sf::Vector2f(0 , 82));
player.push_back(sf::Vector2f(82, 0));
player.push_back(sf::Vector2f(82, 82));
std::cout << this->_x << " " << this->_y << std::endl;
for(auto& layer : layers)
if(layer.type == tmx::ObjectGroup)
for(auto& obj : layer.objects)
if(layer.name == "Objects")
for(auto& point : player)
if(obj.Contains(point + tmp))
return (true);
return (false);
}
this-_x
和 this->_y
是我的精灵左上坐标的位置。
frametime 是自我的主循环以来经过的秒数。
我的精灵大小是 82*82
我通过将精灵的原点设置为图像的中心解决了我的问题。于是我的盒子坐标就变成了
player.push_back(sf::Vector2f(-42, -42));
player.push_back(sf::Vector2f(42 , -42));
player.push_back(sf::Vector2f(42, 42));
player.push_back(sf::Vector2f(-42, 42));