class Cloth extends PhysicsObject{ int grid = 10; float[][] n; float[][] forces; int[][] e; float[] lengths; //------------------------------------------------ Cloth(float iradius, int res, float ix, float iy, float iz){ x = ix; y = iy; z = iz; radius = iradius; grid = res; friction = 0.95; n = new float[(int)sq(grid)][3]; forces = new float[(int)sq(grid)][3]; e = new int[(int)sq(grid)*2][2]; lengths = new float[(int)sq(grid)*2]; for(int i = 0 ; i 0.05 ){ forces[ i ][0] += -0.02*( obj.x-n[i][0] )*d; forces[ i ][1] += -0.02*( obj.y-n[i][1] )*d; forces[ i ][2] += -0.02*( obj.z-n[i][2] )*d; } } } //------------------------------------------------ void surfaceInteraction(PhysicsObject obj){ //apply forces for (int i=0; i < n.length; i++) { if(n[i][1]>obj.y) forces[i][1] += -(n[i][1]-obj.y); } } //------------------------------------------------ void gravityInteraction(PhysicsObject obj){ //apply forces for (int i=0; i < n.length; i++) { forces[i][1] += 2*((Gravity)obj).force; } //vy += ((Gravity)obj).force; } //------------------------------------------------ void frictionInteraction(PhysicsObject obj){ //apply forces vx *= obj.friction; vy *= obj.friction; vz *= obj.friction; } //------------------------------------------------ void update(){ //apply forces for (int i=0; i < n.length; i++) { n[ i ][0] += forces[ i ][0] + vx; n[ i ][1] += forces[ i ][1] + vy; n[ i ][2] += forces[ i ][2] + vz; forces[ i ][0] = 0f; forces[ i ][1] = 0f; forces[ i ][2] = 0f; } if(next!=null) next.update(); } //------------------------------------------------ void visualize(){ stroke(128,0,0); for (int i=0; i < e.length; i++) { line( n[ e[i][0] ][0], n[ e[i][0] ][1], n[ e[i][0] ][2], n[ e[i][1] ][0], n[ e[i][1] ][1], n[ e[i][1] ][2] ); } if(next!=null) next.visualize(); } }