You are not logged in.
Pages: 1
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
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
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
He said what you had to do at the start of his post.
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
I have devised a problem: one must term a problem which resolve by maximal quantity of algorithm! All your ideas please post here.
Offline
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?
Ok, that's an easier question to answer. All straight lines can be written in the form
Wrap it in bacon
Offline
Pages: 1