Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2007-11-05 04:36:50

wolfcomm
Member
Registered: 2007-11-05
Posts: 2

Programming Algorithm

I'm programming a strategy game, and I'm making an AI algorithm that will move units to their destinations. I need to make sure that units won't run into walls though.

So I need a function (parameters: origin x, origin y, destination x, destination y, object x1, object y1, object x2, object y2). The object in this case would be a wall with two points (x1, y1, x2, y2). The function needs to return a boolian (true/false) if the wall intersects the path. As in, does it cross between the origin and destination. In other words, we have two finite lines (path of movement and wall), and I need to know if they cross.

How do I do that?

Thanks in advance!

Offline

#2 2007-11-05 05:58:15

TheDude
Member
Registered: 2007-10-23
Posts: 361

Re: Programming Algorithm

Find the point on the grid where the 2 lines would intersect and determine if that point is between the origin and the destination.  Here's how I would do it in C:

bool func(double originx, double originy, double destx, double desty, double objx1, double objy1, double objx2, double objy2)
{
bool pathInf = false, wallInf = false;
if (destx == originx)
{
   pathInf = true;
}
if (objx2 == objx1)
{
   wallInf = true;
}
if (pathInf && wallInf)
{
   if (originx != objx1)
   {
      return false;
   }
   if ( (originy <= objy1 && originy >= objy2) ||
        (originy <= objy2 && originy >= objy1) ||
        (desty <= objy1 && desty >= objy2) ||
        (desty <= objy2 && desty >= objy1) )
   {
      return true;
   }
   return false;
}
double slopePath, slopeWall, interceptPath, interceptWall;
if (!pathInf)
{
   slopePath = (desty - originy) / (destx - originx);
   interceptPath = originy - (slopePath * originx);
}
if (!wallInf)
{
   slopeWall = (objy2 - objy1) / (objx2 - objx1);
   interceptWall = objy1 - (slopeWall * objx1);
}
if (!pathInf && !wallInf)
{
   if (slopePath == slopeWall)
   {
      if (interceptPath != interceptWall)
      {
         return false;
      }
      else
      {
         if ( (originy <= objy1 && originy >= objy2) ||
              (originy <= objy2 && originy >= objy1) ||
              (desty <= objy1 && desty >= objy2) ||
              (desty <= objy2 && desty >= objy1) )
         {
            return true;
         }
         return false;
      }
   }
   else
   {
      double x = (interceptWall - interceptPath) / (slopePath - slopeWall);
      if ( (x <= originx && x >= destx) ||
           (x <= destx && x >= originx) )
      {
         return true;
      }
      return false;
   }
}
if (pathInf)
{
   if ( (originx <= objx1 && originx >= objx2) ||
        (originx <= objx2 && originx >= objx1) )
   {
      return true;
   }
   return false;
}
else
{
   if ( (objx1 <= originx && objx1 >= destx) ||
        (objx1 <= destx && objx1 >= originx) )
   {
      return true;
   }
   return false;
}
}


This function will return true if the paths collide and false if they do not.  Note that this function (should) determine if 2 line in the xy-plane will collide.  I made no assumptions about the ability of the units in your game to move.  For example, if your units can only move up/down and left/right then a lot of this code is unnecessary.  If that's the case then you should be able to remove a lot of the code, particularly everything dealing with slope.  If they are points that can move anywhere in the xy-plane, though, this should work.


Wrap it in bacon

Offline

#3 2007-11-10 10:26:45

wolfcomm
Member
Registered: 2007-11-05
Posts: 2

Re: Programming Algorithm

I program in Basic (small basic to be precise). Can you explain this in english? It doesn't have to be so comprehensive, I just need to know what to look out for, and I can program it myself.

Thanks again.

Offline

#4 2007-11-10 11:31:27

mathsyperson
Moderator
Registered: 2005-06-22
Posts: 4,900

Re: Programming Algorithm

He said what you had to do at the start of his post.

TheDude wrote:

Find the point on the grid where the 2 lines would intersect [if they were infinite] and determine if that point is between the origin and the destination.

(italics are mine, for clarity)

Because you're interested in two lines, there's only one place they could possibly cross (unless they're parallel, in which case they never will).
You just need to find that point, and then check whether the point is in both the path of movement and the wall. If it's in both then you've got a collision, if it's not then you don't.


Why did the vector cross the road?
It wanted to be normal.

Offline

#5 2007-11-11 02:33:32

Spirinus
Member
Registered: 2007-11-10
Posts: 1

Re: Programming Algorithm

I have devised  a problem: one must term a problem which resolve by maximal quantity of algorithm! All your ideas please post here.

Offline

#6 2007-11-11 23:28:38

wolfcomm (author)
Guest

Re: Programming Algorithm

mathsyperson wrote:

He said what you had to do at the start of his post.

TheDude wrote:

Find the point on the grid where the 2 lines would intersect [if they were infinite] and determine if that point is between the origin and the destination.

(italics are mine, for clarity)

Because you're interested in two lines, there's only one place they could possibly cross (unless they're parallel, in which case they never will).
You just need to find that point, and then check whether the point is in both the path of movement and the wall. If it's in both then you've got a collision, if it's not then you don't.

This is exactly what I need to know how to do. How do I find the point on the grid where the 2 lines would intersect?

#7 2007-11-12 03:11:03

TheDude
Member
Registered: 2007-10-23
Posts: 361

Re: Programming Algorithm

Ok, that's an easier question to answer.  All straight lines can be written in the form


where m is the slope of the line and b is the y-intercept.  To find the slope, use the equation

Note that if
  then m is infinite.  This means that the line goes straight up and down, and the line's equation is of the form

where c is the x-intercept.
After you find m, calculate b with the equation

Now, after you find m and b for both lines (the wall and the path) you can solve for where they meet with the simultaneous equations

Then just plug x into either of the equations to solve for y.  This will be the point where they intersect.  Note that if the slopes for both lines are equal then they are parallel and will never intersect, unless they also have the same y-intercept (b), in which case they are the same line and intersect at every point.


Wrap it in bacon

Offline

Board footer

Powered by FluxBB