Due: Thursday, November 8th by 11:59 PM

In this assignment, you will write a program that simulates the motion of a planet orbiting around a star.

The basic approach will be very similar to the animation you created in Lab 22.

Download CS200_Assign5.zip.

If you have the Marmoset Add-in installed, you can import the zip file using File->Import....

If you do not have the Marmoset Add-in installed, create a new folder, extract the contents of the zip file into the folder, navigate into the folder using a file browser, and then double-click the solution file (CS200_Assign5.sln).

Add your code to the source file Planet.c.

Your task is to simulate a solar system with a star orbited by at least one planet. The star's position is fixed, and the planet orbits around the star. The motion of the planet at each step of the animation is based on two values, dx and dy (delta X and delta Y). At each step of the animation, dx is added to the planet's x coordinate and dy is added to the planet's y coordinate. In the absence of any force acting on the planet (i.e., if dx and dy stay constant), this causes the planet to move in a straight line with a constant velocity. (Bodies in motion tend to stay in motion.)

However, there is a force acting on the planet: the
star's gravity. At each step of the simulation, the current
velocity and direction of the planet (dx and dy) should be changed based
on the gravitational attraction of the planet towards the star.
The magnitude of the gravitational attraction, f_{grav},
is given by the following formula:

f_{grav}
= M / dist^{2}

where M
is a constant representing the mass of the star and the planet, and dist is the
geometric distance between the center of the star and the center of the
planet. The geometric distance between two points (x_{1},y_{1})
and (x_{2},y_{2}) is given by
the formula

dist = sqrt( (x_{2}
- x_{1})^{2} + (y_{2}
- y_{1})^{2} )

In order to determine how the force of gravitational
attraction affects the planet's motion (dx and dy), you will need
to determine the horizontal (f_{x}) and vertical (f_{y}) components of the
gravitational force:

The horizontal and vertical components of the gravitational
force, fx and fy, are proportional to dist_{x} and dist_{y}, the x and y
components of the distance between the star and the planet:

dist_{x}
= planet_{x} - star_{x}

dist_{y}
= planet_{y} - star_{y}

f_{x} = f_{grav} (|dist_{x}|
/ dist)

f_{y} = f_{grav} (|dist_{y}|
/ dist)

Note that |dist_{x}| means the absolute
value of dist_{x}, and |dist_{y}| means the absolute
value of dist_{y}.

So, to determine the change in the direction and velocity of
the planet at each step of the simulation, f_{x} is either added or
subtracted to dx,
and f_{y} is either added or
subtracted to dy.
You will know whether to add or subtract by considering that
gravity is an attractive force; you should change dx and dy in a way
that either decreases the planet's velocity away from the star, or
increases the planet's velocity towards the star.

Once you have computed the change in dx and dy based on the gravitational force, you can then change the x and y coordinates of the planet. When you run the program, you should see the planet orbit around the star.

You can run my solution to the assignment to get a sense of what yours should do:

My initial simulation parameters (Scene data structure) are as follows:

star_{x}
= get_screen_width() / 2

star_{y}
= get_screen_height() / 2

M = 4

planet_{x}
= star_{x}

planet_{y}
= star_{y} + 10

dx = .5

dy = 0

All of your simulation variables (fields in struct Scene) should be double variables. Don't do calculations using integer arithmetic. Do the calculations using floating-point (double) arithmetic, and then, in the render_scene function, convert the x and y coordinates of the planet and star to int column and row values using type casts, e.g.:

int star_x, star_y;

star_x = (int) scene.star_x;

star_y = (int) scene.star_y;

As your simulation runs, the x and y coordinates of the planet may sometimes be off screen. You should handle this in your render_scene function: if either x or y is less than 0, or if x >= get_screen_width(), or if y >= get_screen_height(), don't display the planet.

You may want to increase the size of the console window in which the program runs.

The following two function prototypes are defined in the <math.h> header file:

double sqrt(double value);

double fabs(double value);

The sqrt function returns the square root of a double value, and the fabs function returns the absolute value of a double value.

You can change the value of the ANIMATION_DELAY constant in order to speed up or slow down the simulation.

For extra credit, simulate multiple planets. You do not have to consider gravitational attraction between planets.

If you have the Marmoset Add-in installed, you can submitonline automatically by using the Tools->Submit... menu item. Just type in your Marmoset username and password and click Submit. After a few seconds, you should see a dialog box informing you whether or not the submission was uploaded successfully. If successful, the dialog will look like this:

If you do not have the Marmoset Add-in installed, or if the automatic online submission does not work, create a zip file containing your entire project and manually upload it to the Marmoset server. (See the submission instructions for details.)