Hi!

I want to write a program, which calculates and draws the path of a planet around a sun. Actually it just draws the planet and the sun (and the velocity and the gravity force vectors). The sun is still, it doesn't move, just the planet is moving. The planet has got some velocity at the t=0 time point.

I use OpenGL, actually I changed the sample OpenGL program of Dev-C++.

The gravity force formula by Newton is as follows:
F = f*m*M/(r^2)
where f=6,67e-11
m is the weight of planet, M is the weight of sun
r is the distance between the centers,
assuming that the planet and the sun is a sphere, F is measured by Newton, m and M by kg and r by meter.

In my program the coordinate system is from -1 to 1 by both x- and y-axes, I don't know how to change it to a good range. So r is at maximum 3 (m).

My problem is, that with these distances, if I use the weight of planet and sun as it is in the nature (e24 and e30) and f (e-11), the gravity force is so big that the planet moves abnormally.
My question is, how should I change the datas to make the program work correctly. A good ratio may solve it, but I'm not sure.

I think you're going to have to use all of the 'nature' values and then scale from the nature values to your -1 to 1 grid.

If the farthest the planet gets from the sun is 152,000,000 km then you might scale to 160,000,000 km = 1.

You could continue to do all of the calculations in nature values and only scale to plot, or you could use the nature values once and scale the distance, speed and acceleration (forces).

I can't do all of the calculations in nature values, because the distance is calculated from the coordinates. But maybe my problem was that I scaled by that ratio you also mentioned, but not with it's square, just with first degree. I'll check it out.

Basically, there are two scales in the problem based on the initial conditions.

The first is that the planet and the start are going to orbit each other.
The second is that the planet is going to escape on a hyperbolic or parabolic orbit.

You can test the possiblity of either (a) or (b) by calculating the total energy required to separate the bodies to infinity, and the summing the kenetic energy. Obviously the P.E is

$$P.E= \int^\infty_d \frac{GmM}{r^2}$$

$$P.E= \frac{GmM}{d}$$

were d is the initial distance and I have replaced your F with the more common G.

Now you can get the ellicity of the orbit from energy considerations,
consider the reference frame with the heavier body motionless, then
$$\frac{v^2}{2}+\frac{GM}{r} =\frac{-(GM)^2}{2L^2}(1-e^2)$$
were L is the relative angular momentum and e is the ellipsicity and is the number you are after as it gives your scaling value. (using r as the distance)

Note this can be obtained using Newtons laws and a Lagrangian and is readily availiable on the web. (and is longer than I can write without making stupid typo.)

Now once you have the ellicity you know how to scale your problem since it gives you the max/min distance. I would choose the min and rescale, Note that if the ratio max/min is getting greater than 1e6 then you are going to need to do it in a log frame. [And use tidel forces/general relativity etc]

The first is that the planet and the start are going to orbit each other.
The second is that the planet is going to escape on a hyperbolic or parabolic orbit.

You can test the possiblity of either (a) or (b) by calculating the total energy required to separate the bodies to infinity, and the summing the kenetic energy. Obviously the P.E is

$$P.E= \int^\infty_d \frac{GmM}{r^2}$$

$$P.E= \frac{GmM}{d}$$

were d is the initial distance and I have replaced your F with the more common G.

I thought it was more like:

for ( if not roughly equivalent to r)
v = sqrt(2 * G * r);

That's just what I remember from an old ref. book. Truthfully, this is a tad over my head.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.