Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Body{
- public:
- Vector points[4][5];
- float resolution;
- float time[5];
- Vector position;
- Vector speed;
- float rotate;
- bool is_exist;// ha robbanás után leesik a földre akkor eltünik
- void boom(Vector vec) {
- rotate = 0;
- speed = vec;
- }
- void animate() {
- position.y =position.y+ speed.y*d_time - g_force / 2 * d_time*d_time;
- position.x += speed.x*d_time;
- position.z += speed.z*d_time;
- rotate += 20 * d_time;
- if (position.y < 0)
- is_exist = false;
- }
- Body() {
- resolution = 4;
- time[0] = 0;
- time[1] = 30;
- time[2] = 60;
- time[3] = 90;
- time[4] = 91;
- is_exist = true;
- points[0][0] = Vector(0, 3.5, 0);
- points[0][1] = Vector(-1.5, 4, 0);
- points[0][2] = Vector(-1, 4.5, 0);
- points[0][3] = Vector(1, 4.5, 0);
- points[0][4] = Vector(1.5, 4, 0);
- points[1][0] = Vector(0, 1, 1.5);
- points[1][1] = Vector(-3, 3, 1.5);
- points[1][2] = Vector(-2, 3.5, 1.5);
- points[1][3] = Vector(2, 3.5, 1.5);
- points[1][4] = Vector(3, 3, 1.5);
- points[2][0] = Vector(0, 1.5, 3.5);
- points[2][1] = Vector(-2.5, 2.5, 3.5);
- points[2][2] = Vector(-3, 3, 3.5);
- points[2][3] = Vector(3, 3, 3.5);
- points[2][4] = Vector(-2.5, 2.5, 3.5);
- points[3][0] = Vector(0, 2, 5);
- points[3][1] = Vector(-1, 3, 5);
- points[3][2] = Vector(-0.5, 4, 5);
- points[3][3] = Vector(0.5, 4, 5);
- points[3][4] = Vector(1, 3, 5);
- }
- //másolva a diából
- float B(int i, float t) {
- int n =4; // n deg polynomial = n+1 pts!
- float choose = 1;
- for (int j = 1; j <= i; j++) choose *= (float)(n - j + 1) / j;
- return choose * pow(t, i) * pow(1 - t, 5 - i);
- }
- Vector Bezier(float t,int number) {
- Vector rr=Vector(0, 0,0);
- for (int i = 0; i < 5; i++) rr =rr+ points[number][i] * B(i, t);
- return rr;
- }
- void material() {
- GLfloat kd[] = { 0.0, 1.0, 1.0, 1.0 };
- GLfloat ks[] = { 1.0, 0.5, 1.0, 1.0 };
- GLfloat ka[] = { 0.5, 0.5, 1, 1.0 };
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ka);
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, kd);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ks);
- glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20);
- }
- void draw() {
- material();
- glBegin(GL_QUADS);
- for (int i = 0; i < resolution-1; i++) {
- float current_time = i / (float)resolution;
- for (int j = 0; j < 4; j++)
- {
- Vector B_pont1=Bezier(current_time, j);
- Vector B_pont2 = Bezier(current_time+1.0/(float)resolution, j);
- Vector Bplus1_pont1= Bezier(current_time, j+1);
- Vector Bplus1_pont2 = Bezier(current_time + 1.0 / (float)resolution, j+1);
- Vector Bplus2_pont1 = Bezier(current_time, j + 2);
- Vector Bplus2_pont2 = Bezier(current_time + 1.0 / (float)resolution, j +2);
- Vector Bminus1_pont1 = Bezier(current_time, j -2);
- Vector Bminus1_pont2 = Bezier(current_time + 1.0 / (float)resolution, j -1);
- if (j > 2) {
- Bplus1_pont1= B_pont1;
- Bplus1_pont2= B_pont2;
- }
- if (j ==2) {
- Bplus2_pont1 = Bplus1_pont1;
- Bplus2_pont2 = Bplus1_pont2;
- }
- if (j == 0) {
- Bminus1_pont1 = B_pont1;
- Bminus1_pont2 = B_pont2;
- }
- Vector vi_1;
- Vector viplus1_1;
- Vector vi_2;
- Vector viplus1_2;
- 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;
- 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;
- Vector a0_1 = B_pont1;
- Vector a1_1 = vi_1;
- Vector a2_1 = (Bplus1_pont1 - a0_1)*(3.0f / ((time[j + 1] - time[i])*(time[j + 1] - time[j]))) -
- ((viplus1_1 + (vi_1*2.0f))*(1.0f / (time[j + 1] - time[j])));
- Vector a3_1 = (a0_1 - Bplus1_pont1)*(2.0f / ((time[j + 1] - time[j])*(time[j + 1] - time[j])*(time[j + 1] - time[j]))) +
- ((viplus1_1 + vi_1)*(1.0f / ((time[j + 1] - time[j])*(time[j + 1] - time[j]))));
- 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;
- 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;
- Vector a0_2 = B_pont2;
- Vector a1_2 = vi_1;
- Vector a2_2 = (Bplus1_pont2 - a0_2)*(3.0f / ((time[j + 1] - time[j])*(time[j + 1] - time[j]))) -
- ((viplus1_2 + (vi_2*2.0f))*(1.0f / (time[j + 1] - time[j])));
- Vector a3_2 = (a0_2 - Bplus1_pont2)*(2.0f / ((time[j + 1] - time[j])*(time[j + 1] - time[j])*(time[j + 1] - time[j]))) +
- ((viplus1_2 + vi_2)*(1.0f / ((time[j + 1] - time[j])*(time[j + 1] - time[j]))));
- for (float t = time[j]; t <= time[j + 1]; t += (time[j+1]-time[j])/ (float)resolution) {
- float tplus1 = t + (time[j + 1] - time[j]) / (float)resolution;
- Vector ft_1 = a3_1*((t - time[j])*(t - time[j])*(t - time[j])) +
- a2_1*((t - time[j])*(t - time[j])) +
- a1_1*(t - time[j]) +
- a0_1;
- Vector ft_2 = a3_2*((t - time[j])*(t - time[j])*(t - time[j])) +
- a2_2*((t - time[j])*(t - time[j])) +
- a1_2*(t - time[j]) +
- a0_2;
- Vector ft_3 = a3_2*((tplus1 - time[j])*(t - time[j])*(tplus1 - time[j])) +
- a2_2*((tplus1 - time[j])*(tplus1 - time[j])) +
- a1_2*(tplus1 - time[j]) +
- a0_2;
- Vector ft_4 = a3_1*((tplus1 - time[j])*(tplus1 - time[j])*(tplus1 - time[j])) +
- a2_1*((tplus1 - time[j])*(tplus1 - time[j])) +
- a1_1*(tplus1 - time[j]) +
- a0_1;
- //normálvektor számolása
- Vector normal = (ft_2 - ft_1)%(ft_4 - ft_1);
- glVertex3f(ft_1.x, ft_1.y, ft_1.z);
- glNormal3f(normal.x, normal.y, normal.z);
- glVertex3f(ft_2.x, ft_2.y, ft_2.z);
- glNormal3f(normal.x, normal.y, normal.z);
- glVertex3f(ft_4.x, ft_4.y, ft_4.z);
- glNormal3f(normal.x, normal.y, normal.z);
- glVertex3f(ft_3.x, ft_3.y, ft_3.z);
- glNormal3f(normal.x, normal.y, normal.z);
- }
- }
- }
- glEnd();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement