Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. class Body{
  2.  
  3. public:
  4.     Vector points[4][5];
  5.     float resolution;
  6.     float time[5];
  7.     Vector position;
  8.     Vector speed;
  9.     float rotate;
  10.     bool is_exist;// ha robbanás után leesik a földre akkor eltünik
  11.     void boom(Vector vec) {
  12.         rotate = 0;
  13.         speed = vec;
  14.     }
  15.     void animate() {
  16.         position.y =position.y+ speed.y*d_time - g_force / 2 * d_time*d_time;
  17.         position.x += speed.x*d_time;
  18.         position.z += speed.z*d_time;
  19.         rotate += 20 * d_time;
  20.         if (position.y < 0)
  21.             is_exist = false;
  22.     }
  23.     Body() {
  24.         resolution = 4;
  25.         time[0] = 0;
  26.         time[1] = 30;
  27.         time[2] = 60;
  28.         time[3] = 90;
  29.         time[4] = 91;
  30.         is_exist = true;
  31.         points[0][0] = Vector(0, 3.5, 0);
  32.         points[0][1] = Vector(-1.5, 4, 0);
  33.         points[0][2] = Vector(-1, 4.5, 0);
  34.         points[0][3] = Vector(1, 4.5, 0);
  35.         points[0][4] = Vector(1.5, 4, 0);
  36.  
  37.         points[1][0] = Vector(0, 1, 1.5);
  38.         points[1][1] = Vector(-3, 3, 1.5);
  39.         points[1][2] = Vector(-2, 3.5, 1.5);
  40.         points[1][3] = Vector(2, 3.5, 1.5);
  41.         points[1][4] = Vector(3, 3, 1.5);
  42.  
  43.         points[2][0] = Vector(0, 1.5, 3.5);
  44.         points[2][1] = Vector(-2.5, 2.5, 3.5);
  45.         points[2][2] = Vector(-3, 3, 3.5);
  46.         points[2][3] = Vector(3, 3, 3.5);
  47.         points[2][4] = Vector(-2.5, 2.5, 3.5);
  48.  
  49.         points[3][0] = Vector(0, 2, 5);
  50.         points[3][1] = Vector(-1, 3, 5);
  51.         points[3][2] = Vector(-0.5, 4, 5);
  52.         points[3][3] = Vector(0.5, 4, 5);
  53.         points[3][4] = Vector(1, 3, 5);
  54.  
  55.     }
  56.  
  57.     //másolva a diából
  58.     float B(int i, float t) {
  59.         int n =4; // n deg polynomial = n+1 pts!
  60.         float choose = 1;
  61.         for (int j = 1; j <= i; j++) choose *= (float)(n - j + 1) / j;
  62.         return choose * pow(t, i) * pow(1 - t, 5 - i);
  63.     }
  64.  
  65.     Vector Bezier(float t,int number) {
  66.         Vector rr=Vector(0, 0,0);
  67.         for (int i = 0; i < 5; i++) rr =rr+ points[number][i] * B(i, t);
  68.         return rr;
  69.     }
  70.  
  71.  
  72.  
  73.    
  74.     void material() {
  75.         GLfloat kd[] = { 0.0, 1.0, 1.0, 1.0 };
  76.         GLfloat ks[] = { 1.0, 0.5, 1.0, 1.0 };
  77.         GLfloat ka[] = { 0.5, 0.5, 1, 1.0 };
  78.         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ka);
  79.         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, kd);
  80.         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ks);
  81.         glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20);
  82.     }
  83.     void draw() {
  84.         material();
  85.         glBegin(GL_QUADS);
  86.         for (int i = 0; i < resolution-1; i++) {
  87.             float current_time = i / (float)resolution;
  88.             for (int j = 0; j < 4; j++)
  89.             {
  90.                 Vector B_pont1=Bezier(current_time, j);
  91.                 Vector B_pont2 = Bezier(current_time+1.0/(float)resolution, j);
  92.                 Vector Bplus1_pont1= Bezier(current_time, j+1);
  93.                 Vector Bplus1_pont2 = Bezier(current_time + 1.0 / (float)resolution, j+1);
  94.                 Vector Bplus2_pont1 = Bezier(current_time, j + 2);
  95.                 Vector Bplus2_pont2 = Bezier(current_time + 1.0 / (float)resolution, j +2);
  96.                 Vector Bminus1_pont1 = Bezier(current_time, j -2);
  97.                 Vector Bminus1_pont2 = Bezier(current_time + 1.0 / (float)resolution, j -1);
  98.                 if (j > 2) {
  99.                     Bplus1_pont1= B_pont1;
  100.                     Bplus1_pont2= B_pont2;
  101.                 }
  102.                 if (j ==2) {
  103.                     Bplus2_pont1 = Bplus1_pont1;
  104.                     Bplus2_pont2 = Bplus1_pont2;
  105.                 }
  106.                 if (j == 0) {
  107.                     Bminus1_pont1 = B_pont1;
  108.                     Bminus1_pont2 = B_pont2;
  109.                 }
  110.  
  111.  
  112.                 Vector vi_1;
  113.                 Vector viplus1_1;
  114.                 Vector vi_2;
  115.                 Vector viplus1_2;
  116.  
  117.                 viplus1_1 = ((Bplus2_pont1 - Bplus1_pont1)*((float)1 / (time[j + 2] - time[j + 1])) + (Bplus1_pont1 - B_pont1)*((float)1 / (time[j + 1] - time[j])))*0.5f;
  118.                 vi_1 = ((Bplus1_pont1 - B_pont1)*((float)1 / (time[j + 1] - time[i])) + (B_pont1 - Bminus1_pont1)*((float)1 / (time[j] - time[j - 1])))*0.5f;
  119.                 Vector a0_1 = B_pont1;
  120.                 Vector a1_1 = vi_1;
  121.                 Vector a2_1 = (Bplus1_pont1 - a0_1)*(3.0f / ((time[j + 1] - time[i])*(time[j + 1] - time[j]))) -
  122.                     ((viplus1_1 + (vi_1*2.0f))*(1.0f / (time[j + 1] - time[j])));
  123.                 Vector a3_1 = (a0_1 - Bplus1_pont1)*(2.0f / ((time[j + 1] - time[j])*(time[j + 1] - time[j])*(time[j + 1] - time[j]))) +
  124.                     ((viplus1_1 + vi_1)*(1.0f / ((time[j + 1] - time[j])*(time[j + 1] - time[j]))));
  125.  
  126.                 viplus1_2 = ((Bplus2_pont2 - Bplus1_pont2)*((float)1 / (time[j + 2] - time[j + 1])) + (Bplus1_pont2 - B_pont2)*((float)1 / (time[j + 1] - time[j])))*0.5f;
  127.                 vi_2 = ((Bplus1_pont2 - B_pont2)*((float)1 / (time[j + 1] - time[j])) + (B_pont2 - Bminus1_pont2)*((float)1 / (time[j] - time[j - 1])))*0.5f;
  128.                 Vector a0_2 = B_pont2;
  129.                 Vector a1_2 = vi_1;
  130.                 Vector a2_2 = (Bplus1_pont2 - a0_2)*(3.0f / ((time[j + 1] - time[j])*(time[j + 1] - time[j]))) -
  131.                     ((viplus1_2 + (vi_2*2.0f))*(1.0f / (time[j + 1] - time[j])));
  132.                 Vector a3_2 = (a0_2 - Bplus1_pont2)*(2.0f / ((time[j + 1] - time[j])*(time[j + 1] - time[j])*(time[j + 1] - time[j]))) +
  133.                     ((viplus1_2 + vi_2)*(1.0f / ((time[j + 1] - time[j])*(time[j + 1] - time[j]))));
  134.  
  135.  
  136.                 for (float t = time[j]; t <= time[j + 1]; t +=  (time[j+1]-time[j])/ (float)resolution) {
  137.                     float tplus1 = t + (time[j + 1] - time[j]) / (float)resolution;
  138.                     Vector ft_1 = a3_1*((t - time[j])*(t - time[j])*(t - time[j])) +
  139.                         a2_1*((t - time[j])*(t - time[j])) +
  140.                         a1_1*(t - time[j]) +
  141.                         a0_1;
  142.                     Vector ft_2 = a3_2*((t - time[j])*(t - time[j])*(t - time[j])) +
  143.                         a2_2*((t - time[j])*(t - time[j])) +
  144.                         a1_2*(t - time[j]) +
  145.                         a0_2;
  146.                     Vector ft_3 = a3_2*((tplus1 - time[j])*(t - time[j])*(tplus1 - time[j])) +
  147.                         a2_2*((tplus1 - time[j])*(tplus1 - time[j])) +
  148.                         a1_2*(tplus1 - time[j]) +
  149.                         a0_2;
  150.                     Vector ft_4 = a3_1*((tplus1 - time[j])*(tplus1 - time[j])*(tplus1 - time[j])) +
  151.                         a2_1*((tplus1 - time[j])*(tplus1 - time[j])) +
  152.                         a1_1*(tplus1 - time[j]) +
  153.                         a0_1;
  154.  
  155.                     //normálvektor számolása
  156.                     Vector normal = (ft_2 - ft_1)%(ft_4 - ft_1);
  157.                     glVertex3f(ft_1.x, ft_1.y, ft_1.z);
  158.                     glNormal3f(normal.x, normal.y, normal.z);
  159.                     glVertex3f(ft_2.x, ft_2.y, ft_2.z);
  160.                     glNormal3f(normal.x, normal.y, normal.z);
  161.                     glVertex3f(ft_4.x, ft_4.y, ft_4.z);
  162.                     glNormal3f(normal.x, normal.y, normal.z);
  163.                     glVertex3f(ft_3.x, ft_3.y, ft_3.z);
  164.                     glNormal3f(normal.x, normal.y, normal.z);
  165.                    
  166.  
  167.                 }
  168.             }
  169.         }
  170.  
  171.         glEnd();
  172.     }
  173. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement