Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <math.h>
- using namespace std;
- class cPlanet
- {
- public:
- double x, y; //position x and y components
- double vx, vy; //velocity x and y components
- double ax, ay; //acceleration x and y components
- double GM; //G constant times star mass is a new quantity that is constant
- double xS, yS; //the star is fixed and its position depends on the screen resolution
- cPlanet( double x, double y,
- double vx, double vy,
- double GM,
- double xS, double yS)
- {
- this->x = x;
- this->y = y;
- this->vx = vx;
- this->vy = vy;
- this->GM = GM;
- this->xS = xS;
- this->yS = yS;
- ax = 0.0;
- ay = 0.0;
- }
- void verletStep(double dt)
- {
- double xn = x + vx*dt + ax*dt*dt/2;
- double yn = y + vy*dt + ay*dt*dt/2; //calculate future position
- double dx = xn - xS;
- double dy = yn - yS;
- double r3 = pow(dx*dx+dy*dy, 1.5);
- double axn = 0.0;
- double ayn = 0.0;
- if(r3 != 0)
- {
- axn = -GM * dx / r3;
- ayn = -GM * dy / r3; //calculate future acceleration
- }
- double vxn = vx + (axn + ax)*dt/2;
- double vyn = vy + (ayn + ay)*dt/2; //calculate future velocity
- x = xn;
- y = yn;
- vx = vxn;
- vy = vyn;
- ax = axn;
- ay = ayn; //apply changes in position, velocity and force
- }
- };
- int main()
- {
- double resX = GetSystemMetrics(SM_CXSCREEN); //get screen width
- double resY = GetSystemMetrics(SM_CYSCREEN); //get screen height
- cPlanet planet( resX/2.0, resY/8.0, //x and y
- resY, 0.0, //vx and vy
- 0.75*pow(resY, 3.0), //GM constant
- resX/2.0, resY/2.0); //xS and yS
- while(1)
- {
- planet.verletStep(0.02); //move the planet forward in time using velocity verlet algorithm
- SetCursorPos(planet.x, planet.y); //move the mouse cursor
- Sleep(20);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment