// Box + Mouse = Relationship // Cadin Batrack 2.6.05 // www.cadinbatrack.com // Presented by The Pencil Farm // www.thepencilfarm.com // Try to figure out the relationship represented // by the interaction between the mouse and the box. // Scroll to the end of the code for the answer. // box values float posX = 50.; float posY = 50.; float angle; int boxSize = 10; // distance variables float dx,dy,delta; // the initial box state (unsquished) boolean squished = false; // two counters used to time if the mouse is inactive int inactiveCount = 0; int runningCount = 0; // to be used for the boxes random destination point float destinationX = 0; float destinationY = 0; void setup() { size(400,400); rectMode(CENTER); noStroke(); framerate(30); } void draw() { background(100); // get the distance between the mouse and the box dx = mouseX-posX; dy = mouseY-posY; delta = sqrt(dx*dx + dy*dy); // get the angle of the mouse (in relation to the box) angle = atan2(dy, dx); if (delta < 100 && (squished == false)) { // if the mouse is closer than 100px, and it is unsquished // set new coordinates to move the box away from the mouse posX = posX - (dx/3); posY = posY - (dy/4); // drawBox is the function that redraws the box. // It takes the color value of the box as an argument. // Here the color is based on how close the mouse is to the box drawBox(255 - int(delta*1.5)); } else if (squished ==true) { // if the box is squished // set the squished box properties and redraw angle = 45; boxSize= 15; drawBox(color(255,170,0)); } else if (inactiveCount > 80) { // if the box is not squished , and the mouse has been inactive for 80 cycles if (runningCount < 10) { // if the box has been running randomly for less than 10 cycles if (runningCount == 0) { // if the box just started running randomly // choose random coordinates within 255 px of the current position destinationX = random(posX-255,posX+255); destinationY = random(posY-255,posY+255); // these next two bits just check to see if the coordinates that // were chosen are off the stage. // If so, it chooses new ones. if(destinationX > 395) { destinationX = random(posX-255,posX-100); } else if(destinationX < 5) { destinationX = random(posX+100,posX+255); } if (destinationY > 395) { destinationY = random(posY-255,posY-100); } else if(destinationY < 5) { destinationY = random(posY+100,posY+255); } } // get the distance between the box and the random coordinates float dx2 = destinationX - posX; float dy2 = destinationY - posY; float delta2 = sqrt(dx2*dx2 + dy2*dy2); // set the new box position posX = posX + dx2/2; posY = posY + dy2/2; // redraw the box with a color value related to the distance to the destination drawBox(int(delta2/2) + 103); // increment the counter so we know how long the box has been running around runningCount++; } else { // if the box has been running for 10 cycles // reset the counters runningCount = 0; inactiveCount = 0; } } else { // if the box is not squished, and the mouse is moving, but not near the box // redraw it in the same position drawBox(103); } // increment the counter so we know how long the mouse has been inactive // this just counts every cycle and it gets reset if mouse movement is detected inactiveCount++; } // When the mouse is moved, reset the counter to 0 void mouseMoved() { inactiveCount = 0; } void mousePressed() { // When the mouse is pressed if (delta < 5) { // If the mouse is within the box if (squished == true){ // If the box is already squished // unsquish the box boxSize = 10; squished = false; } else { // If the box isn't already squished // squish that sucker squished = true; } } } void drawBox(color fillColor) { // a random number to be used to push the box back from the // edge if it starts to go out of bounds. // Using a random number just makes it harder to trap the box in a corner float randomJump = random(10,20); // These next two bits check if the box is going out of bounds // if so it pushes it back by a random number of pixels between 10 and 20 if (posX > 395) { posX = 400-randomJump; } else if (posX < 5 ) { posX = randomJump; } if (posY > 395) { posY = 400-randomJump; } else if (posY < 5 ) { posY = randomJump; } // translate the stage to the new coords translate(posX,posY); // set the box color fill(fillColor); //set the rotation rotate(angle); // draw the box (finally!) rect(0,0,boxSize,boxSize); } // The answer: // It's a cockroach! // It runs away and hides when you try to chase it. // If it doesn't see you for a while it comes out of hiding // and tries to make a run for it. // If you catch it you can squish it.