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 index1 =tile1X= Position1.X +Width1+ Width1 / 16.0);
int index2 =tile1Y= Position1.Y + Height1 / 16.0);
int num1 =tile2X= Position2.X + Width2 / 16.0);
int num2 =tile2Y= Position2.Y + Height2 / 16.0);
do
{
int dx= Math.Abs(index1 tile1X- num1tile2X);
int dy= Math.Abs(index2 tile1Y- num2tile2Y);
if (index1 ==tile1X== num1tile2X && index2 ==tile2Y== num2tile1Y)
return true;
if (dx> dy)
{
if (index1 <tile1X< num1tile2X)
++index1;++tile1X;
else
--index1;tile1X;
(check collision)
}
else
{
if (index2 <tile1Y< num2tile2Y)
++index2;++tile1Y;
else
--index2;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.