int x1 = 10; int y1 = 40; int w1 = 280; int h1 = 120; boolean m1 = false; //whether box 1 moves int i1 = 1; //increment value when moving box1 int x2 = 10; int y2 = 60; int w2 = 40; int h2 = 80; boolean m2 = false; //whether box 2 moves int i2 = 1; //increment value when moving box2 int x3 = 10; int y3 = 74; int w3 = 10; int h3 = 52; boolean m3 = true; //whether box 3 moves int i3 = 2; //increment value when moving box3 void setup() { size(300, 200); framerate(60); } void draw() { background(255, 255, 255); stroke(0, 0, 0); rect(x1, y1, w1, h1); rect(x2, y2, w2, h2); rect(x3, y3, w3, h3); if(m3 && (x3 + w3 < x2 + w2)) //box3 can move and has not run into box2's right side { x3 = x3 + i3; } else if(m3 && !(x3 + w3 < x2 + w2)) //box3 can move and has run into box2's right side { m3 = false; m2 = true; } if((m2 && (x2 + w2 < x1 + w1)) //box2 can move and has not run into box1's right side && (m2 && (x2 < x3 ))) //box2 can move and has not run into box3's left side { x2 = x2 + i2; } else if(m2 && !(x2 < x3)) { //box2 can move and has run into box3's left side m2 = false; m3 = true; } }