N-Body Simulation loading universe files; body class; graphics.
We'll be working through the Princeton assignment at http://www.cs.princeton.edu/courses/archive/fall13/cos126/assignments/nbody.html.
More details will be provided as their material assumes Java and we're using C++.
For this part of the assignment, Part A, we will create a program that loads and displays a static universe. In Part B, we will add the physics simulation, and animate the display!
Here are the particular assignment requirements for us:
- Make sure to download the universe specification files and image files from Princeton: ftp://ftp.cs.princeton.edu/pub/cs126/nbody.zip
- You should build a command-line app which reads the universe file (e.g.,
stdin. Name your executable
NBody, so you would run it with e.g:
NBody < planets.txt
< planets.txtconstruct is known as an "input redirect".
planets.txtuniverse file contains the Sun and the first four planets, with the Sun at the center of universe (x=0, y=0) and the four planets in order toward the right (per below). When this is working, you should be rewarded with:
- You must implement a class representing the celestial bodies. We suggest naming the class
Body. The class should have the following features:
- It must be
sf::Drawablewith a private virtual void method named
- Each instance of the class should contain all properties needed for the simulation; e.g.: x and y position, x and y velocity, mass, and image data.
- It probably should contain an
sf::Spriteobject (as well as the
sf::Textureobject needed to hold the sprite's image).
- For full credit, you should override the input stream operator
>>, and use it to load parameter data into an object.
- It must be
- Please see the grading rubric for all the details and pieces of the project.
- Please submit all files needed to build your project:
.cpp, any header files, and a
- Please submit the
planet.txtfile, and the specific GIF images associated with it.
- Please submit a screenshot of your running code, named
- Fill out and include this
ps3a-readme.txtfile with your work.
There are a lot of parts to this assignments. We'd suggest the following incremental development process:
- Create a bare-bones implementation of your
Bodyclass that has a constructor where you specify all the initial parameters (x,y position and velocity; mass; image filename).
- Have the constructor load the image into a new Texture object; create a new Sprite with that Texture.
- Given the initial x,y position in the universe, figure out the corresponding pixel-position for display in an SFML window. Hint: your class will need to know and store the universe radius, and display window dimensions. Hint 2: the universe's center is (0,0), and SFML's (0,0) point is in the upper-left.
- Implement the
drawmethod in your
- Write a main file that manually creates a
Bodyobject, by copying initialization parameters from the
planets.txtfile into your source code.
- Have the main file draw that object in the SFML display loop.
At this point, you should be able to display one planet (or the Sun). You can add one or two more manually, to know you're on the right track.
At this point, the way forward is to:
- Implement a vector of
Bodyobjects. For subtle reasons having to do with the default copy constructor, you'll fare better making a vector of pointers to
Bodyobjects, and instantiating them with
- When you have the vector working, you can write the code to overload the stream input operator
>>, and read in the universe file to set up your bodies. (See http://www.tutorialspoint.com/cplusplus/input_output_operators_overloading.htm for example code.)
Submit your work via Bottlenose:
The executable file that your
Makefile builds should be called
NBody (the grading script checks that this executable builds successfully.)
|core implementation||8||full & correct implementation|
|1 pt celestial body object is |
|1 pt implementation loads universe from |
|1 pt body class has |
|1 pt supports arbitrary number of body objects (per universe file)|
|1 pt scaling works for arbitrary universe size and given SFML window size declared in |
|must have dependencies correct|
|4||must explain how each of the features noted above is implemented to receive credit for those features!|