Advertisement
Ladies_Man

body >> (block/cargo) << node

Oct 29th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1.  
  2.  
  3.  
  4. float delta_pos(float velocity, float acceleration);
  5.  
  6.  
  7.  
  8. class block {
  9. public:
  10.     char *name;
  11.     int mass;
  12.  
  13.     void grav();
  14.     void init();
  15.  
  16.     block(float pos_x, float pos_y) {
  17.         this->init_x = pos_x;
  18.         this->init_y = pos_y;
  19.  
  20.         this->curr_x = pos_x;
  21.         this->curr_y = pos_y;
  22.  
  23.         this->visibility = true;
  24.     }
  25.  
  26. private:
  27.     float init_x, init_y;
  28.     float curr_x, curr_y;
  29.     bool visibility;
  30.  
  31.     void draw(float pos_x, float pos_y);
  32. };
  33.  
  34. void block::draw(float pos_x, float pos_y) {
  35.     glBegin(GL_POLYGON);
  36.     glColor3f(1, 0, 0);
  37.     glVertex2i(pos_x, pos_y);
  38.     glVertex2i(pos_x + 20, pos_y);
  39.     glVertex2i(pos_x + 20, pos_y + 20);
  40.     glVertex2i(pos_x, pos_y + 20);
  41.     glEnd();
  42. }
  43.  
  44. void block::grav() {
  45.     if (this->curr_y > 0) {
  46.  
  47.         float pos_x = this->curr_x + delta_pos(0, 0);
  48.         float pos_y = this->curr_y - delta_pos(-2, G_CONST);
  49.  
  50.         this->curr_x = pos_x;
  51.         this->curr_y = pos_y;
  52.  
  53.     }
  54.     else {
  55.         this->curr_y = 0;
  56.         this->visibility = false;
  57.     }
  58.  
  59.     this->draw(this->curr_x, this->curr_y);
  60.  
  61.     printf("x:%f, y:%f\n", this->curr_x, this->curr_y);
  62. }
  63.  
  64. void block::init() {
  65.     float pos_x = this->init_x;
  66.     float pos_y = this->init_y;
  67.  
  68.     this->draw(pos_x, pos_y);
  69. }
  70.  
  71.  
  72. float delta_pos(float velocity, float acceleration) {
  73.     //s = s0 + v*t + (a*t^2)/2
  74.     float delta = velocity * t + (acceleration * t * t) / 2;
  75.     printf("delta=%f\n", delta);
  76.  
  77.     t += 0.0001;
  78.  
  79.     return delta;
  80. }
  81.  
  82.  
  83.  
  84.  
  85. class cargo {
  86. public:
  87.     char *name;
  88.     int ord_num;
  89.     int mass;
  90.     int move_direction;
  91.     block* left_block;
  92.     block* right_block;
  93.     cargo* above_cargo;
  94.     cargo* below_cargo;
  95.  
  96.     void grav(bool);
  97.     void init();
  98.     void add_right_block(block*);
  99.     void add_left_block(block*);
  100.     void add_above_cargo(cargo*);
  101.     void add_below_cargo(cargo*);
  102.  
  103.     cargo(int n, float pos_x, float pos_y) {
  104.         this->ord_num = n;
  105.  
  106.         this->init_x = pos_x;
  107.         this->init_y = pos_y;
  108.  
  109.         this->curr_x = pos_x;
  110.         this->curr_y = pos_y;
  111.  
  112.         this->velo_x = 0;
  113.         this->velo_y = 0;
  114.  
  115.         if (this->velo_y == 0) {
  116.             this->move_direction = -1;
  117.         }
  118.     }
  119.  
  120. private:
  121.     float init_x, init_y;
  122.     float curr_x, curr_y;
  123.     float velo_x, velo_y;
  124.  
  125.     void draw(float, float);
  126. };
  127.  
  128. void cargo::draw(float pos_x, float pos_y) {
  129.     glBegin(GL_POLYGON);
  130.     glColor3f(0, 1, 0);
  131.     glVertex2i(pos_x, pos_y);
  132.     glVertex2i(pos_x + 25, pos_y);
  133.     glVertex2i(pos_x + 25, pos_y + 25);
  134.     glVertex2i(pos_x, pos_y + 25);
  135.     glEnd();
  136. }
  137.  
  138. void cargo::grav(bool fall) {
  139.     if (fall) {
  140.  
  141.         float pos_y;
  142.  
  143.         if (this->curr_y <= 0) {
  144.  
  145.             this->curr_y = 0;
  146.  
  147.             this->velo_y = sqrt(2 * G_CONST * this->init_y) * 0.8;
  148.  
  149.             this->move_direction = 1;
  150.  
  151.         }
  152.  
  153.         if (this->curr_y >= 0) {
  154.  
  155.             if (this->move_direction < 0) {
  156.  
  157.                 float pos_y = (G_CONST * t * t) / 2;
  158.                 this->curr_y -= pos_y;
  159.  
  160.             }
  161.  
  162.             if (this->move_direction > 0) {
  163.  
  164.                 float pos_y = this->velo_y * t - (G_CONST * t * t) / 2;
  165.                 this->curr_y += pos_y;
  166.  
  167.             }
  168.  
  169.         }
  170.     }
  171.  
  172.     this->draw(this->curr_x, this->curr_y);
  173.  
  174.     t += 0.001;
  175.  
  176.     printf("x:%f, y:%f, v=%f\n", this->curr_x, this->curr_y, this->velo_y);
  177. }
  178.  
  179. void cargo::init() {
  180.     float pos_x = this->init_x;
  181.     float pos_y = this->init_y;
  182.  
  183.     this->draw(pos_x, pos_y);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement