Ladies_Man

#CCG FINAL PART3 (pendulum)

Dec 25th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. //pendulum.h
  2. #pragma once
  3. class pendulum
  4. {
  5. public:
  6.     coord carrier;
  7.     coord init_ticker;
  8.     coord curr_ticker;
  9.  
  10.     float rope_len;
  11.     float curr_angle, angle_degree, start_angle;
  12.     float amplitude, start_amplitude;
  13.     int mass;
  14.  
  15.     void set(coord carrier, coord ticker);
  16.     void set_physics(bool k);
  17.  
  18.     void count_rope_len();
  19.     void count_angle();
  20.  
  21.     void draw();
  22.     void move(float t);
  23.  
  24.     bool get_physics();
  25.  
  26.     pendulum();
  27.     ~pendulum();
  28.  
  29. private:
  30.     bool physics;
  31.     int ticker_rad, ticker_border;
  32. };
  33.  
  34.  
  35.  
  36.  
  37.  
  38. //pendulum.cpp
  39. #include "stdafx.h"
  40.  
  41. using namespace std;
  42.  
  43. pendulum::pendulum() {
  44.     physics = false;
  45. }
  46.  
  47. pendulum::~pendulum()
  48. {
  49. }
  50.  
  51. void pendulum::set_physics(bool ka) {
  52.     this->physics = ka;
  53. }
  54.  
  55. bool pendulum::get_physics() {
  56.     return this->physics;
  57. }
  58.  
  59. void pendulum::set(coord carrier, coord ticker) {
  60.  
  61.     this->carrier = carrier;
  62.    
  63.     this->init_ticker = ticker;
  64.     this->curr_ticker = ticker;
  65.  
  66.     this->start_angle = 0;
  67.  
  68.     this->count_angle();
  69.  
  70.     this->amplitude = this->curr_angle;
  71.     this->start_amplitude = this->amplitude;
  72.  
  73.     this->physics = false;
  74.  
  75.     this->ticker_rad = 40;
  76.     this->ticker_border = 5;
  77. }
  78.  
  79. void pendulum::count_angle() {
  80.  
  81.     this->count_rope_len();
  82.  
  83.     float TM = abs(this->carrier.x - this->curr_ticker.x);
  84.     float CT = this->rope_len;
  85.  
  86.     float angle = asin(TM / CT);
  87.    
  88.     this->curr_angle = angle;
  89.     this->angle_degree = angle * 180 / M_PI;
  90. }
  91.  
  92. void pendulum::count_rope_len() {
  93.     coord carr = this->carrier;
  94.     coord tick = this->curr_ticker;
  95.  
  96.     float CM = abs(carr.y - tick.y);
  97.     float TM = abs(carr.x - tick.x);
  98.     float CT = sqrt(TM*TM + CM*CM);
  99.  
  100.     this->rope_len = CT;
  101. }
  102.  
  103. void draw_pendulum_bracket(int x, int y, int size) {
  104.  
  105.     glLineWidth(3.0);
  106.     glColor3f(0, 0, 0);
  107.  
  108.     glBegin(GL_LINES);
  109.     glVertex2f(x, y);
  110.     glVertex2f(x, y - size);
  111.     glEnd();
  112.  
  113.     //outer
  114.     GLfloat phi = 0, theta = 0;
  115.     glBegin(GL_POLYGON);
  116.     glColor3f(0, 0, 0);
  117.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  118.         glVertex2f(x + 8 * cos(phi), (y - size) + 8 * sin(phi));
  119.     }
  120.     glEnd();
  121.  
  122.     //inner
  123.     glBegin(GL_POLYGON);
  124.     glColor3f(0.6, 0.6, 0.6);
  125.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  126.         glVertex2f(x + 5 * cos(phi), (y - size) + 5 * sin(phi));
  127.     }
  128.     glEnd();
  129.  
  130. }
  131.  
  132. void pendulum::draw() {
  133.  
  134.     coord carrier = this->carrier;
  135.     coord ticker = this->curr_ticker;
  136.  
  137.    
  138.     //carrier
  139.     draw_pendulum_bracket(carrier.x, carrier.y + 20, 15);
  140.  
  141.     //ticker outer
  142.     GLfloat phi = 0, theta = 0;
  143.     glBegin(GL_POLYGON);
  144.     glColor3f(0, 0, 0);
  145.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  146.         glVertex2f(ticker.x + this->ticker_rad * cos(phi), ticker.y + this->ticker_rad * sin(phi));
  147.     }
  148.     glEnd();
  149.  
  150.     //ticker inner
  151.     glBegin(GL_POLYGON);
  152.     glColor3f(1, 1, 1);
  153.     for (phi = 0; phi < 2 * M_PI; phi += 0.1) {
  154.         glVertex2f(ticker.x + (this->ticker_rad - this->ticker_border) * cos(phi), ticker.y + (this->ticker_rad - this->ticker_border) * sin(phi));
  155.     }
  156.     glEnd();
  157.  
  158.     //ticker center
  159.     glPointSize(7.0);
  160.     glColor3f(0, 0, 0);
  161.     glBegin(GL_POINTS);
  162.         glVertex2f(ticker.x, ticker.y);
  163.     glEnd();
  164.  
  165.     //rope
  166.     glLineWidth(3.0);
  167.     glColor3f(0, 0, 0);
  168.     glBegin(GL_LINES);
  169.         glVertex2f(carrier.x, carrier.y);
  170.         glVertex2f(ticker.x, ticker.y);
  171.     glEnd();
  172. }
  173.  
  174. void pendulum::move(float t) {
  175.  
  176.     coord carr = this->carrier;
  177.     coord tick = this->curr_ticker;
  178.  
  179.     if (tick.y - this->ticker_rad > FLOOR_HEIGHT && tick.y + this->ticker_rad < ROOF_HEIGHT) {
  180.         this->count_angle();
  181.  
  182.         float omega = sqrt(G_CONST / this->rope_len);
  183.         //float attenuation = pow(M_E, -log(this->amplitude / (this->amplitude * 0.5)));
  184.         float attenuation_k = 0.99999;
  185.         float phi = this->amplitude * cos(omega * t + this->start_angle);
  186.         this->amplitude *= attenuation_k;
  187.  
  188.         this->curr_angle = phi;
  189.  
  190.         float TM = this->rope_len * sin(phi);
  191.         float CM = this->rope_len * sin(90*M_PI/180 - phi); //1,57 rad
  192.  
  193.         coord T;
  194.         T.x = carr.x - TM;
  195.         T.y = carr.y - CM;
  196.  
  197.         this->curr_ticker = T;
  198.     }
  199. }
Add Comment
Please, Sign In to add comment