我目前正在使用 C++ 和 SFML 1.6 在业余时间编写游戏。当是时候进行碰撞检测时,我遇到了一个烦人的错误,程序检测到碰撞,就好像对象被移动到左上角的量根据我使用的静止精灵的大小而不同测试这个,通常是它的一半大小。我似乎无法找到我的代码中的错误,但也许你可以。我的代码在下面列出,在此先感谢。
#include <SFML/Graphics.hpp>
#include <math.h>
bool collide_rec(sf::Sprite object_1, sf::Sprite object_2);
bool collide_point(sf::Vector2f point, sf::Sprite object);
void collide(sf::Sprite& movable, object& movable_data, sf::Sprite& sationary);
void collide(sf::Sprite& movable, object& movable_data, sf::Sprite& stationary)
{
//Handle Collision NOTE: Results in infinate loop if started on colliding object
for(;collide_rec(movable, stationary);)
{
if((movable_data.move.spd.x<0)||(movable_data.move.left==true||movable_data.direction==LEFT )) movable.Move( 1, 0);
if((movable_data.move.spd.x>0)||(movable_data.move.right==true||movable_data.direction==RIGHT)) movable.Move(-1, 0);
if((movable_data.move.spd.y<0)||(movable_data.move.up ==true||movable_data.direction==UP )) movable.Move(0, 1);
if((movable_data.move.spd.y>0)||(movable_data.move.down ==true||movable_data.direction==DOWN )) movable.Move(0, -1);
}
}
bool collide_point(sf::Vector2f point, sf::Sprite object)
{
}
bool collide_rec(sf::Sprite object_1, sf::Sprite object_2)
{
bool hit=true;
sf::Vector2f tl_1, br_1, tl_2, br_2;//Top-Left Coner, Botom Right Corner
//Assign the corners proporly
tl_1= object_1.GetPosition();
tl_2= object_2.GetPosition();
br_1= (object_1.GetPosition()+object_1.GetSize());
br_2= (object_2.GetPosition()+object_2.GetSize());
if((tl_1.x<tl_2.x)&&(br_1.x<tl_2.x) || (tl_1.x<br_2.x)&&(br_1.x<br_2.x)) //if both points are to the left or right on the x demtion
hit=false;
if((tl_1.y<tl_2.y)&&(br_1.y<tl_2.y) || (tl_1.y<br_2.y)&&(br_1.y<br_2.y)) //if both points are to the left or right on the y demtion
hit=false;
return hit;
}