//TODO //DONE //reverse order of drawing //anchor block //make boxes stick to sides after inequality void setup(){ size(500,500); reset(); frameRate(30); } Gift g;//= new Gift(105,105,40,0); //was 3 void draw(){ background(255); g.moveKids(); g.draw(); } flaot speedmul = 1; void reset(){ g= new Gift(mouseX,mouseY,random(20,40),3,true);//was 3 or 4 g.x = 250; g.y = 250; } void keyPressed(){ if(key == ' '){ reset(); } } void mousePressed(){mouseDragged();} void mouseDragged(){ g.x = mouseX; g.y = mouseY; speedmul = map(mouseY,500,0,.2,2); } class GiftHolder{ Gift g; int side;float rotspeed; GiftHolder(Gift g, int side, float rotspeed){ this.g = g; this.side = side; this.rotspeed = rotspeed; } void moveGift(Gift parent){ if(side == LEFT){ g.y -= rotspeed * speedmul; if(g.y + g.ysz < 0){ side = TOP; g.y = 0 - g.ysz; return; } if(g.y > parent.ysz){ side=BOTTOM; g.y = parent.ysz; return; } } if(side == RIGHT){ g.y += rotspeed * speedmul; if(g.y > parent.ysz){ side = BOTTOM; g.y = parent.ysz; return; } if(g.y + g.ysz < 0 ){ side = TOP; g.y = -g.ysz; return; } } if(side == TOP){ g.x += rotspeed * speedmul; if(g.x > parent.xsz){ side = RIGHT; g.x = parent.xsz; return; } if(g.x + g.xsz < 0){ side = LEFT; g.x = -g.xsz; return; } } if(side == BOTTOM){ g.x -= rotspeed * speedmul; if(g.x + g.xsz < 0){ side = LEFT; g.x = -g.xsz; return; } if(g.x > parent.xsz){ side = RIGHT; g.x = parent.xsz; return; } } } } class Gift { float x,y,z,xsz,ysz,ribwidth; color col, ribcol; float SPEEDRANGE = width / 50; float ROTRANGE = .3; float SIZEMIN = width / 12.0; float SIZEMAX = width / 6.0; float GRAV = .5; ArrayList kids = new ArrayList(); GiftHolder anchor = null; // String dir; Gift(float x, float y, float z, int kidCount, boolean hasAnchor){ if(hasAnchor){ anchor = new GiftHolder(new Gift(xsz,0,0,0,false),RIGHT,random(-3,3)); } this.x = x; this.y = y; xsz = z; //random(SIZEMIN,SIZEMAX); //println(width); ysz = z;// random(SIZEMIN,SIZEMAX); ribwidth = min(xsz,ysz)/4; col = color(random(128,255),random(128,255),random(128,255)); ribcol = color(random(128,255),random(128,255),random(128,255)); idCount = 0; for(int i = 0; i < kidCount ;i++){ kids.add(new GiftHolder(new Gift(xsz,0,random(SIZEMIN,SIZEMAX),kidCount-1,false),RIGHT,random(-6,6))); } } void moveKids(){ if(anchor != null){ anchor.moveGift(this); } for(GiftHolder kid : kids){ kid.moveGift(this); kid.g.moveKids(); } } void draw(){ rectMode(CORNER); ellipseMode(CENTER); fill(col); noStroke(); pushMatrix(); float cx = x ; float cy = y ; translate(cx,cy); //rotate(r); if(anchor != null){ translate(-anchor.g.x,-anchor.g.y); //anchor.g.draw(); } for(GiftHolder kid : kids){ kid.g.draw(); } rect(0,0,xsz,ysz); fill(ribcol); rect((xsz-ribwidth)/2,0,ribwidth,ysz); rect(0,(ysz-ribwidth)/2,xsz,ribwidth); ellipse(ysz/2,0,ribwidth*2,ribwidth*2); popMatrix(); } }