Advertisement
wavicle

in-terminal simulation

Feb 13th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. //cheap physics simulation of bouncing balls in a tube.
  2. //Object location history is "graphed" time vs location.
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main()
  8. {   char tube[82]; //width of terminal + 2, must be even
  9.    
  10.     int blank = 32; //' '
  11.     int wall = 119; //'w'
  12.     int plus = 43;  //'+'
  13.     int minus = 45; //'-'
  14.    
  15.     for(int a = 0; a <= 81; a++)
  16.     {   tube[a] = blank;
  17.     }
  18.    
  19.     tube[0] = wall;
  20.     tube[81] = wall;
  21.    
  22.     //initial symbol positions (separate by even # of blocks)
  23.     tube[14] = plus;
  24.     tube[17] = minus;
  25.    
  26.     //"pointers" to symbol positions
  27.     int left_loc = 14, right_loc = 17;
  28.    
  29.     //initial directional states of symbols (go left = -1, go right = 1)
  30.     int left_state = -1, right_state = 1;
  31.    
  32.     for(int a = 0; a <= 1020; a++)
  33.     {   for(int a = 1; a <= 80; a++)
  34.         {   cout << tube[a];
  35.         }
  36.        
  37.         cout << endl;
  38.        
  39.         if(tube[left_loc + left_state] == blank) //plus normal move
  40.         {   tube[left_loc] = blank;
  41.             tube[left_loc + left_state] = plus;
  42.             left_loc += left_state;
  43.         }
  44.        
  45.         if(tube[left_loc + left_state] == wall) //plus wall bounce
  46.         {   left_state *= -1;
  47.         }
  48.        
  49.         if(tube[right_loc + right_state] == blank) //minus normal move
  50.         {   tube[right_loc] = blank;
  51.             tube[right_loc + right_state] = minus;
  52.             right_loc += right_state;
  53.         }
  54.        
  55.         if(tube[right_loc + right_state] == wall) //minus wall bounce
  56.         {   right_state *= -1;
  57.         }
  58.        
  59.         if(tube[left_loc + 1] == minus) //plus on minus collision
  60.         {   left_state *= -1;
  61.             right_state *= -1;
  62.         }
  63.        
  64.         for(int waste = 0; waste <= 16000000; waste++){} //for 3.4GHz (i7-5557U)
  65.     }
  66.    
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement