1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help programming an Orbital Simulator

  1. Jun 16, 2015 #1
    1. The problem statement, all variables and given/known data
    Hey, so basically I'm doing Advanced Higher Computing Science at school (It's a Scottish qualification), and I need to build an orbital simulator in VB.Net 2008 (nothing too fancy), but I'm having issues planning it all out. I'm only going to have one planet orbiting a star, so I figured I would need to enter the mass of the star, the distance from the planet to the star, and also the initial velocity of the planet. From there I could go on to calculate the gravitational force acting on the planet, using F = G*M1*M2/r^2 , then find the acceleration of the planet using a = f/m. After that I can use v = u + at to calculate the new horizontal and vertical velocities of the planet, then use them to find the new position of the planet, and then finally use the pen to draw it out. Is this approach any good? Some help of the structure of the program would be hugely appreciated, I'm not asking for the exact code, just some indication of the different methods and procedures I'll need to make it work. Thanks!:)

    2. Relevant equations
    • Given that I know the new velocities, how would I find the new position of the planet? Could I use this equation : x2 = x1 + (Vx * t) I read it somewhere that this would be the way to do it, however I am unsure if it is correct.

    3. Part 1 is kinda my attempt I guess.
     
  2. jcsd
  3. Jun 16, 2015 #2
    I helped a friend program something similar for a physics course he took.

    The way we approached it was quite similar. Single planet, single star (although it wouldn't be hard to scale this up to do multiples). Then we just used the basic physics formulas like you listed. The key thing to make your life easier is pick a deltaT to be your smallest time step. Then you dont have to get into processing integrals. I think we used an hour, but play around with it.

    After we got it all programed, we needed data to try it out on. So we used the mass of the earth and sun. Then the earths avg distance from the sun and its average orbital velocity as starting points, and..... it didn't work. The planet kept flying off into the void. After a hour or so of trying to track down the issue, we finally clued in that the average distance and velocity didn't occur at the same time so our starting point was invalid. I guess my point is when you get something programmed and it doesn't work in this case it could be your code, or it could also be your starting data :)


    To get started plan out what data you'll need to keep for the orbiting objects, and how you'll store that info in the program.
    Then how you'll go about actually calculating each step. 5min of planning can save your hours of coding

    Let us know how it goes!
     
  4. Jun 16, 2015 #3

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    @Big Big Ron: That simple approach, used in a loop, will give some reasonable results for small time steps, but it suffers from the problem cpscdave mentioned: you accumulate errors in each time step. To make it worse, all errors go in the same direction, probably letting the planet leave the system over time. Try this first, once you get it running you can look for better approaches.

    The method to calculate positions based on known accelerations or velocities is called integration scheme. The next-to-easiest algorithm is the leapfrog integration where you calculate velocities and positions at different steps. Verlet integration is even better. See also this overview article
     
  5. Jun 16, 2015 #4

    Drakkith

    User Avatar

    Staff: Mentor

    Hi Ron,

    I actually just made a simulation of the Earth orbiting the Sun in another program this past Saturday. It works. Sort of. Earth likes to spiral into the Sun after a few orbits before being shot out into intergalactic space, so I'm sure I messed up somewhere. I used:

    A=F/M
    V = Vo +At
    X = Xo +Vt + 1/2At2
    F = GM1M2/r2

    Xo and Vo are "X-naught" and "V-naught" and represent the initial position and velocity at each time step.

    I had to break the first 3 equations into into X and Y components using some trigonometry and then calculate each component. Steps in my program:
    1. I first calculate the total force using the 4th equation.
    2. I break the force into X and Y components based on the position of the planet relative to the star. (Tricky if you're not used to trigonometry like me)
    3. Using each force component I find the acceleration in each axis.
    4. Using the acceleration I find the change in velocity in each axis and then add the value to a variable called "X-Velocity" and "Y-Velocity" (See note in next step)
    5. Using the acceleration and new velocity I calculate the change in position in each axis and add this change to variables "X-Position" and "Y-Position". (Basically I set Xo to zero, solve for X, which gives me a change in position in that axis, and then add this change to the variables. The same goes for the velocity in the previous step. I leave Vo as zero and solve for V using the acceleration)
    6. Repeat.

    I might have some of these steps in the wrong order, or be using the old/new values when I'm supposed to be using the other. I'm not sure yet. Hope this helps you get started.
     
    Last edited: Jun 16, 2015
  6. Jun 16, 2015 #5
    Wow! Thanks for great the help guys!:) I shall read about leapfrog integration tomorrow during school! These tips will definitely get me going in the right direction!
     
  7. Jun 16, 2015 #6

    Drakkith

    User Avatar

    Staff: Mentor

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Need help programming an Orbital Simulator
  1. C program help needed (Replies: 3)

Loading...