Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with an orbit sim

  1. Feb 24, 2005 #1
    I’ve been trying to make a multiple body 2D orbit simulator off and on for a few years now. Just as sort of a hobby, nothing too serious (ie, doesn’t have to model what would happen in a reality exactly but the body motions should be more or less correct)

    Well anyway, I’m not a programmer by any sense of the word. The only language I am somewhat okay at is Qbasic.

    I recently started work on a simulator again using a more realistic approach. My previous attempts all looked very nice and were simple enough as to allow up to a thousand bodies to be smoothly calculated and displayed. Because of the calculations required in my latest attempt, only about 40 interacting objects at a time can be smoothly done.

    Anyway, things are looking pretty nice except I have one major nagging problem:
    When testing on 2 bodies (one mass orbiting another much heavier mass) there seems to be an anomalous motion to the system. Now, I expect the centre, heavy mass to wobble as the lighter mass orbits it, however, the wobble is not complete as I would expect. Please see diagram:
    (Note, because I’m running the program in DOS, I can’t take a screen shot)

    The diagram shows 2 complete orbits of m1 about m2. You’ll notice the wobble of m2 is not a complete circle/oval as is expected. This is my problem, I don’t know why. You see that the system also moves to the right for some reason. In this example, m1 is given an initial velocity in the horizontal direction to the right, but m2 is not. Yet for some reason, m2 moves continually rightward. The graphs at the side of the diagram approximates the x and y component velocities of both m1 and m2 during one orbit. You’ll notice that m1 looks just about as it should while m2 never has a negative x velocity. I have no idea why. I’ve tried changing the order in which the forces are calculated, the initial position of m1 with respect to m2 and I still get the same strange incomplete wobble. If I start m1 to the left of m2 and give it an initial vertical velocity, then the wobble of m2 is incomplete, but in the y direction and the motion of the system is now in the y direction.

    The way I program is really gross and incoherent, but that’s just my style. The program is attached if anyone can make sense of it. I’ve checked over the calculations and they all seem to be fine. The problem is always with the greater mass.

    *Actually while I was writing this, I realized that the problem may be that the forces are added twice (and thus changing velocities) during a loop. I fixed that and now the lighter masses don’t spiral into the heavier ones, but the weird motion of the system still remains.

    Attached Files:

  2. jcsd
  3. Feb 24, 2005 #2


    User Avatar
    Science Advisor
    Gold Member

    Here's what's happening.

    Even though you don't give M2 any initial velocity, the velocity you give to M2 is with respect to M1, not the system barycenter. As you guessed, M2 will not sit still. So it is not the stationary point in your system. That's why it drifts. M2 needs to have a little bit of of leftward motion to counter M1's rightward motion. So that the combined motions on the x axis = 0. But it must be a weighted calculation unless your masses are the same. For example, if M1 has a velocity of 4 km/s in the x direction relative to M2, and M1 is half the mass of M2, then you should give m1 an initial velocity 2.667 km/s to the right, and M2 an initial velocity of 1.333 km/s to the left. These velocities still add to 4, so m1 will still be moving 4 km/s relative to 2. But now the speeds are relative to the system barycenter, which does not move.

    I encountered this problem in Gravity Simulator, a program I wrote in Visual Basic and C++ that is similar to what you're trying to do. ( www.gravitysimulator.com )

    By the way, it's not much more work to add the z axis to your computations. If you do this, and you use real units and numbers instead of ones you make up, your system should be quite accurate. ie: Use 6.672e-11 for G, 5.98e24 kg for Earth's mass, express your distances in meters, and your velocity in m/s, and you'll notice that the period of an object at the moon's orbit behaves just like the real moon. Low earth satellites too.
  4. Feb 24, 2005 #3

    Oh my! Thank you very much. It all makes sense now :biggrin:
    Yeah, I was going to add realistic units a bit later on...maybe I'll add a z-axis. But thanks a lot!


    Just checked out your program. It's great!
    Last edited: Feb 25, 2005
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook