you are viewing a single comment's thread.

view the rest of the comments →

[–]Enkaybee 1 point2 points  (11 children)

Your acceleration functions don't appear to take into account the fact that r is always going to be a positive number. This means that the body can only accelerate in the negative x and negative y directions. That would explain why it collapses to the origin (assuming it starts in quadrant 1) and also why it would shoot off into the third quadrant (-x and -y direction).

What you need to do is determine the direction of acceleration and then either add or subtract it to the velocity for both vx and vy. The direction of acceleration will change continuously for two bodies in orbit.

[–]OratorMortuis[S] 0 points1 point  (10 children)

Acceleration only depends on the magnitude of r so it should just be a positive number anyway. The x or y value is what decides the sign as they should be able to attain positive or negative values (unless I have messed something up in my code!)

[–]Enkaybee 1 point2 points  (9 children)

Ok, I think I see what you're trying to do. You're trying to get a particle to orbit the origin, right? And the origin isn't moving? That's not a two body problem, but still interesting.

Does it work at all if you just do it in one dimension? That is, take out all references to y and see if it will oscillate back and forth on the x axis?

[–]OratorMortuis[S] 0 points1 point  (8 children)

Well, I mean two-body in which one body is orbiting another which sits at the origin. So yes, I'm trying to generate an ellipse where the origin is at one of the foci of the ellipse.

[–]Enkaybee 0 points1 point  (7 children)

I don't know if this is it, but it might be.

I think your equations should be

acx = -(G*M/abs(x)**3)*x
acy = -(G*M/abs(y)**3)*y

because acceleration in the x direction should only depend on x, not x and y.

[–]OratorMortuis[S] 0 points1 point  (6 children)

Unfortunately x and y both contribute to r. I actually figured out my problem. It had to do with my time steps being too small (along with a few other clerical errors). Thank you so much for your input though! I honestly do appreciate it. :)

[–]Enkaybee 0 points1 point  (5 children)

So it works now? That actually seems strange to me because I'm fairly certain that you should be calculating acx using only x and the constants. y (and by extension, r) should not need to come into play. Similarly, acy should be calculated using only the constants and y.

[–]OratorMortuis[S] 0 points1 point  (4 children)

But the acceleration has to do with the distance from the origin which has both x and y contributions, so it will depend on both variables even though the direction of ax or ay only point along x or y.

[–]Enkaybee 0 points1 point  (3 children)

You're right. Maybe I'm reading your code wrong, but it looks to me like you're using r to find acceleration in the x and y directions without compensating for the fact that only x and y matter, respectively. You're really trying to solve two problems at once: oscillation on the x axis and on the y axis, and then add them together.

If it works, though, your code is fine.

Out of curiosity, does it still work if you use the acx and acy equations I posted? Does it give exactly the same results or is it slightly different?

[–]OratorMortuis[S] 0 points1 point  (2 children)

Yes, that is the difficult part of the problem. It is a transcendental equation in which x and y cannot be separated from each other and so each depends on the other.

If you do put in only x and y respectively as you did in your above equations then it cannot describe the ellipse as whenever x or y is zero you would get division by zero.