Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef UNIV_CPP
- #define UNIV_CPP
- #include "Universe.h"
- #include <string>
- #include <fstream>
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <vector>
- #include <freeglut.h>
- #include <cstdlib>
- #include "Body.h"
- #include "Vec.h"
- using namespace std;
- void Universe :: increaseTime(double dt){
- vector<Vec> f;
- //Populate a vector the size of N, with Vec objects initialized to 0
- for(int i=0; i<N; i++){
- Vec newOne;
- f.push_back(newOne);
- }
- //Nested for/loops the size of N; whcih sum up the forces from all orbs on 1.
- for(int i=0; i<N; i++){
- Vec toAdd = f.at(i); //Create a new Vec object from the new vector<Vec>
- for(int j=0; j<N; j++){
- Vec currForce; //Create a new Vec for each force
- if(i != j){
- currForce = bodies.at(j).forceTo(bodies.at(i)); //Calculate the force
- //currForce = toAdd+(currForce); //
- toAdd.x += currForce.x;
- toAdd.y += currForce.y;
- }
- }
- f.at(i) = toAdd;
- }
- //Call the move method for each body with the calculated summed forces.
- for(int i=0; i<N; i++){
- bodies.at(i).move(f.at(i), dt);
- }
- }
- void Universe :: draw(){
- for(int k=0; k<bodies.size(); ++k){
- glPushMatrix();
- glTranslatef(bodies[k].pos.x, bodies[k].pos.y, bodies[k].pos.z);
- glutSolidSphere(radius,20,20);
- glPopMatrix();
- }
- }
- void Universe::read(char file[]){
- string line; //To store current line
- ifstream inFile;
- inFile.open("lagrange.txt"); //Open file stream
- vector<string> content;
- if(!inFile){ //Check for failure
- cerr << "error opening file" << endl;
- exit(1);
- }
- //While there is data..write to the vector
- while(inFile >> line){
- content.push_back(line);
- }
- for(int i=0; i<content.size(); i++){
- printf("%s\n",content.at(i));
- }
- //Populate the body objects
- /*
- 2
- 5.0e10
- 0.0e00 4.5e10 1.0e4 0.0e00 1.5e30
- 0.0e00 -4.5e10 -1.0e4 0.0e00 1.5e30
- */
- /*
- radius = 5.0e10;
- N = 2;
- Vec posOne(0.0e00,4.5e10);
- //posOne.x = 0.0e00;
- //posOne.y = 4.5e10;
- Vec velOne(1.0e4,0.0e00);
- //velOne.x = 1.0e4;
- //velOne.y = 0.0e00;
- double massOne = 1.5e30;
- Vec posTwo(0.0e00,-4.5e10);
- //posTwo.x = 0.0e00;
- //posTwo.y = -4.5e10;
- Vec velTwo(-1.0e4,0.0e00);
- //velTwo.x = -1.0e4;
- //velTwo.y = 0.0e00;
- double massTwo = 1.5e30;
- */
- /*
- 3
- 1.25e11
- 0.0e10 0.0e00 0.05e04 0.0e03 5.97e24
- 0.0e00 4.5e10 3.0e04 0.0e00 1.989e30
- 0.0e00 -4.5e10 -3.0e04 0.0e00 1.989e30
- */
- /*
- radius = 1.25e11;
- N = 3;
- Vec posOne(0.0e10,0.0e00);
- //posOne.x = 0.0e00;
- //posOne.y = 4.5e10;
- Vec velOne(0.05e04,0.0e03);
- //velOne.x = 1.0e4;
- //velOne.y = 0.0e00;
- double massOne = 5.97e24;
- Vec posTwo(0.0e00,4.5e10);
- //posTwo.x = 0.0e00;
- //posTwo.y = -4.5e10;
- Vec velTwo(3.0e04,0.0e00);
- //velTwo.x = -1.0e4;
- //velTwo.y = 0.0e00;
- double massTwo = 1.989e30;
- Vec posThree(0.0e00,-4.5e10);
- //posTwo.x = 0.0e00;
- //posTwo.y = -4.5e10;
- Vec velThree(-3.0e04,0.0e00);
- //velTwo.x = -1.0e4;
- //velTwo.y = 0.0e00;
- double massThree = 1.989e30;
- */
- /*
- 3
- .1e1
- 0. 01. 0..8 1.
- -0.5 0.866025403784439 -0.692820323027551 -0.4 1.
- -0.5 -0.866025403784439 0.692820323027551 -0.4 1.
- */
- radius = .1e1;
- N = 3;
- Vec posOne(1.,0.);
- //posOne.x = 0.0e00;
- //posOne.y = 4.5e10;
- Vec velOne(0.,0.8);
- //velOne.x = 1.0e4;
- //velOne.y = 0.0e00;
- double massOne = 1.;
- Vec posTwo(-0.5, 0.866025403784439);
- //posTwo.x = 0.0e00;
- //posTwo.y = -4.5e10;
- Vec velTwo(-0.692820323027551, -0.4);
- //velTwo.x = -1.0e4;
- //velTwo.y = 0.0e00;
- double massTwo = 1.;
- Vec posThree(-0.5, -0.866025403784439);
- //posTwo.x = 0.0e00;
- //posTwo.y = -4.5e10;
- Vec velThree(0.692820323027551, -0.4);
- //velTwo.x = -1.0e4;
- //velTwo.y = 0.0e00;
- double massThree = 1.;
- Body bodyOne(posOne, velOne, massOne);
- Body bodyTwo(posTwo, velTwo, massTwo);
- Body bodyThree(posThree, velThree, massThree);
- bodies.push_back(bodyOne);
- bodies.push_back(bodyTwo);
- bodies.push_back(bodyThree);
- }
- #endif
Add Comment
Please, Sign In to add comment