Gravitation in a space that is topologically toroidal In my scant spare time I'm building an Asteroids game. You know - a little ship equipped with a pea shooter and a bunch of asteroids floating around everywhere waiting to be to blown up. But, I wanted to add a little twist. Wouldn't it be cool if Newtonian gravity was in effect and you could do thinks like enter into an orbit around an asteroid and fire at it, or shoot gravity assisted bankshots around a massive asteroid so that you can shoot one behind it.
But the problem is that in asteroids, space is topologically toroidal. If you fly off of the top of the screen, you reappear at the same x coordinate at the bottom of the screen (and similarly for the right of the screen). So how does one calculate the distance between two bodies in this space? Really, I realize that this question doesn't make sense because body A would pull upon body B from a variety of directions each with their corresponding distances.
But anyway, the main questions: How would Newtonian gravity work in toroidal space? AND Is there any applicability to the answer to this question outside of my game?
 A: Forgetting about the specifics of your problem, you say you want to work in the Newtonian regime for gravitation on a toroidal space.  The way this differs from a non-toroidal space is that you can "unroll" the torus into an infinite lattice of duplicates.  This is a lot like the lattice of mirror charges if you were doing electrostatics on a torus (the problems are clearly equivalent).  So, the force from body B on body A is the sum of the forces of all B's multiple copies, one per cell in the unrolled version.  Add each of these force vectors together, and there's the force exerted by B on A in this toroidal universe.  So that's an infinite sum but the terms die off like $\text{distance}^{-2}$.
Back to your problem though, it may just be easier to neglect that detail and simply compute the force between A and the "nearest copy" of B.
A: I'm going to assume your window has dimensions of 1x1, in length units.  Since it's a video game units don't really matter.  The gravity acceleration you're looking for will be a vector with 2 components.
$$\langle a_x, a_y\rangle$$
Next, I'm going to say the spaceship is at the origin with the asteroid at (x,y).  This just makes it easier.  The gravity from a single asteroid will be: 
$$\vec{a} = \frac{\vec{r}}{\left| \vec{r} \right|^3}$$
$$a_x = \frac{-x}{ \left( x^2 + y^2 \right)^{3/2}}$$
$$a_y = \frac{-y}{ \left( x^2 + y^2 \right)^{3/2}}$$
Here again I exercised reckless disregard for units.  One way to tackle your problem is to actually sum up the acceleration from an entire infinite lattice of masses that repeat every x and y increment.  If you do this for the top right quadrant alone you'll simply find it diverges.  A way around this is to sum the contribution from a point of index (i,j) with a point in the 3 other quadrants of indices (-i,j), (i,-j), and (-i,-j).  That series will converge for the calculated acceleration but not the potential.  Here is a formalism of what I've described.
$$x_{i} = x+i$$
$$x_{-i} = x-1-i$$
$$y_{j} = y+j$$
$$y_{j} = y-1-j$$
I've reserved the $i=0$ and $j=0$ values for the 4 asteroids closest to the origin.  Now we can write the acceleration from a given set of 4 asteroids with one (i,j) index pair.
$$a_{xij} = \frac{-x_{i}}{(x_{i}^2+y_{j}^2)^{3/2}} - \frac{x_{-i}}{(x_{-i}^2+y_{j}^2)^{3/2}} - \frac{x_{i}}{(x_{i}^2+y_{-j}^2)^{3/2}} - \frac{x_{-i}}{(x_{-i}^2+y_{-j}^2)^{3/2}}$$ 
$$a_{yij} = \frac{-y_{j}}{(x_{i}^2+y_{j}^2)^{3/2}} - \frac{y_{j}}{(x_{-i}^2+y_{j}^2)^{3/2}} - \frac{y_{-j}}{(x_{i}^2+y_{-j}^2)^{3/2}} - \frac{y_{-j}}{(x_{-i}^2+y_{-j}^2)^{3/2}}$$ 
Then we can just take these contributions and do a double infinite sum.
$$a_x = \sum_{j=0}^{\infty} \sum_{i=0}^{\infty} a_{xij}$$
$$a_y = \sum_{j=0}^{\infty} \sum_{i=0}^{ \infty} a_{yij}$$
We're basically finished here.  I thought it would be easy to get a closed form solution for the series, but it really isn't.  I did implement it numerically.  Note that your calculations grow as $n^2$ where $n$ is the upper limit of your series because it's a double sum.  I found that $n=0$ gives an error about 5% and drops rather slowly for higher values.  When making your code, you'll need to consult a mathematician or apply some set of tricks in order to a smoothly behaving code.  You'll probably find it easiest to calculate a vector field that has good accuracy and then just interpolate values from that when running the code.  However, that's only for points far from the asteroid.  Once you get very close to the asteroid this method will break down and you'll want to use an integration technique that can handle the large slope in that region.  Thankfully, the contribution from the asteroid mirror images will diminish as you get close to it, and you can transition to a single $1/r^2$ acceleration.
Addition:
There seems to be some skepticism as to whether or not the series should converge at all.  I assure you it does.  Below are the x and y components ($\langle a_x, a_y\rangle$) of the acceleration computed with the method described here, doing a sum to $5$.  That means, $5^2=25$ terms in the series sum with 4 asteroids for each term, resulting in 100 total asteroids.  So the following is the acceleration from a mesh of 100 asteroids in a 10x10 mesh around the origin.  You can increase the side form 10 to 400 asteroids and you only get a small revision in these numbers.
x-component

y-component

This is smooth, continuous, and repeating, exactly as expected.  To a very good level of accuracy, this is the field from an infinite mesh of asteroids doing a $1/r^2$ sum as described above.  It does converge.
A: Newtonian gravity, as such, cannot exist on any closed manifold, and in particular not on the torus. Here we take Newtonian gravity to mean that we consider a gravitational potential $\Phi$ generated by a mass density $\rho$ such that $\triangle \Phi = \rho$ where $\triangle$ is the Laplace-Beltrami operator. However, on a closed manifold, we have that $\int \triangle \Phi \mathrm{dvol}_g = 0$ by the divergence theorem, whereas since we generally prefer our matter to have positive and not negative masses, the right hand side integrates to $\int \rho \mathrm{dvol}_g \geq 0$ with equality only when the mass density vanishes everywhere! 
The usual solution to this problem is to (a) use that total mass is conserved and (b) add some uniform "dark energy" contribution. That is, we assume that we have a mass distribution $\rho$ with total mass $\int \rho\mathrm{dvol}_g = M$. Assume our closed manifold has total volume $V$. Then instead of considering the function $\Phi$ such that $\triangle \Phi = \rho$, we consider the function $\Phi$ such that $\triangle \Phi = \rho - M/V$ and call this $\Phi$ the "gravitational potential".  In the limit where you take really large torus so that the situation looks more and more like normal Euclidean space, since $V\to \infty$ you formally get that the limiting case solves $\triangle \Phi = \rho$ as we are used to. 
In the case of the torus, this modified Newtonian gravitation potential given by a mass $M$ sitting at position $w$, when evaluated at the point $z$, is now $G(z,w)M$, where $G(z,w)$ is the Green's function for the equation $\triangle \Phi = \rho - M/V$, and which can be explicitly written in terms of theta functions and eta functions, if we unfold the torus and identify it with the unit square:
$$ G(z,w) = - \frac{1}{4\pi} \log \left| \frac{\vartheta_1(z-w;i)}{\eta(i)}\right| + \frac12 \left(\Im (z-w)\right)^2 $$
(Source.)
As you can see, if you want to actually be mathematical about it, the formula gets rather complicated and hard to compute. For programming your video game, it is perhaps much better to just chose a potential function that vanishes outside a sufficiently large ball. For example, instead of thinking about the gravitational potential 
$$ -\frac{GM}{r} ~\textrm{in three dimensions, or}\quad -GM\log r ~\textrm{in two dimensions} $$
let $\Psi(r)$ be a differentiable bump function that vanishes if $r > R$ and assume the potential looks like
$$ - \frac{GM}{r} \Psi(r) \quad \textrm{or}\quad -GM \log\frac{r}{R} \Psi(r) $$
This way to compute the potential you just need to sum over finitely many images of each object. 
A: Here we will just consider a Newtonian-like generalization of gravity, i.e., a central $1/r^p$ force law, where $p>0$ is some power. (We will not necessarily assume a Gauss' law.)
Consider the 2d torus as a complex plane $z\in\mathbb{C}$ with double-periodic identifications $z\sim z+1$ and $z\sim z+\tau$. Here $\tau$ is a complex modulus parameter, which is pure imaginary for rectangular screens. Let us for simplicity put the complex modulus parameter to $\tau=i$, so the screen is just the unit square.
Let a point mass be located at position $0$, and a second point mass in position $z$.  For a $1/r^p$ force law, the gravity force becomes a double-series (over all mirror images) proportional to
$$ F ~\propto~ \sum_{n,m\in\mathbb{Z}} \frac{z_{nm}}{|z_{nm}|^{p+1}}, 
\qquad\qquad z_{nm}~:=~z-(n+m\tau). $$
The corresponding absolute double-series reads
$$  \sum_{n,m\in\mathbb{Z}}  \frac{1}{|z_{nm}|^p}. $$
It is straightforward to prove that  this is absolutely divergent for $0\leq p \leq 2$, and absolutely convergent for $p>2$. So the smallest integer power force law that converge absolutely (and therefore unconditionally), is a $1/r^3$ force law. This follows, basically, from the following estimate
$$ \sum_{k=2}^{\infty} \frac{k-1}{k^p}
~=~\sum_{n,m\in\mathbb{N}} \frac{1}{(n+m)^p}
~\leq~\sum_{n,m\in\mathbb{N}} \frac{1}{(n^2+m^2)^{\frac{p}{2}}}
~\leq~\frac{1}{2}\sum_{n\in\mathbb{N}} \frac{1}{n^{\frac{p}{2}}}\sum_{m\in\mathbb{N}} \frac{1}{m^{\frac{p}{2}}}, $$
and the standard fact that
$$\sum_{n\in\mathbb{N}} \frac{1}{n^p}~\left\{\begin{array}{ccc} =\infty &{\rm for} &p\leq 1,  \cr\cr  <\infty &{\rm for} &p> 1.\end{array} \right. $$
By the way, for a related discussion, see this question.
A: This is kind of a funky answer, but I'd say this--
Calculate everything in Eliptical geometry where the square display that the user sees is just the representation of a sphere that everything is moving around the surface. (Like how a flat map is the representation of a globe. 
That way every asteroid and the spaceship would have a gravitational pull that wouldn't change when they appeared to move from the top to the bottom of the display. 
But it would open up another whole group of problems. 
Good luck!
