Guest User

Untitled

a guest
Oct 11th, 2014
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. class cPlanet
  9. {
  10.     public:
  11.         double x, y;    //position x and y components
  12.         double vx, vy;  //velocity x and y components
  13.         double ax, ay;  //acceleration x and y components
  14.        
  15.         double GM;  //G constant times star mass is a new quantity that is constant
  16.         double xS, yS;  //the star is fixed and its position depends on the screen resolution
  17.        
  18.         cPlanet(    double x, double y,
  19.                 double vx, double vy,
  20.                 double GM,
  21.                 double xS, double yS)
  22.         {
  23.             this->x = x;
  24.             this->y = y;
  25.             this->vx = vx;
  26.             this->vy = vy;
  27.             this->GM = GM;
  28.             this->xS = xS;
  29.             this->yS = yS;
  30.             ax = 0.0;
  31.             ay = 0.0;
  32.         }
  33.        
  34.         void verletStep(double dt)
  35.         {
  36.             double xn = x + vx*dt + ax*dt*dt/2;
  37.             double yn = y + vy*dt + ay*dt*dt/2; //calculate future position
  38.            
  39.             double dx = xn - xS;
  40.             double dy = yn - yS;
  41.             double r3 = pow(dx*dx+dy*dy, 1.5);
  42.            
  43.             double axn = 0.0;
  44.             double ayn = 0.0;
  45.             if(r3 != 0)
  46.             {
  47.                 axn = -GM * dx / r3;
  48.                 ayn = -GM * dy / r3;    //calculate future acceleration
  49.             }
  50.            
  51.             double vxn = vx + (axn + ax)*dt/2;
  52.             double vyn = vy + (ayn + ay)*dt/2;  //calculate future velocity
  53.            
  54.             x = xn;
  55.             y = yn;
  56.             vx = vxn;
  57.             vy = vyn;
  58.             ax = axn;
  59.             ay = ayn; //apply changes in position, velocity and force
  60.         }
  61. };
  62.  
  63. int main()
  64. {
  65.     double resX = GetSystemMetrics(SM_CXSCREEN);    //get screen width
  66.     double resY = GetSystemMetrics(SM_CYSCREEN);    //get screen height
  67.    
  68.     cPlanet planet( resX/2.0, resY/8.0, //x and y
  69.             resY, 0.0,      //vx and vy
  70.             0.75*pow(resY, 3.0),    //GM constant
  71.             resX/2.0, resY/2.0);    //xS and yS
  72.    
  73.     while(1)
  74.     {
  75.         planet.verletStep(0.02);        //move the planet forward in time using velocity verlet algorithm
  76.         SetCursorPos(planet.x, planet.y);   //move the mouse cursor
  77.         Sleep(20);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment