csansoon

Launching ball phyisics sim [Test] (Just ignore xd)

Jan 3rd, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5. #define PI 3.14159265
  6.  
  7.     //int x,y,sizex,sizey;
  8.     //double initialx,initialy,force,angle,mass,gravity,time,timeskip;
  9.     double initialx = 1.0;
  10.     double initialy = 1.0;
  11.     int sizex = 75;
  12.     int sizey = 50;
  13.     double mass = 0.7;
  14.     double gravity = 9.8;
  15.     double time = 20;
  16.     double timeskip = 0.05;
  17.    
  18.    
  19. print(vector <vector <bool> >& map) {
  20.     for (int i=map.size()-1; i>=0; --i) {
  21.         for (int j=0; j<map[0].size(); ++j) {
  22.             if (i==initialy and j==initialx) cout<<"x";
  23.             else {
  24.                 if (map[i][j]) cout << "*";
  25.                 else cout << " ";
  26.             }
  27.         }
  28.         cout<<endl;
  29.     }
  30.     for (int j=0; j<map[0].size(); ++j) cout<<"=";
  31.     cout<<endl;
  32. }
  33.  
  34. void clear(int n) {for (int i=0; i<n; ++i) cout<<endl;}
  35. //string tolower(string s) {for (int i=0; i<s.length(); i++) tolower(s[i]); return s;}
  36.  
  37. int main() {
  38.     int x,y;
  39.     double force,angle;
  40.     string s;
  41.    
  42. mainMenu:
  43.     cout << "Type to select:\n";
  44.     cout << "\n";
  45.     cout << "   [Play]\n";
  46.     cout << " [Settings]\n";
  47.     cin>>s;
  48.     //s = tolower(s);
  49.     if (s=="play") goto Play;
  50.     else if (s=="settings") goto Settings;
  51.     cout << "ERROR: Unknown command. Try writing in lowercase."<<endl;
  52.     goto mainMenu;
  53.    
  54. Settings:
  55.     clear(50);
  56.     cout<<"  ~ Table size: "<<sizex<<"x"<<sizey<<"               <size>"<<endl;
  57.     cout<<"  ~ Gravity: "<<gravity<<"m/s2              <gravity>"<<endl;
  58.     cout<<"  ~ Ball Mass: "<<mass<<"kg                <mass>"<<endl;
  59.     cout<<"  ~ Simulation Time: "<<time<<"s            <time>"<<endl;
  60.     cout<<"  ~ Time skipping: "<<timeskip<<"s            <skip>"<<endl;
  61.     cout<<"  ~ Launch position: ["<<initialx<<"]["<<initialy<<"]       <position>"<<endl;
  62.     cout<<endl<<endl<<" [Menu]"<<endl;
  63.    
  64.     cin>>s;
  65.     if (s=="size") {
  66.         cout<<endl<<endl<<"Enter new size: ";
  67.         cin>>sizex>>sizey;
  68.         goto Settings;
  69.     }
  70.     if (s=="gravity") {
  71.         cout<<endl<<endl<<"Enter new gravity: ";
  72.         cin>>gravity;
  73.         goto Settings;
  74.     }
  75.     if (s=="mass") {
  76.         cout<<endl<<endl<<"Enter new mass: ";
  77.         cin>>mass;
  78.         goto Settings;
  79.     }
  80.     if (s=="time") {
  81.         cout<<endl<<endl<<"Enter new time: ";
  82.         cin>>time;
  83.         goto Settings;
  84.     }
  85.     if (s=="skip") {
  86.         cout<<endl<<endl<<"Enter new skip time: ";
  87.         cin>>timeskip;
  88.         goto Settings;
  89.     }
  90.     if (s=="position") {
  91.         cout<<endl<<endl<<"Enter new launch position: ";
  92.         cin>>initialx>>initialy;
  93.         goto Settings;
  94.     }
  95.     if (s=="menu") goto mainMenu;
  96.     else goto Settings;
  97.    
  98. Play:
  99.     clear(50);
  100.     vector <vector <bool> > map(sizey, vector <bool> (sizex));
  101.     print(map);
  102.     cout << "Angle: ";
  103.     cin >> angle;
  104.     cout << "Force: ";
  105.     cin >> force;
  106.     cout << endl;
  107.     for (double t=0; t<time; t = t + timeskip) {
  108.         //x = initialx + (cos(angle)*force*t*t)/(2*mass));
  109.         x = initialx + (cos(angle*PI/180))*force*t;
  110.         y = initialy + (sin(angle*PI/180))*force*t - (gravity/2)*t*t;
  111.         if (x>=0 and x<map[0].size() and y>=0 and y<map.size()) {
  112.             map[y][x] = true;
  113.             cout << "[" << x << "] [" << y << "]" << endl;
  114.         }
  115.     }
  116.    
  117.     print(map);
  118.     cout<<endl<<" [Restart]"<<endl<<"  [Menu]"<<endl<<endl;
  119.     cin>>s;
  120.     if (s=="restart") goto Play;
  121.     else if (s=="menu") goto mainMenu;
  122.     else cout << "ERROR: Unknown command. Try writing in lowercase."<<endl;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment