1

I'm currently writing a program that attempts to simulate particle movement in a gravitational field with more than one object exerting a force on it.

I decided that I'd have the particle move by constantly assigning it with new velocity vectors. This is determined as follows:

  1. I loop through each object inside the scene that isn't the particle. I use the equation: A = GM/R^2 to determine the acceleration that the particle will feel with respect to the current object. Then I multiply the distance vector (difference X, difference Y) between the particle and the object by the Acceleration and add that to a list.

  2. I loop through the final list of all Acceleration Vectors, and add the X's together and the Y's together in order to determine the resultant acceleration vector will be on the particle.

  3. I take this resultant acceleration vector, and then set it as the velocity vector of the particle.

While I think I've gotten the resultant acceleration down okay, I keep finding myself unable to figure out how I would account for the particles current velocity.

I could add it as a vector to the list of acceleration vectors acting on the particle, but it doesn't behave properly.

For example, if I attempt to get a particle to orbit an object, I expect to use the equation:

V = sqrt(G*M/r) to get myself the appropriate velocity needed to orbit the object.

However, even with both the acceleration vector and velocity vector sharing the same magnitude, the particle behaves as follows: enter image description here

In Scene 1, R is the resultant vector of both V and A. This will now be set as the particles new velocity vector.

enter image description here

In Scene 2, you can see that R is now again the resultant between V and A, but the problem is that V is now incorrectly represented and thus the resultant will angle even more towards the surface of the object.

I quickly saw that this was a problem, I couldn't be setting velocity as resultant acceleration. But I can't think of how I would properly implement it either. The current velocity would have to be 'angled' 90 degrees to the acceleration vector, but I cannot envision how I could determine this in the infinite amount of other cases where it won't be a perfect 90 degrees.

I'm beginning to feel that I've made some major mistakes in the entire implementation of this model too, and if anyone could suggest how I could properly account for velocity that would be such a help.

Thanks again.

Kyle Kanos
  • 29,127
Micrified
  • 113

2 Answers2

2

You are trying to model a simple 2-body simulation (a special case of $n$-body simulations). Your basic set up should revolve around two equations: \begin{align} \frac{dx}{dt}&=v\\ \frac{dv}{dt}&=\frac{F}{m} \end{align} which is really breaking up Newton's 2nd law into two steps. Here $$ F=G\frac{mM}{r^2} $$ is the gravitational force and $a=F/m$ your acceleration.

In discrete steps, you should be evolving the position and velocity of the particles as \begin{align} x_{new} &= x_{old}+v_{old}\cdot dt\tag{1}\\ v_{new} &= v_{old}+\frac{F}{m}\cdot dt\tag{2} \end{align} where $x$ and $v$ are 2D vectors.

Your most simple algorithm is the Euler method, described in Equations (1) & (2). A more stable integration scheme, called Verlet integration, goes

  1. Compute the new position step: $$x_{n+1}=x_n+v_{n}dt+\frac12a(x_n)dt^2$$
  2. Compute new velocity: $$v_{n+1}=v_{n}+\frac12\left(a(x_{n+1})+a(x_n)\right)dt$$
  3. Set $x_n=x_{n+1}$ and $v_n=v_{n+1}$, increment time: $t=t+dt$ and repeat until $t=t_{end}$

And there are other integration methods available to you, but either of these two are simple enough to implement and should work for your purposes (provided $dt$ is small enough).

Kyle Kanos
  • 29,127
1

Just to be clear, this part

I take this resultant acceleration vector, and then set it as the velocity vector of the particle.

is where your simulation diverges from reality. It sounds like you know that. I've described how to fix your problem below - other than that, it sounds like you have a fine simulation design.

The solution to your problem can be found in the definition of acceleration.

Acceleration is the rate of change of velocity. In mathematical symbols,

$$ a = \frac{\Delta v}{\Delta t} $$

Where $\Delta v$ is the change in velocity for some small change in time $\Delta t$. We can expand this a little by writing out what $\Delta v$ means:

$$ a = \frac{v_1 - v_0}{\Delta t} $$

Since you want to find the velocity at the end of each time step in your simulation, let's solve for $v_1$:

$$ v_1 = v_0 + a \Delta t $$

This is your answer, but let's interpret it carefully. $v_1$ is the velocity at the end of each of your simulation's time steps. $v_0$ is the velocity at the end of the last time step, and at the beginning of the current time step. $\Delta t$ is the duration of your simulation's time step - this is something you have to set for your simulation - how long each discrete time step will be.

So, what this equation tells you is that to "update" the velocity (get the current velocity $v_1$), you take the previous velocity of your particle $v_0$ and add the amount $a \Delta t$.

Now, it sounds like you're doing this in two dimensions, so these are all really vectors:

$$ \vec{v}_1 = \vec{v_0} + \vec{a} \Delta t $$

which means we have two equations, one for each dimension:

$$ v_{1,x} = v_{0,x} + a_x \Delta t $$

and

$$ v_{1,y} = v_{0,y} + a_y \Delta t $$

Brionius
  • 7,605
  • 1
  • 33
  • 31