Need help programming an Orbital Simulator

Click For Summary

Discussion Overview

The discussion revolves around programming an orbital simulator in VB.Net 2008, focusing on simulating the motion of a single planet orbiting a star. Participants explore the necessary calculations for gravitational force, acceleration, velocity, and position updates, as well as programming structure and methods.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant outlines a basic approach involving the mass of the star, distance, and initial velocity to calculate gravitational force and subsequent motion.
  • Another participant suggests using a small time step (deltaT) to simplify calculations and avoid complex integrals, sharing their experience with similar programming challenges.
  • A different participant warns about accumulating errors in simple integration methods and suggests exploring more advanced techniques like leapfrog and Verlet integration for better accuracy.
  • One participant describes their own experience with an Earth-Sun simulation, detailing their steps and calculations, while expressing uncertainty about the correctness of their implementation.
  • Another participant expresses gratitude for the shared advice and indicates an intention to research leapfrog integration further.
  • A participant references another thread that may provide additional insights into related methods for orbital simulations.

Areas of Agreement / Disagreement

Participants generally agree on the basic principles of orbital mechanics and programming structure, but there are differing opinions on the best integration methods and the potential issues with simple approaches. The discussion remains unresolved regarding the optimal implementation strategies.

Contextual Notes

Participants express uncertainty about specific implementation details, such as the order of calculations and the effects of initial conditions on simulation outcomes. There is also a mention of potential errors accumulating over time in simpler methods.

Big Big Ron
Messages
2
Reaction score
0

Homework Statement


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!:)

Homework 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.
 
Physics news on Phys.org
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 don't 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 Earth's 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!
 
@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
 
Big Big Ron said:
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!:)

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:
  • Like
Likes   Reactions: berkeman
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!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 15 ·
Replies
15
Views
7K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 57 ·
2
Replies
57
Views
10K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 22 ·
Replies
22
Views
1K