Guest User

Untitled

a guest
Oct 18th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.17 KB | None | 0 0
  1. #include <windows.h>
  2. #include <cmath>
  3.  
  4. /* The cBall class represents a point in a 2D space. */
  5.  
  6. class cBall
  7. {
  8.     public:
  9.         double xp, yp;  /* The ball previous position. */
  10.         double x, y;    /* The ball current position. */
  11.        
  12.         cBall(double x, double y)
  13.         {
  14.             this->xp = x;   /* These two lines mean */
  15.             this->yp = y;   /* that the ball is not moving at first. */
  16.             this->x = x;
  17.             this->y = y;
  18.         }
  19.         void verlet()
  20.         {
  21.             /* Calculate velocity from the ball previous and current position.
  22.             Actually, this isn't velocity but the product of velocity and the time step. */
  23.             double vx = x - xp;
  24.             double vy = y - yp;
  25.             /* Calculate the ball future position. */
  26.             double xn = x + vx;
  27.             double yn = y + vy;
  28.             /* Apply changes in the ball previous and current position. */
  29.             xp = x;
  30.             yp = y;
  31.             x = xn;
  32.             y = yn;
  33.         }
  34. };
  35.  
  36. /* The cSpring class represents a link between two balls. */
  37.  
  38. class cSpring
  39. {
  40.     public:
  41.         cBall *b1, *b2; /* Pointers to two ball objects. */
  42.         double l;   /* The spring resting distance. */
  43.        
  44.         cSpring(cBall *b1, cBall *b2, double l)
  45.         {
  46.             this->b1 = b1;
  47.             this->b2 = b2;
  48.             this->l = l;
  49.         }
  50.        
  51.         void solve()
  52.         {
  53.             /* The solve method repels the linked balls if they are too close to each other
  54.             and attracts them if they are to far from each other. */
  55.             double dx = b2->x - b1->x;
  56.             double dy = b2->y - b1->y;
  57.             double dl = sqrt(dx*dx + dy*dy); /* The distance between two balls. */
  58.             double C = 0.0;
  59.             if(dl != 0.0)
  60.                 C = 0.3 * (l - dl) / dl;
  61.                 /* If the distance between the balls is equal to the resting distance,
  62.                 the C equals 0 and the balls neither repel nor attract each other.
  63.                 If the distance between the balls is smaller than the resting distance,
  64.                 the C is positive and the balls repel each other.
  65.                 If the distance between the balls is greater than the resting distance,
  66.                 the C is negative and the balls attract each other. */
  67.             double rx = C*dx;
  68.             double ry = C*dy;
  69.             b1->x -= rx;
  70.             b1->y -= ry;
  71.             b2->x += rx;
  72.             b2->y += ry;
  73.         }
  74. };
  75.  
  76. /* The cSoftBody class represents the soft body consisting of one centre ball
  77. and four side balls linked to the centre ball with four springs. */
  78.  
  79. class cSoftBody
  80. {
  81.     public:
  82.         cBall *balls[5];    /* Array of pointers to the balls. */
  83.         cSpring *springs[4];    /* Array of pointers to the springs. */
  84.         double worldW, worldH;  /* Screen resolution (width and height) in pixels. */
  85.         double R;       /* The radius of the undeformed soft body. */
  86.        
  87.         cSoftBody()
  88.         {
  89.             R = 50.0;
  90.             worldW = GetSystemMetrics(SM_CXSCREEN); /* Assign the screen width and height */
  91.             worldH = GetSystemMetrics(SM_CYSCREEN); /* to the worldW and worldH variables. */
  92.             /* The position of the screen centre in pixels. */
  93.             double cx = worldW / 2.0;
  94.             double cy = worldH / 2.0;
  95.             /* The position of each ball is given in the screen coordinates
  96.             and the centre ball is placed in the middle of the screen. */
  97.             balls[0] = new cBall(cx, cy);
  98.             balls[1] = new cBall(cx - R, cy);
  99.             balls[2] = new cBall(cx, cy - R);
  100.             balls[3] = new cBall(cx + R, cy);
  101.             balls[4] = new cBall(cx, cy + R);
  102.            
  103.             for(int i = 0; i < 4; i++)
  104.             {
  105.                 /* Each spring links the centre ball to one of the side balls. */
  106.                 springs[i] = new cSpring(balls[0], balls[i+1], R);
  107.             }
  108.         }
  109.         ~cSoftBody()
  110.         {
  111.             for(int i = 0; i < 4; i++)
  112.             {
  113.                 delete balls[i];
  114.                 delete springs[i];
  115.             }
  116.             delete balls[4];
  117.         }
  118.         void applyVelocity(double vx, double vy, double dt)
  119.         {
  120.             /* The applyVelocity method adds velocity to each ball
  121.             by modifying its previous position.
  122.             The velocity should be given in pixels per second. */
  123.             for(int i = 0; i < 5; i++)
  124.             {
  125.                 balls[i]->xp = balls[i]->x - vx*dt;
  126.                 balls[i]->yp = balls[i]->y - vy*dt;
  127.             }
  128.         }
  129.         void loop()
  130.         {
  131.             /* Solve the constraint for each spring. */
  132.             for(int i = 0; i < 4; i++)
  133.             {
  134.                 springs[i]->solve();
  135.             }
  136.             for(int i = 0; i < 5; i++)
  137.             {
  138.                 /* Solve the equation of motion for each ball. */
  139.                 balls[i]->verlet();
  140.                 /* React when a ball leaves the screen. */
  141.                 if(balls[i]->x < 0.0)
  142.                     balls[i]->x = 0.0;
  143.                 if(balls[i]->y < 0.0)
  144.                     balls[i]->y = 0.0;
  145.                 if(balls[i]->x > worldW)
  146.                     balls[i]->x = worldW;
  147.                 if(balls[i]->y > worldH)
  148.                     balls[i]->y = worldH;
  149.             }
  150.             /* The following piece of code makes sure that
  151.             the left ball and the right ball can only move
  152.             in the x direction in relation to the centre ball
  153.             and that the top ball and the bottom ball can only move
  154.             in the y direction in relation to the centre ball. */
  155.             balls[1]->y = balls[0]->y;
  156.             balls[2]->x = balls[0]->x;
  157.             balls[3]->y = balls[0]->y;
  158.             balls[4]->x = balls[0]->x;
  159.         }
  160. };
  161.  
  162. /* Create an object of the cSoftBody class. */
  163. cSoftBody body;
  164.  
  165. /* This is where all the input to the window goes to */
  166. LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  167. {
  168.     switch(Message)
  169.     {
  170.         /* Upon destruction, tell the main thread to stop */
  171.         case WM_DESTROY:
  172.         {
  173.             PostQuitMessage(0);
  174.             break;
  175.         }
  176.         case WM_TIMER:
  177.         {
  178.             /* Predict the next state of the body. */
  179.             body.loop();
  180.             /* Create the following variables for an easier access. */
  181.             double pl = body.balls[1]->x;
  182.             double pt = body.balls[2]->y;
  183.             double pr = body.balls[3]->x;
  184.             double pb = body.balls[4]->y;
  185.             double pcx = body.balls[0]->x;
  186.             double pcy = body.balls[0]->y;
  187.             double R = body.R;
  188.             /* Move the window so that its centre is where the centre ball is.
  189.             The width and height of the window should be four times bigger
  190.             than the radius of the undeformed soft body.
  191.             The window should be on top of everything. */
  192.             SetWindowPos(hwnd, HWND_TOPMOST, pcx-2*R, pcy-2*R, 4*R, 4*R, SWP_SHOWWINDOW);
  193.             /* Create an elliptic region.
  194.             Its position and dimensions should be given in the window coordinates.
  195.             The left, top, right and bottom ball specifies respectively
  196.             the left, top, right and bottom edge of the region bounding box. */
  197.             HRGN ellipse = CreateEllipticRgn(2*R-pcx+pl, 2*R-pcy+pt, 2*R-pcx+pr, 2*R-pcy+pb);
  198.             SetWindowRgn(hwnd, ellipse, TRUE);
  199.             break;
  200.         }
  201.         /* All other messages (a lot of them) are processed using default procedures */
  202.         default:
  203.             return DefWindowProc(hwnd, Message, wParam, lParam);
  204.     }
  205.     return 0;
  206. }
  207.  
  208. /* The 'main' function of Win32 GUI programs: this is where execution starts */
  209. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  210. {
  211.     body.applyVelocity(500.0, 500.0, 0.04);
  212.    
  213.     WNDCLASSEX wc; /* A properties struct of our window */
  214.     HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
  215.     MSG Msg; /* A temporary location for all messages */
  216.  
  217.     /* zero out the struct and set the stuff we want to modify */
  218.     memset(&wc,0,sizeof(wc));
  219.     wc.cbSize       = sizeof(WNDCLASSEX);
  220.     wc.lpfnWndProc      = WndProc; /* This is where we will send messages to */
  221.     wc.hInstance        = hInstance;
  222.     wc.hCursor      = LoadCursor(NULL, IDC_ARROW);
  223.     wc.hbrBackground    = CreateSolidBrush(RGB(255, 0, 0)); /* Make the window red. */
  224.     wc.lpszClassName    = "WindowClass";
  225.     wc.hIcon        = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
  226.     wc.hIconSm      = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
  227.  
  228.     if(!RegisterClassEx(&wc))
  229.     {
  230.         MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
  231.         return 0;
  232.     }
  233.  
  234.     hwnd = CreateWindowEx(0,"WindowClass","Caption",WS_POPUP,
  235.         body.worldW/2.0, /* x */
  236.         body.worldH/2.0, /* y */
  237.         4*body.R, /* width */
  238.         4*body.R, /* height */
  239.         NULL,NULL,hInstance,NULL);
  240.  
  241.     if(hwnd == NULL)
  242.     {
  243.         MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
  244.         return 0;
  245.     }
  246.    
  247.     /* Loop the simulation every 40 milliseconds. */
  248.     SetTimer(hwnd, 1, 40, (TIMERPROC) NULL);
  249.  
  250.     /*
  251.         This is the heart of our program where all input is processed and
  252.         sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
  253.         this loop will not produce unreasonably high CPU usage
  254.     */
  255.     while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
  256.         TranslateMessage(&Msg); /* Translate key codes to chars if present */
  257.         DispatchMessage(&Msg); /* Send it to WndProc */
  258.     }
  259.     return Msg.wParam;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment