I have a question. I'm playing around some 2D game dev, particularly AI and i've come onto a problem that I would love some help in solving. I know that there must be some simple solution to my problem and I'm over complicating it, I've looked but cant seem to find anything.
The problem is that I have a 2D world that wrapped around a 2D playing field, i.e. (when x > wide : x = 0 ...). I'm trying to do simple follow and flee type AI and I had the obvious problem that when one object is following another which goes off the edge of the screen and appears at the opposite side, it does not follow off the edge but has to turns around.
The only robust way that I could come up with is to calculate is as follows :
Assume that the pos can exists at 9 position (for each side of the screen) i.e
In the following X is following # (the real grid in in the middle one)
__________________________________
|..........|..........|..........|
|..........|..........|..........|
|..........|..........|..........|
|........1*|........2*|........3*|
|__________|__________|__________|
|..........|..........|..........|
|..........|..x.......|..........|
|..........|..........|..........|
|........4*|........5#|........6*|
|__________|__________|__________|
|..........|..........|..........|
|..........|..........|..........|
|..........|..........|..........|
|........7*|........8*|........9*|
|__________|__________|__________|
To determine which point to "Follow" I determine which point is closest (1-9) and then I move
the point i do my calculation to that one.
Vector2D object; //This is the position of my object
Vector2D pos; //This is the position the object is tracking in the "Real" grid
Vector2D rst; //This is the position of the object even if it goes off the edge of the screen;
Vector2D[] points = new Vector2D[9];
points[0] = new Vector2D(pos.x - width, pos.y - height);
points[1] = new Vector2D(pos.x, pos.y - height);
points[2] = new Vector2D(pos.x + width, pos.y - height);
points[3] = new Vector2D(pos.x - width, pos.y);
points[4] = new Vector2D(pos.x, pos.y);
points[5] = new Vector2D(pos.x + width, pos.y);
points[6] = new Vector2D(pos.x - width, pos.y + height);
points[7] = new Vector2D(pos.x, pos.y + height);
points[8] = new Vector2D(pos.x + width, pos.y + height);
int minIndex = 0;
float minDist = 0;
float dist = 0;
for (int i = 0; i < 9; i++) {
dist = (float) points[i].distanceSq(object);
if (i == 0 || dist < minDist) {
minDist = dist;
minIndex = i;
}
}
rst.x = points[minIndex].x;
rst.y = points[minIndex].y;[/font]
Can anyone give me any advice on this? What I do above works provide I move the positon of the object i'm following before I start my calculations to follow, but this seems a bit overkill. There must be a simpler way of doing this.
Any help or direction would be greatly appreciated.
Regards
Joey






