- #1
Who Am I
- 89
- 2
Homework Statement
I'm trying to write a program with three bodies, two in orbit around a large central object. The main object orbits fine, that was dealt with earlier. I'm having trouble making a smaller object that is the only object whose position is dependent on the position of both of the other two masses.
I'm writing this in C, though I think anyone with some programming experience in physics should be able to give me a hand.
It's only being done in two dimensions.
This also might be more of a basic physics question than a programming question.
Homework Equations
F=Gm2m1/r2
The Attempt at a Solution
So, this is what I have inside my loop.
Everything in blue works. Orange is questionable, red is most likely the source of the error.
Code:
for(i=0;i<500000;i++) {
putpoint_plot(x,y,10,1,3,0.6,1);
putpoint_plot(x2,y2,10,2,4,0.3,1);
delay_plot(1);
flush_plot();
// distance magnitudes
r=sqrt(x*x+y*y); //distance of large object to central object
r1=sqrt(x2*x2+y2*y2); // distance of tiny object to central object
r2=sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y)); //distance of tiny object to large object
//acceleration components
[COLOR="Blue"] a=-1.0/r/r;[/COLOR]
[COLOR="Blue"] ax=a*x/r; // Components ...
ay=a*y/r; // of the acceleration[/COLOR]
[COLOR="orange"] a1=-1.0/r1/r1; //acceleration of small object towards the massive central object
a2=-0.5/r2/r2; //Acceleration of tiny object towards the large object
[/COLOR]
[COLOR="red"]ax2=a1*x2/r1+a2*(x-x2)/r2;
ay2=a1*y2/r1+a2*(y-y2)/r2;
[/COLOR]
[COLOR="Gray"]//changing velocity components
dvx=ax*dt; //Change in velocity ...
dvy=ay*dt; // over one time step
dvx2=ax2*dt;
dvy2=ay2*dt;
vx+=dvx; // New velocty
vy+=dvy;
vx2+=dvx2;
vy2+=dvy2;
[/COLOR]
Now, my difficulty is that I don't really know how to break up the acceleration into the x and y components for the second object using the method that is used above. As I stated, the acceleration of the main object works fine, but I don't quite what math was used to get there as I used this from an example.
So, the code in red is the part where I am breaking the two magnitudes of acceleration up into x and y components. The second portion is supposed to be the acceleration towards the moving object, which is really what's giving me trouble.
I'd definitely appreciate some help on this one. It's kind of tricky.
Last edited: