Skip to main content
added 9 characters in body
Source Link

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.

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

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.

Source Link

What is the meaning of this line algorithm?

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