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:
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.
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.
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:
In Scene 1, R is the resultant vector of both V and A. This will now be set as the particles new velocity vector.
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.
Answer
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
- Compute the new position step: $$x_{n+1}=x_n+v_{n}dt+\frac12a(x_n)dt^2$$
- Compute new velocity: $$v_{n+1}=v_{n}+\frac12\left(a(x_{n+1})+a(x_n)\right)dt$$
- 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).
No comments:
Post a Comment