Simple question about 2D wrapped Coordinate System

Started by
3 comments, last by taby 12 years ago
Hi all,
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)



[font=courier new,courier,monospace]__________________________________
|..........|..........|..........|
|..........|..........|..........|
|..........|..........|..........|
|........1*|........2*|........3*|
|__________|__________|__________|
|..........|..........|..........|
|..........|..x.......|..........|
|..........|..........|..........|
|........4*|........5#|........6*|
|__________|__________|__________|
|..........|..........|..........|
|..........|..........|..........|
|..........|..........|..........|
|........7*|........8*|........9*|
|__________|__________|__________|[/font]


To determine which point to "Follow" I determine which point is closest (1-9) and then I move
[font=courier new]the point i do my calculation to[/font] that one.


[source]
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.distanceSq(object);
if (i == 0 || dist < minDist) {
minDist = dist;
minIndex = i;
}
}
rst.x = points[minIndex].x;
rst.y = points[minIndex].y;[/font]
[/source]

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

Advertisement
not sure if this works, but i would take the one with the smaller absolute size (tar.x and bot.x being x positions of target and bot, mao.x being map x size) and use the non-absolute version as the relative position and get the direction from that.

poor design pseudocode:

realx=tar.x - bot.x
wrappedx=(map.x - bot.x) + tar.x //These for each axis

relativex=abs(realx) < abs(wrappedx) ? realx : wrappedx;
relativey=abs(realy) < abs(wrappedy) ? realy : wrappedy;

Direction=MakeThisVectorAnUnitVectorOkay(Vector(relativex,relativey));


(edit: removed y axis for some reason)

o3o

First some observations:

  1. You can do the calculation for each axis individually.
  2. The distance you have to move along any axis is never larger than half the size of the square.
  3. If the direct distance on the surface is longer than half the size of the square, you move the other direction instead.

Point 1 means we can reduce the problem into thinking only in one dimension. You don't have to think about this problem in two dimensions; one dimension is enough, and you can replicate the calculations to both dimensions (or to however many dimensions you want, in fact).

To begin with, calculate the distance you would have to travel if this wasn't a toroidal surface. Say you want to move from point A to point B, so the distance to move is then V=B-A.

Point 2 states that we should not have to move longer than half the size. Point 3 furthermore states that if we do, then we move in the opposite direction instead. With that in mind, we can wrap the vector V if it is too long.
[source]
A = start point
B = end point
V = B-A
if(abs(V) > size/2)
V = V - size
end
[/source]

edit: Repeat for all dimensions individually as stated in point 1 of course... and apparently beaten by a minute or two.
Thanks so much. I look now and see how obvious it was. Heres the code I used in the end if anyone wants it.
[source]

[font=courier new,courier,monospace]

rst.x = pos.x-obj.x;
rst.y = pos.y-obj.y;

if (abs(rst.x) > sizeX / 2) {
if (rst.x > 0) {
rst.x = pos.x + sizeX;
} else {
rst.x = pos.x - sizeX;
}
} else {
rst.x = pos.x;
}
if (abs(rst.y) > sizeY / 2) {
if (rst.y > 0) {
rst.y = pos.y + sizeY;
} else {
rst.y = pos.y - sizeY;
}
} else {
rst.y = pos.y;
}

[/font]
[/source]
Thanks for the help
Joey
That's right on, and I wish I had replied. The space you are talking about is a toroidal space.

Analogously, in the case of a spherical space, the shorter distance is called the orthodromic distance. Fun times.

This topic is closed to new replies.

Advertisement