I have been looking at codes of several open source 2d games and I have found this function.
GetLineCollisionHit(Vector2 Position1, int Width1, int Height1, Vector2 Position2, int Width2, int Height2)
{
int tile1X= Position1.X + Width1 / 16.0);
int tile1Y= Position1.Y + Height1 / 16.0);
int tile2X= Position2.X + Width2 / 16.0);
int tile2Y= Position2.Y + Height2 / 16.0);
do
{
int dx= Math.Abs(tile1X- tile2X);
int dy= Math.Abs(tile1Y- tile2Y);
if (tile1X== tile2X && tile2Y== tile1Y)
return true;
if (dx> dy)
{
if (tile1X< tile2X)
++tile1X;
else
--tile1X;
(check collision)
}
else
{
if (tile1Y< tile2Y)
++tile1Y;
else
--tile1Y;
(return true if collision)
}
}
while (true);
return false;
}
What is really checking is if there is any collision of solid tiles between two points, for this look for the line between the two tiles. But I do not know if it is a coding problem since it is not implementing neither Bresenham's line algorithm nor DDA or similar. So what are you trying to find in this line dx>dy? What sense does it have to advance x while x > y since that way a line is not correctly created.