I am currently in the process of coding a game in my spare time using C++ and SFML 1.6. When it got around to be time to put collision detection in, I ran into an annoying bug where the program detects collision as if the object was shifted to the upper left by an amount differing based on the size of the stationary sprite I am using to test this, usually by about half its size. I can't seem to find what the bug is in my code, but perhaps you can. My code is listed below, thanks in advance.
#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;
}