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?

  • 2
    $\begingroup$ Just to be clear, are you looking for a description of how gravity would actually work in a small toroidal space (which is necessarily going to involve general relativity), or just a way to adapt Newtonian gravity to your Asteroids game? In the latter case the question would be off topic here but it might be suitable for migration to Stack Overflow. $\endgroup$
    – David Z
    Mar 5, 2012 at 0:42
  • $\begingroup$ I'm actually looking for a description of how gravity would work in such a space. Though I was hoping that it wouldn't be necessary to introduce anything newer than Newtonian physics - force is proportional to M1*M2/(r^2) $\endgroup$ Mar 5, 2012 at 0:49
  • $\begingroup$ Perhaps I don't have the vocabulary for this, could you clarify the word use of "topological"? I think you mean that it wraps back around on itself, is that right? I would agree with @DavidZaslavsky's off-topic point if you were looking for something that could be discontinuous. I think you're looking for something that smooth - then that is a thought experiment for not our universe's physics laws, but a consistent set of gravitation laws for the rectangular repeating space of the Asteroid game. $\endgroup$ Mar 5, 2012 at 1:17
  • 1
    $\begingroup$ @AlanSE that's right, a topologically toroidal space is one that wraps around on itself - otherwise known as periodic boundary conditions. John, if you don't want to involve general relativity, it would probably be better to reword your question as something like "Is there a consistent formulation of Newtonian gravity in a toroidal space?" Any question of the form "How does gravity really work in X?" can only be answered with GR. $\endgroup$
    – David Z
    Mar 5, 2012 at 1:48
  • 1
    $\begingroup$ By the way, on second thought, I guess my first comment that any question about adapting Newtonian gravity would be off topic isn't really correct. What I should have said is that a full understanding of how to implement gravity in a game involves more than just physics (so after you get your answers here, you might want to look at Game Development). $\endgroup$
    – David Z
    Mar 5, 2012 at 1:52

5 Answers 5


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.

  • $\begingroup$ "The problem is equivalent in electrostatics on a torus", neat! So this is applicable to real-world questions. (Though technically, the distance metric is euclidian-like in the space I'm thinking about. Different from a torus.) I was already beginning to contemplate your solution, but hadn't gotten around to writing the math out. I presume that some simplification can take place allowing you not to have to write the infinite summation. $\endgroup$ Mar 5, 2012 at 2:17
  • $\begingroup$ You could stop summing after a fixed distance away, e.g. Using the Euclidean metric is right. The formalism of potentials is the non-relativistic limit. You're summing over purely spatial distances in the denominator, as usual for potentials. $\endgroup$
    – josh
    Mar 5, 2012 at 2:38
  • $\begingroup$ There's no obvious way to do the "nearest copy" approach and have a continuous acceleration field. You'll want to introduce a fudge factor correction of the type $1/r^2-C$ where $C$ is a constant introduced to make the acceleration roughly zero at the crossover point. $\endgroup$ Mar 5, 2012 at 4:24
  • $\begingroup$ @AlanSE: in the context of video games, I think a fudge factor is allowed... $\endgroup$ Mar 5, 2012 at 11:53
  • 1
    $\begingroup$ @AlanSE: in a video game, the space is a discrete grid. The values of acceleration smaller than "pixel / (clock tick)^2" is meaningless. So while a nearest neighbor or finitely many copy sum approach will give discontinuous functions mathematically, practically a "fudge factor" would be present anyway from round offs and what not. $\endgroup$ Mar 5, 2012 at 13:58

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.


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 x

y-component y

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.

  • 3
    $\begingroup$ An additional complication is that it is unclear whether the model the OP considers is a 2-dimensional Newtonian gravity or a 2-dimensional slice of a 3-dimensional Newtonian gravity. In the former case the Newtonian potential is $\log (r)$ and the force is $1/r$. The pair-off and cancel approach gives you a gain of $1/r$ decay, so one naively expects that with pairing off you still have $1/r^2$ decay of the force with $n = O(r)$ many grid points, for which we still get a divergent sum. $\endgroup$ Mar 5, 2012 at 14:07
  • $\begingroup$ The divergence of a $1/r$ potential is non-trivial for this particular case. You're statements apply for a $1/(x+i)$ sum but the sum is really closer to a double sum of $1/(x+i)-1/(x+i+1)$. A single sum converges but it will probably diverge when you add the 2nd dimension in there. But yes, I don't know why we would entertain the idea of a 1/r force when there's no real reason to and no one expects it to converge in the first place. $\endgroup$ Mar 5, 2012 at 14:57
  • $\begingroup$ Numerical tests are no substitute for a rigorous proof. But say your prescription is convergent, are you sure it generates a force that varies continuously under small changes in the positions of the two point masses, e.g., if one of the masses enters a neighboring screen? $\endgroup$
    – Qmechanic
    Mar 5, 2012 at 21:39
  • $\begingroup$ @Qmechanic In this formulation the "mirror images" are 1 unit apart in the x and y directions and the spacecraft has an (x,y) coordinate. So maybe you're asking about placing the asteroid mirror images apart at some spacing other than 1, but as long as it averages to 1 the convergence will hold because the Taylor 2nd order approximation is valid for large $n$. Or maybe you're talking about spacing the mirror images 0.9 units apart in one direction and 1 unit in another direction, in which case I think it would diverge. But why talk about that? It has nothing to do with the question. $\endgroup$ Mar 5, 2012 at 21:49
  • $\begingroup$ Ah what one can do with Matlab and a little time. ... I miss my engineering days. Good commentary. I still feel like I can find a closed form solution - though I suspect you're correct that it's not as easy as it seems. $\endgroup$ Mar 6, 2012 at 4:20

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 $$


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.


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.

  • $\begingroup$ No, this is wrong! The convergence question only matters for $|z_{nm}|\gg 1$, and in that case, you have a mirror asteroid at $-z_{nm}$. When you vector sum those contributions you're left with a magnitude on the order of $1 d/dr (1/r^p)$. Your claims equate to the statement that gravity diverges if only points in a certain direction are considered. It converges for $1/r^2$ and I'm going to add a graph to my answer just to illustrate that. $\endgroup$ Mar 5, 2012 at 18:16
  • 1
    $\begingroup$ @AlanSE : His point (when he speaks of unconditional convergence) is that the convergence depends of the order in which the sum is made, which is problematic for the physical realism of the situation. Of course, your argument says that there is a way to have a convergent sum. the two are not contradictory. $\endgroup$ Mar 5, 2012 at 18:54
  • $\begingroup$ @FrédéricGrosshans Ok, thanks. But I still find parts of this hard to swallow. Yes, the convergence depends on the order of summation, but why does that challenge the physicality of the situation? I can sum up the electric fields from all electrons in the universe in a certain direction, ignoring the positive charges, and find it diverges easily. Why is that a problem? $\endgroup$ Mar 5, 2012 at 19:22
  • $\begingroup$ @AlanSE : Basically, the fact that it depends of the summation order means that it is sensitive to things which are unspecified in the model. For example, we assumed that the gravitation was propagating instantly. If it's propagates at a finite but high anisotropic speed, it will have effects. If the torus is not perfectly symmetrical, you can have macroscopic effect of very tiny deviation from the theory. And numerically, one need to be careful in the order where this addition is made. Basically, by always summing opposing reflection, you make supplementary (physical) assumptions. ... $\endgroup$ Mar 6, 2012 at 14:56
  • $\begingroup$ ... It is OK, but has to be explicit. $\endgroup$ Mar 6, 2012 at 14:57

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!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.