# Thread: calculations in an unbounded 2D space

1. ## calculations in an unbounded 2D space

Greetings, O wise and knowledgable math geniuses. Is there an elegant system for representing a finite, unbounded 2D space and performing the common 2D geometric calculations (distance between points, movement, etc.) on it in a coherent way? I want to code an arcade game played on a 2D space that is finite but wraps around in every direction. In past attempts I just represented space as a bounded box, and I corrected coordinates after every calculation that produced them, but this was rather messy. Is there a better approach?

My first thought was to somehow map the 2D space over a sphere and translate 2D calculations into 3D calculations around the sphere.

2. ## Re: calculations in an unbounded 2D space

i think you can do this:

let frac(x) be the fractional part of x (for example if x = 6.25, frac(x) = 0.25). this can also be calculated as x - floor(x).

assign all your coordinates on your "screen" (world) values between 0 and 1. then define:

(a,b) + (c,d) = (frac(a+c), frac(b+d)).

if you want "on-screen" distances, apply frac before you calculate the distance. if you want "off-screen distances" (like you shoot at the right end of the screen, and it hits something on the left side) don't apply frac at all (note: this means two objects can be "multiple distances away from each other"...could get confusing).

using a map of your "world" to a sphere will identify all 4 corners (and edges) as the same point...what i think you want to do to achieve left/right wrap-around and up/down wrap-around is to map your world to a torus (doughnut). while you could parameterize such a surface in 2 variables like so:

x = Rcos(s) + rcos(s)cos(t)
y = Rsin(s) + rsin(s)cos(t)
z = rsin(t)

and calculate distance using arc-length, i think the "frac" approach above would be easier to implement (basically, we are identifying neighboring worlds (off-screen) with the one you can see (on-screen)).

3. ## Re: calculations in an unbounded 2D space

Originally Posted by infraRed
Is there an elegant system for representing a finite, unbounded 2D space and performing the common 2D geometric calculations (distance between points, movement, etc.) on it in a coherent way? I want to code an arcade game played on a 2D space that is finite but wraps around in every direction.

On any metric space, if $d(x,y)$ is the metric then $e(x,y)=\frac{d(x,y)}{1+d(x,y)}$ is also a metric.

Moreover, it is a bounded metric, i.e. $e(x,y)\le 1.$

4. ## Re: calculations in an unbounded 2D space

Originally Posted by Plato

On any metric space, if $d(x,y)$ is the metric then $e(x,y)=\frac{d(x,y)}{1+d(x,y)}$ is also a metric.

Moreover, it is a bounded metric, i.e. $e(x,y)\le 1.$
this is true, but it tends to "compress" the edges. if the OP is doing something like the game asteroids, this wouldn't work...the edges of the screen are "infinity" and you can't ever cross them.

5. ## Re: calculations in an unbounded 2D space

Originally Posted by Deveno
this is true, but it tends to "compress" the edges. if the OP is doing something like the game asteroids, this wouldn't work...the edges of the screen are "infinity" and you can't ever cross them.
Yes, basically like Asteroids -- space wraps around in whichever direction you go -- except that space is larger than just the screen width and height. So, by "unbounded" I meant that one can move infinitely long in one direction in space, but it is finite because this periodically brings you back to your starting point. So if you were passed by a spaceship going faster than you in the same direction, you would eventually see it approaching from the rear.

6. ## Re: calculations in an unbounded 2D space

A "finite unbounded space" would be just the opposite- a finite set of points such that there exist arbitrarily large distances between points. And it seems to me that the only way you could do that would be to have an infinite distance between some of the points.