Guest User

Untitled

a guest
Jul 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 KB | None | 0 0
  1. #ifndef UNIV_CPP
  2. #define UNIV_CPP
  3. #include "Universe.h"
  4. #include <string>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <fstream>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <vector>
  11. #include <freeglut.h>
  12. #include <cstdlib>
  13. #include "Body.h"
  14. #include "Vec.h"
  15.  
  16.  
  17. using namespace std;
  18.  
  19.  
  20. void Universe :: increaseTime(double dt){
  21.  
  22.     vector<Vec> f;
  23.    
  24.     //Populate a vector the size of N, with Vec objects initialized to 0
  25.     for(int i=0; i<N; i++){
  26.         Vec newOne;
  27.         f.push_back(newOne);
  28.     }
  29.  
  30.     //Nested for/loops the size of N; whcih sum up the forces from all orbs on 1.
  31.     for(int i=0; i<N; i++){
  32.         Vec toAdd = f.at(i); //Create a new Vec object from the new vector<Vec>
  33.         for(int j=0; j<N; j++){
  34.             Vec currForce; //Create a new Vec for each force
  35.             if(i != j){
  36.                 currForce = bodies.at(j).forceTo(bodies.at(i)); //Calculate the force
  37.                 //currForce = toAdd+(currForce); //
  38.                 toAdd.x += currForce.x;
  39.                 toAdd.y += currForce.y;
  40.             }
  41.         }
  42.        
  43.         f.at(i) = toAdd;
  44.     }
  45.     //Call the move method for each body with the calculated summed forces.
  46.     for(int i=0; i<N; i++){
  47.         bodies.at(i).move(f.at(i), dt);
  48.     }
  49.  
  50. }
  51.  
  52. void Universe :: draw(){
  53.    
  54.     for(int k=0; k<bodies.size(); ++k){
  55.  
  56.         glPushMatrix();
  57.         glTranslatef(bodies[k].pos.x, bodies[k].pos.y, bodies[k].pos.z);
  58.         glutSolidSphere(radius,20,20);
  59.         glPopMatrix();
  60.     }
  61. }
  62.  
  63. void Universe::read(char file[]){
  64.    
  65.    
  66.     string line; //To store current line
  67.     ifstream inFile;
  68.     inFile.open("lagrange.txt"); //Open file stream
  69.    
  70.     vector<string> content;
  71.    
  72.     if(!inFile){ //Check for failure
  73.         cerr << "error opening file" << endl;
  74.         exit(1);
  75.     }
  76.     //While there is data..write to the vector
  77.     while(inFile >> line){
  78.         content.push_back(line);
  79.     }
  80.     for(int i=0; i<content.size(); i++){
  81.  
  82.         printf("%s\n",content.at(i));
  83.  
  84.     }
  85.    
  86.  
  87.     //Populate the body objects
  88.     /*
  89.     2
  90.     5.0e10
  91.     0.0e00 4.5e10 1.0e4 0.0e00 1.5e30
  92.     0.0e00 -4.5e10 -1.0e4 0.0e00 1.5e30
  93.     */
  94.     /*
  95.     radius = 5.0e10;
  96.     N = 2;
  97.     Vec posOne(0.0e00,4.5e10);
  98.     //posOne.x = 0.0e00;
  99.     //posOne.y = 4.5e10;
  100.     Vec velOne(1.0e4,0.0e00);
  101.     //velOne.x = 1.0e4;
  102.     //velOne.y = 0.0e00;
  103.     double massOne = 1.5e30;
  104.  
  105.     Vec posTwo(0.0e00,-4.5e10);
  106.     //posTwo.x = 0.0e00;
  107.     //posTwo.y = -4.5e10;
  108.     Vec velTwo(-1.0e4,0.0e00);
  109.     //velTwo.x = -1.0e4;
  110.     //velTwo.y = 0.0e00;
  111.     double massTwo = 1.5e30;
  112.     */
  113.     /*
  114.     3
  115.     1.25e11
  116.     0.0e10 0.0e00 0.05e04 0.0e03 5.97e24
  117.     0.0e00 4.5e10 3.0e04 0.0e00 1.989e30
  118.     0.0e00 -4.5e10 -3.0e04 0.0e00 1.989e30
  119.     */
  120.     /*
  121.     radius = 1.25e11;
  122.     N = 3;
  123.     Vec posOne(0.0e10,0.0e00);
  124.     //posOne.x = 0.0e00;
  125.     //posOne.y = 4.5e10;
  126.     Vec velOne(0.05e04,0.0e03);
  127.     //velOne.x = 1.0e4;
  128.     //velOne.y = 0.0e00;
  129.     double massOne = 5.97e24;
  130.  
  131.     Vec posTwo(0.0e00,4.5e10);
  132.     //posTwo.x = 0.0e00;
  133.     //posTwo.y = -4.5e10;
  134.     Vec velTwo(3.0e04,0.0e00);
  135.     //velTwo.x = -1.0e4;
  136.     //velTwo.y = 0.0e00;
  137.     double massTwo = 1.989e30;
  138.  
  139.     Vec posThree(0.0e00,-4.5e10);
  140.     //posTwo.x = 0.0e00;
  141.     //posTwo.y = -4.5e10;
  142.     Vec velThree(-3.0e04,0.0e00);
  143.     //velTwo.x = -1.0e4;
  144.     //velTwo.y = 0.0e00;
  145.     double massThree = 1.989e30;
  146.     */
  147.     /*
  148.     3
  149.     .1e1
  150.      0. 01. 0..8 1.
  151.     -0.5 0.866025403784439 -0.692820323027551 -0.4 1.
  152.     -0.5 -0.866025403784439 0.692820323027551 -0.4 1.
  153.     */
  154.    
  155.     radius = .1e1;
  156.     N = 3;
  157.     Vec posOne(1.,0.);
  158.     //posOne.x = 0.0e00;
  159.     //posOne.y = 4.5e10;
  160.     Vec velOne(0.,0.8);
  161.     //velOne.x = 1.0e4;
  162.     //velOne.y = 0.0e00;
  163.     double massOne = 1.;
  164.  
  165.     Vec posTwo(-0.5, 0.866025403784439);
  166.     //posTwo.x = 0.0e00;
  167.     //posTwo.y = -4.5e10;
  168.     Vec velTwo(-0.692820323027551, -0.4);
  169.     //velTwo.x = -1.0e4;
  170.     //velTwo.y = 0.0e00;
  171.     double massTwo = 1.;
  172.  
  173.     Vec posThree(-0.5, -0.866025403784439);
  174.     //posTwo.x = 0.0e00;
  175.     //posTwo.y = -4.5e10;
  176.     Vec velThree(0.692820323027551, -0.4);
  177.     //velTwo.x = -1.0e4;
  178.     //velTwo.y = 0.0e00;
  179.     double massThree = 1.;
  180.    
  181.  
  182.     Body bodyOne(posOne, velOne, massOne);
  183.     Body bodyTwo(posTwo, velTwo, massTwo);
  184.     Body bodyThree(posThree, velThree, massThree);
  185.    
  186.     bodies.push_back(bodyOne);
  187.     bodies.push_back(bodyTwo);
  188.     bodies.push_back(bodyThree);
  189. }
  190. #endif
Add Comment
Please, Sign In to add comment