Sign in to follow this  
mastermind88

Cycled (infinite) maps

Recommended Posts

Hi all!
Anybody seen cycled maps in games? You can see example in pacman game for android: [url="https://play.google.com/store/apps/details?id=namco.pacman.cedemo."]https://play.google.....pacman.cedemo.[/url] When you come to the right corner of the maze you finally appear in the left part and vise versa.
it's not just teleport, look's like rendering two screens side by side. Don't know how to explain clearer [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
May be there is more appropriate (than my "cycled map") name for that trick? Do you have any ideas how to do that?

Share this post


Link to post
Share on other sites
Doing some random searches, there is a wikipedia page that seems to be what you mean:

[url="http://en.wikipedia.org/wiki/Wraparound_(video_games)"]Wraparound[/url]

I've also heard the terms "torus wrapping" or "toroidal playing field" used.


(Edit) I guess Wraparound is incorrect; you're interested in rendering such a playing field as one continuous area. As far as actually rendering such a thing, I've only seen this in an old game called Star Control (and its sequels). Star Control 2 has an open source project written in C called "Ur-Quan Masters" (abbreviated UQM) if you are interested to see how they implemented it.

In UQM, there are two spaceships fighting in 2D space, top down (like Asteroids or SpaceWar), with other things in the arena such as asteroids and a planet. The playing field itself wraps on both X and Y axes. The camera is always centered between the two ships. If the ships move too far apart, the camera snaps to a new location, making it appear that one of the ships has wrapped around from the other side of the arena. The ships are also allowed to both fly in one direction indefinitely, but you'll see the planet and asteroids wrapping around.

I haven't dived into the UQM code that handles rendering or object positioning, but I suspect they may use a technique called "relocation" - when one of the ships moves off the edge of the screen, or exceeds limits in certain coordinates, the camera center and all objects are imediately repositioned to APPEAR that wrap-around has occurred.


In a game such as pac-man, you could probably achieve this by rendering the playing field multiple times next to each other. Unfortunately UQM cannot do this; It renders a starfield with several layers of parallax in the background which cannot use this kind of simple rendering wrapping. Edited by Nypyren

Share this post


Link to post
Share on other sites
The short answer is that pac man space is "toroidal", which means donut-shaped. There are two directions that you can travel which wrap around.

Share this post


Link to post
Share on other sites
Wow, thanks guys! You gave me a lot of mental pabulum :)


[quote name='jefferytitan' timestamp='1344397649' post='4967238']
The short answer is that pac man space is "toroidal", which means donut-shaped. There are two directions that you can travel which wrap around.
[/quote]
Nice idea to make toroidal space, but how to use it in 2D game development? Do you have any samples or so?

Share this post


Link to post
Share on other sites
[quote name='mastermind88' timestamp='1344398445' post='4967240']
Nice idea to make toroidal space, but how to use it in 2D game development? Do you have any samples or so?
[/quote]

The "torus" is just an analogy. It's not a real 3D shape, it just means that if you continue to the left or right, you will loop, and also if you move up or down you will loop. This is like how the surface of a torus wraps around in those two ways.

Here's a video of UQM in action. Watch what happens when the ships pass the edges or corners of the screen: [media]http://www.youtube.com/watch?v=BeZNoEVycOc[/media] Edited by Nypyren

Share this post


Link to post
Share on other sites
From video I see that screen just switches when reaching edge, but in pacman (from link above) there is no instant screen switches. You can go and go left (or right) forever and camera will follow you.
Imagine you have a cilinder (as I need looping only on X axis) and camera moving around it, just like in civilization.

Infinite game world, that's what I mean. Edited by mastermind88

Share this post


Link to post
Share on other sites
This blog has some information on it, your essentially drawing the game world twice (just different parts of it) as you go over the borders
http://www.wildbunny.co.uk/blog/2011/12/11/how-to-make-a-2d-platform-game-part-1/

Share this post


Link to post
Share on other sites
It's only an analogy because there's no embedding space in which to step out into, which is to say it's not wholly an analogy.

Share this post


Link to post
Share on other sites
[quote name='mastermind88' timestamp='1344401856' post='4967254']
From video I see that screen just switches when reaching edge, but in pacman (from link above) there is no instant screen switches. You can go and go left (or right) forever and camera will follow you.
Imagine you have a cilinder (as I need looping only on X axis) and camera moving around it, just like in civilization.

Infinite game world, that's what I mean.
[/quote]

Are you familiar with the mod operation? I'll use real numbers for the coordinates, but integers work similarly. Say that you have a flat, repeating 1D space that is 2*pi in length (and has an origin at position 0), and that you're currently at position 1.5. You then move ahead by 16.2 units of distance, which gives you a new position of 16.2 + 1.5 = 17.7, but this is problematic because any position value larger than or equal to the space's length of 2*pi is simply too large. What you need to do to get the valid position is divide 17.7 by 2*pi and take the remainder to get your new, valid position. A straightforward and transparent way of doing this is to repeatedly substract 2*pi from this initial value of 17.7 until the result is less than 2*pi. You should get a new, valid position of 5.13 or so.

If you don't want to do the subtraction yourself (you shouldn't), the mod operation can do it for you automatically...

[source]
#include <cmath>
#include <iostream>
using namespace std;

int main(void)
{
const double pi = 4*atan(1.0);

const double world_length = 2*pi;

double old_pos = 1.5;
double movement = 16.2;
double new_pos = old_pos + movement;

while(new_pos >= world_length)
new_pos -= world_length)

cout << "The remainder is..." << endl;
cout << new_pos << endl;
cout << "Which is equivalent to..." << endl;
cout << fmod(old_pos + movement, world_length) << endl;

return 0;
}
[/source]

The mod operation can be performed using the % operator when using integers. Somewhat similarly to the example given above, say that your space is 64 units in length (and has an origin at position 0). If you start at position 63 and you move ahead 1 unit, then you arrive at the new, invalid position of 64. To touch this value up in order to make it valid, you mod it by 64 (your space's length), which gives the new, valid position of 64 % 64 == 0. If your new position happens to be negative, I'm sure you're familiar enough with mod now to work out what you need to do with the code. For instance, if you start at position 2 and you move -5 units, you should arrive at the new, invalid position of -3. Maybe try seeing how -3 % 64 is useful in a situation like this. Also, watch out for invalid positions like -65... Maybe try seeing how -65 % 64 is useful in a situation like this. Mod is pretty useful.

For a flat, repeating 2D space, you can just do a mod operation on each of the individual position components -- because this repeating space is flat, you don't have to worry about mixing the position components together in any fancy way. Ditto for a flat, repeating 3D space, etc, etc.

A technical, fancy term for a flat, repeating (aka closed) space is a "toroidal space". This is in opposition to curved, repeating spaces, such as a spherical space (which would involve a bit of mixing of the position components, which is not a lot harder, just not really like the Pacman example that you're describing).

Now, all that said, you give a second example of a cylindrical space, which just means that one dimension doesn't repeat. Instead of doing a mod operation on that non-repeating dimension, just limit the values accordingly if they're invalid. Do you need full code for this, or are you willing to try it out on your own first?

I've never played Civilization. Is the space laid out flat onto the screen, like in Pacman, or is the world literally in the shape of a cylinder that is embedded (floating) in 3D space? Your post doesn't make that clear. Edited by taby

Share this post


Link to post
Share on other sites
[url="http://en.wikipedia.org/wiki/Tetrisphere"]Tetrisphere[/url] is a game that first springs to mind that actually use a torus. The game could not work on a sphere so instead they used a torus and did some perspective tricks to make it look like a sphere.

taby shows how you can resolve what a position would be. Actually rendering the same scene wrapped around involves different techniques though. A [url="http://en.wikipedia.org/wiki/Portal_rendering"]portal rendering system[/url] is one that could be used to accomplish this. It depends on what kind of game world you have though.

Share this post


Link to post
Share on other sites
Hello!
I'm developing a 2D "MMO"RPG (still very early) whose main feature is that the world is "spherical" like the Earth xd.
Here is a video demo, where you can see how it works. (0:48 if you dont want wait jeje).
Player1 in PC1 starts walking in right direction, Player2 in PC2 starts walking in left direction, then they find each other...

[media]http://www.youtube.com/watch?v=HgTHaBS0rMw[/media]

Hope you like it.
Regards

pd: sorry for my english, i'm from Argentina xd.

Share this post


Link to post
Share on other sites
[quote name='wallytsx' timestamp='1344524940' post='4967809']
Hello!
I'm developing a 2D "MMO"RPG (still very early) whose main feature is that the world is "spherical" like the Earth xd.
Here is a video demo, where you can see how it works. (0:48 if you dont want wait jeje).
Player1 in PC1 starts walking in right direction, Player2 in PC2 starts walking in left direction, then they find each other...

[media]http://www.youtube.com/watch?v=HgTHaBS0rMw[/media]

Hope you like it.
Regards

pd: sorry for my english, i'm from Argentina xd.
[/quote]

Thanks for sharing your implementation. :)

I'd give you a positive vote, but clearly the last one that I handed out in this thread was a total waste, and I wouldn't want to insult you. :)

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