Advertisement
daktarism

2D Physics Simulator

Apr 25th, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #define _WIN32_WINNT 0x0500      //needed by gotoXY function only.
  3. #include "Windows.h"             //needed by gotoXY function only.
  4. using namespace std;
  5.  
  6. //gotoXY function, puts the cursor to x,y coordinate
  7. void gotoXY(int x, int y)
  8. {
  9.   static HANDLE h = NULL;
  10.   if(!h)
  11.     h = GetStdHandle(STD_OUTPUT_HANDLE);
  12.   COORD c = { x, y };
  13.   SetConsoleCursorPosition(h,c);
  14. }
  15.  
  16. //main function
  17. int main()
  18. {
  19.     float x=0,y=0,vx,vy,g,fs;
  20.  
  21.     printf("        Enter the Horizontal Velocity ( Recommended Value : 2.5 ) = ");
  22.     scanf("%f",&vx);
  23.     printf("        Enter the Vertical Velocity ( Recommended Value : 2.5 ) = ");
  24.     scanf("%f",&vy);
  25.     printf("        Enter the Gravity ( Recommended Value : 0.5 ) = ");
  26.     scanf("%f",&g);
  27.     printf("        Enter the Friction ( Recommended Value : 0.025 ) = ");
  28.     scanf("%f",&fs);
  29.  
  30.     while(1){
  31.  
  32.             if(GetAsyncKeyState(VK_UP) != 0){ //reset
  33.                 system("CLS");
  34.                 x=0;
  35.                 y=0;
  36.                 printf("        Enter the Horizontal Velocity ( Recommended Value : 2.5 ) = ");
  37.                 scanf("%f",&vx);
  38.                 printf("        Enter the Vertical Velocity ( Recommended Value : 2.5 ) = ");
  39.                 scanf("%f",&vy);
  40.                 printf("        Enter the Gravity ( Recommended Value : 0.5 ) = ");
  41.                 scanf("%f",&g);
  42.         printf("        Enter the Friction ( Recommended Value : 0.025 ) = ");
  43.             scanf("%f",&fs);
  44.             }
  45.  
  46.             //update
  47.             vy+=g;
  48.             x+=vx;
  49.             y+=vy;
  50.  
  51.             //check & fix
  52.             if(y+vy>24)
  53.                 y=24;
  54.  
  55.             if((y<15 && y+vy>15 ) || (y>15 && y+vy<15) )
  56.                y=15;
  57.             if(x+vx>78)
  58.                 x=78;
  59.             else if(x+vx<0)
  60.                 x=0;
  61.  
  62.                 if(y==24 && vx!=0){
  63.                     if(vx>0)
  64.                         vx-=fs;
  65.                     else if(vx<0)
  66.                         vx+=fs;
  67.                 }
  68.  
  69.             if(y>=24 || (x>=10 && x<=35 && y==15))
  70.                     vy*=-1;
  71.             if(x>=78 || x<=0)
  72.                     vx*=-1;
  73.  
  74.             //draw
  75.             system("CLS");
  76.             if(x>=0 && x<=78 && y<=24 && y>=0){
  77.                 gotoXY(x,y);
  78.                 printf("O");
  79.             }
  80.  
  81.             for(int i=10;i<=35;i++){
  82.                 gotoXY(i,15);
  83.                 printf("*");
  84.             }
  85.  
  86.             gotoXY(1,1);
  87.             printf("     x = %f , vx = %f  //  y = %f , vy = %f\n\n           Reset : Up Arrow  // Tolga Ay - http://about.me/tolga.ay", x,vx,y,vy);
  88.  
  89.             Sleep(40);
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement