3

To better understand how vectors work, I'm trying to create a very simple simulation of the earth orbiting the sun. Currently, all I want is for the earth to go around the sun in a circle. No laws of physics are taken into account.

I thought that what I was doing would work, however it creates some kind of a movement into a fibonacci spiral.

// initial positions
vec2 sun(0.0,0.0);
vec2 earth(10.0,0.0);

while(true) {

    vec2 earthToSun = normalize(sun - earth); // this is the vector 'pointing from the earth towards the sun'
    vec2 speedVector = vec2(-earthToSun.y, earthToSun.x); // this is the vector perpendicular to the earthToSun vector

    earth = earth + earthToSun + speedVector; // move the earth along the resulting vector

}

What is wrong in my calculations?

JustSid
  • 24,711
  • 7
  • 72
  • 97
RaptorDotCpp
  • 1,375
  • 12
  • 25
  • I knew that tag couldn't possibly be for what you used it for when I saw it. – chris May 25 '13 at 21:15
  • try using 0.0, 10.0 and so on instead of integers – HAL9000 May 25 '13 at 21:17
  • I removed the orbit tag and tested with floats, still the same. – RaptorDotCpp May 25 '13 at 21:18
  • @RaptorDotCpp, Technically, those are doubles. It shouldn't matter, though. – chris May 25 '13 at 21:19
  • Ugh. I meant doubles but I keep mixing up the words (since a double is just a double precision floating point) – RaptorDotCpp May 25 '13 at 21:20
  • `earth = earth + earthToSun + speedVector;` moves the earth by 1 towards the sun and by 1 in a perpendicular direction. This is not a circle orbit. – nullptr May 25 '13 at 21:27
  • @Inspired I see. I figured that is how the orbiting in our solar system works? The planet has a certain velocity-vector, but the gravity makes it go in an ellipse. Multiplying the speedVector with a certain scalar factor makes the earth turn around the sun for a longer period of time. Makes me wonder if the earth is ever going to 'hit' the sun. – RaptorDotCpp May 25 '13 at 21:30
  • Well, given a quantum of time `t`, and given `sun` position, `earth` position and `earthVelocity`, you can approximately calculate the new earth's position and velocity via: `acceleration = K*normalize(sun-earth); earth = earth + earthVelocity*t + acceleration*t*t/2.0; earthVelocity = earthVelocity + t*acceleration;` where `K` is some constant. But the earth will rather quick hit the sun (or flow away) anyway because of discrete nature of the simulation. – nullptr May 25 '13 at 21:35
  • See my edited answer. I suspect your `normalize` does not divide by the squared distance. The acceleration drops squared with distance, not linearly. – s.bandara May 25 '13 at 21:35

2 Answers2

3

Why not just do it with trigonometry:

#define PI 3.1415926
float r = 10.0;
for (int theta=0; theta<2*PI; theta += 0.01)
{
    float x = r*cos(theta),
          y = r*sin(theta);
    earth = vec2(x, y);
}

Obviously, alter the period, starting value of theta, increment, etc. as you see fit.

Matt Phillips
  • 8,691
  • 8
  • 41
  • 72
  • I have already created the simulation this way, but my goal is to eventually base it all on the laws of Newton and Kepler, and create a more realistic simulation. And I need vectors for that. I also want to get used to vector math. – RaptorDotCpp May 25 '13 at 21:23
  • @RaptorDotCpp Well, [those laws](http://en.wikipedia.org/wiki/Kepler's_laws_of_planetary_motion) analyze planetary motion in terms of angle/angular velocity. Trying to add a vector is probably the wrong approach since it necessarily moves earth on a tangent off the orbit. You only get a proper orbit when your step -> 0. A trigonometric approach might actually end up being more easy extend to encompass the actual laws. – Matt Phillips May 25 '13 at 21:35
1

Your initial conditions should be

vec2 sun(0.0,0.0);
vec2 earth(10.0,0.0);
vec2 speedVector = vec2(-earthToSun.y, earthToSun.x);

That looks good. However, there are two problems with your equations.

  1. The position of your earth should change over time like this:

    vec2 earthToSun = normalize(sun - earth);
    earth = earth + earthToSun;  // no speedVector added here
    

    Note that I did not add speedVector to earth in the update code. What you did there is accelerate your earth throughout your simulation.

  2. Your normalize function will have to normalize by the squared distance. I am not sure how you implemented that. F = g m1 m2 / r ^ 2. I suspect that your normalize only devides by r, not r ^ 2. Check https://en.wikipedia.org/wiki/Gravitational_constant for reference

Your earth will not necessarily go in a circle. Most likely it will be an elliptical orbit. Also make sure to choose a small enough step size. Per iteration, earth should change only by a small fraction of its distance to the sun, or otherwise you will accumulate obvious integration errors.

s.bandara
  • 5,553
  • 1
  • 19
  • 36