I have a gravity-related question. I am programming an orbit simulator. I have everything up and running, but I would like to render the smaller body's orbital path (the larger body is fixed). To do this, I need the parametric equations for the body's position for any given time (i.e. I need an $x(t)$ and $y(t)$ function). I have access to each body's position, velocity, and mass. I have no other accessible variables.
P.S. Here is a link to what I have so far. The equations currently used to plot the orbit are $x(t)=x+v_xt$ and $y(t)=y+v_yt$.
Answer
It seems you've done the hard part already, which is to evolve the object's position as a function of time. And moreover, the simulation seems stable over a number of orbits. (But eventually things start to go wrong; you may want to look at an answer I wrote to What is the correct way of integrating in astronomy simulations?)
So my understanding is all you really need is the full orbit plotted. In that case, there's no need to make it a function of actual time (that's hard). Instead, we can fall back to Kepler's First Law, which says that the separation $r$ between the bodies obeys $$ r = \frac{r_\mathrm{max}(1-e)}{1+e\cos(\theta)}. $$ $r_\mathrm{max}$ is the maximum separation, which I believe is the initial separation in your particular simulation. The eccentricity $e$ (formula here) is given by $$ e^2 = 1 + \frac{2Er^4\dot{\theta}^2}{G^2M^2m}, $$ where $E$ is the orbital energy, $G$ is the gravitational constant, $M$ is the mass that is so heavy it essentially doesn't move, and $m$ is the lighter mass. This can more conveniently be rewritten $$ e^2 = 1 + 2 \left(\frac{rv}{GM}\right)^2 \left(\frac{v^2}{2} - \frac{GM}{r}\right), $$ where $v$ is the velocity of the moving particle.
Note that the convention is for $\theta$ to measure the angle from closest approach (i.e., $\theta = 0$ is the negative $y$-axis in your simulation). If you want $\theta$ to increase with time, then the (nonstandard) transformation to Cartesian coordinates is \begin{align} x & = -r \sin(\theta) \\ y & = -r \cos(\theta), \end{align} assuming $r = 0$ corresponds to the massive object. In any event, to plot the orbit all you need to do is calculate the constant $e$ at one point in the orbit, sample $\theta$ with as many points as you like, calculate the separations $r(\theta)$, and convert the $(r, \theta)$ pairs to $(x, y)$.
No comments:
Post a Comment