//2D Physics Library //by Michael Chang, UCLA //http://users.design.ucla.edu/~mflux //May 21, 2005 // I won't care to explain my physics here, but please feel free to poke around. // It's not difficult. boolean WORLDBORDER=true; float WORLDBORDERBUFFER=0; float FRICTION=0.98; vec GRAVITY=new vec(0,.4); class mass { vec p; //spatial position vec v; //velocity vector vec a; //acceleration vector mass cm[]; float sl[]; float sk[]; float x() { return p.x; } float y() { return p.y; } mass() { p=new vec(); v=new vec(); a=new vec(); } mass(mass m) { this.p=new vec(m.p); this.v=new vec(m.v); this.a=new vec(m.a); if(cm==null) return; cm=new mass[m.cm.length]; for(int i=0;iwidth+buff) { p.x=width+buff; // vec nextV=new vec(p); // nextV.add(v); // sfSingle(this,new mass(nextV),0,-.5); } if(p.x<-buff) { p.x=-buff; // vec nextV=new vec(p); // nextV.add(v); // sfSingle(this,new mass(nextV),0,-.5); } if(p.y>height+buff) { p.y=height+buff; // vec nextV=new vec(p); // nextV.add(v); // sfSingle(this,new mass(nextV),0,-.5); } if(p.y<-buff) { p.y=-buff; // vec nextV=new vec(p); // nextV.add(v); // sfSingle(this,new mass(nextV),0,-.5); } } void connectSpringTo(mass m,float springLength,float springConstant) { if(m==null) return; if(cm==null) cm=new mass[0]; mass temp[]=new mass[cm.length+1]; System.arraycopy(cm,0,temp,0,cm.length); temp[cm.length]=m; cm=temp; if(sl==null) this.sl=new float[0]; this.sl=append(sl,springLength); if(sk==null) this.sk=new float[0]; this.sk=append(sk,springConstant); } void connectSpringTo(mass m,float k) { float d=dist(this.p,m.p); connectSpringTo(m,d,k); } void updateSprings() { if(cm==null) return; for(int i=0;i