// 2D drag and turn // Written June 2006 by Jarek Rossignac float t; float t0; Mouse M = new Mouse(); // to track mouse int nr=4; // number of rectangles Rectangle R [] = new Rectangle [nr]; // table of rectangles pt C = new pt(); class Mouse { Mouse() {}; pt P = new pt(); // mouse position pt L = new pt(); // last mouse position vec V = new vec(); // mouse speed vec A = new vec(); // mouse acceleration float l = millis(); // time of last update void update() { L.setTo(P); P.setToMouse(); float t=millis()-l; // time since last update t=1; vec U = L.vecTo(P); U.div(t); // new speed U = LP/t; A.setTo(U); A.sub(V); A.div(t); // new acceleration A=(U-V)/t V.setTo(U); l=millis(); }; } class Rectangle { pt O = new pt(400,400); // origin vec V = new vec(0,0); // velocity vec R0= new vec(0,0); // picked position in body coord. float a=0.0, dadt=0.0; // angle and angular speed float h=30, w=70; // dimensions vec I=new vec(1,0), J=new vec(0,1); // local CS boolean picked = false; // true if being dragged color col; // color float Mass = .001; float Inertia = (h*h*h*w + h*w*w*w)/3 * (Mass/w/h); Rectangle (float px, float py, color pc) {O.x=px; O.y=py; col=pc;}; // create from position and color Rectangle (float px, float py, float pa, color pc) {a=pa; O.x=px; O.y=py; col=pc;}; // create from position and color void move () { println("moved"); O.addVec(V); this.updateBasis(); // rotates to have angle a }; // old mouse is now new mouse void updateBasis() {I.x=cos(a); I.y=sin(a); J.x=-sin(a); J.y=cos(a);};// reco,putes basis by rotating by a void paint() {pushMatrix(); translate(O.x,O.y); rotate(a); fill(col); if(picked) fill(100); rect(0,0,2*w,2*h); popMatrix(); }; boolean mouseIsIn() {pt N = new pt(); N.setToMouse(); vec D=O.vecTo(N); float x=dot(I,D); float y=dot(J,D); return((abs(x)=0; i--) { if (!found) {R[i].register(); found=R[i].picked;}; }; }