void ClothDemo::Update(float duration) { if (duration <= 0.0f) return; cloth.addForce(Vector3(0.0, -0.02, 0.0)); cloth.addWindForce(Vector3(0.5, 0, 0.2)); cloth.timeStep(duration); cloth.ballCollision(sphere); }
/* display method called each frame*/ void display(void) { // calculating positions ball_time++; ball_pos.f[2] = cos(ball_time/50.0)*7; cloth1.addForce(Vec3(0,-0.2,0)*TIME_STEPSIZE2); // add gravity each frame, pointing down cloth1.windForce(Vec3(0.5,0,0.2)*TIME_STEPSIZE2); // generate some wind each frame cloth1.timeStep(); // calculate the particle positions of the next frame cloth1.ballCollision(ball_pos,ball_radius); // resolve collision with the ball // drawing glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glDisable(GL_LIGHTING); // drawing some smooth shaded background - because I like it ;) glBegin(GL_POLYGON); glColor3f(0.8f,0.8f,1.0f); glVertex3f(-200.0f,-100.0f,-100.0f); glVertex3f(200.0f,-100.0f,-100.0f); glColor3f(0.4f,0.4f,0.8f); glVertex3f(200.0f,100.0f,-100.0f); glVertex3f(-200.0f,100.0f,-100.0f); glEnd(); glEnable(GL_LIGHTING); glTranslatef(0.0f, 0.25f, z); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); //glTranslatef(-6.5,6,-9.0f); // move camera out and center on the cloth //glRotatef(25,0,1,0); // rotate a bit to see the cloth from the side cloth1.drawShaded(myImage); // finally draw the cloth with smooth shading glPushMatrix(); // to draw the ball we use glutSolidSphere, and need to draw the sphere at the position of the ball glTranslatef(ball_pos.f[0],ball_pos.f[1],ball_pos.f[2]); // hence the translation of the sphere onto the ball position glColor3f(0.4f,0.8f,0.5f); glutSolidSphere(ball_radius-0.1,50,50); // draw the ball, but with a slightly lower radius, otherwise we could get ugly visual artifacts of cloth penetrating the ball slightly glPopMatrix(); glutSwapBuffers(); glutPostRedisplay(); }