• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
joey.enfield

Simple question about 2D wrapped Coordinate System

4 posts in this topic

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)



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


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[i].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
0

Share this post


Link to post
Share on other sites
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)
0

Share this post


Link to post
Share on other sites
First some observations:[list=1]
[*]You can do the calculation for each axis individually.
[*]The distance you have to move along any axis is never larger than half the size of the square.
[*]If the direct distance on the surface is longer than half the size of the square, you move the other direction instead.
[/list]
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0