Making rects disappear on touch

float xOffset = 0.0; 
float yOffset = 0.0; 

int rectSize = 30;

int ellipseX = 90;
int ellipseY = 320;
int ellipseSize = 30;

int boxPosX = 250;
int boxPosY = 130;

int spacingX = 45;
int spacingY = 45;

boolean ellipseHovering = false;

void setup () {
	size (500, 350);
	
}

void draw() {
	background (0);
	
	 for (int x = 10; x < width; x = x + spacingX) {
	for (int y = 0; y < height-10; y = y + spacingY) {
	//	rectSize = 30;
		rect (x, y, rectSize, rectSize);
		if (ellipseRectDist < 10) {
	rectSize = 0;
	}
		
	}
	 }
	
	float ellipseRectDist = dist(x, y, ellipseX, ellipseY);
	
	
	
	fill(200, 200, 100);
	//rect (boxPosX, boxPosY, 50, 50);
	
	fill(255, 100, 100);
	ellipse(ellipseX, ellipseY, ellipseSize, ellipseSize);
	xOffset = mouseX-ellipseX; 
  yOffset = mouseY-ellipseY;
	
	//float boxRectDist = dist(rectX, rectY, boxPosX, boxPosY);
	
	//if (boxRectDist < 20) {
	//rectX = 90;
 //rectY = 320;
//	}
	
	if (mouseX < ellipseX + 15 && mouseX > ellipseX-15 && mouseY < ellipseY + 15 && mouseY > ellipseY - 15) {
		ellipseHovering = true;
	} else {
	ellipseHovering = false;	
	}
	
}

	void mouseDragged() {
  if (ellipseHovering) {
    ellipseX = mouseX-xOffset; 
    ellipseY = mouseY-yOffset; 
  }
	}


When I move the ellipses around, touching the rects I still don’t get a response. My goal is to make them disappear.

1 Like

Hello!
It would probably make more sense to use a class in this scenario, as it would make it way easier to handle.
Something like:

class box{
  PVector pos;
  PVector boxsize;
  box(PVector position, PVector sizeVec){
   pos = position;
   boxsize = sizeVec;
  }
boolean destroyed = false;
  void update(){
    if(mouseX >= pos.x && mouseX <= pos.x + boxsize.x){
      if(mouseY >= pos.y && mouseY <= pos.y + boxsize.y){
        destroyed = true; //Handle this however.
        //I'd probably just store each box inside of an array then cycle through that array to find any 'dead' boxes.
      }
    }
  }
  void display(){
    rect(pos.x,pos.y,boxsize.x,boxsize.y);
  }
}

Basically, create an array with a box object in each, then cycle through that array for any “dead” boxes.
You can move each PVector with a custom function or by calling it through the boxObject.pos type of thing.

2 Likes

this works (without a class)

The order of the things inside the for-loop was wrong.

You had this:

      rect (x, y, rectSize, rectSize);
		if (ellipseRectDist < 10) {
	rectSize = 0;
	}
		
	}
	 }
	
	float ellipseRectDist = dist(x, y, ellipseX, ellipseY);

(The last line is even outside the for loops)

But you need:

      float ellipseRectDist = dist(x, y, ellipseX, ellipseY);
      
      rectSize = 30;
      if (ellipseRectDist < 10) {
        rectSize = 0;
      }
      rect (x, y, rectSize, rectSize);

Hope this helps!

Please remember to use ctrl-t in processing so you get auto-format.

Warm regards,

Chrisir


Full Sketch



float xOffset = 0.0; 
float yOffset = 0.0; 

final int rectSizeBig = 30;
int rectSize = rectSizeBig;

float ellipseX = 90;
float ellipseY = 320;
int ellipseSize = 30;

int boxPosX = 250;
int boxPosY = 130;

int spacingX = 45;
int spacingY = 45;

boolean ellipseHovering = false;

// ---------------------------------------------------------------

void setup () {
  size (500, 350);
}

void draw() {
  background (0);

  for (int x = 10; x < width; x = x + spacingX) {
    for (int y = 0; y < height-10; y = y + spacingY) {

      float ellipseRectDist = dist(x+rectSizeBig/2, y+rectSizeBig/2, ellipseX, ellipseY);

      if (ellipseRectDist < 15) {
        rectSize = 0;
      } else {
        rectSize = rectSizeBig;
      }

      rect (x, y, rectSize, rectSize);
    }//for
  }//for

  //-------------

  fill(200, 200, 100);
  //rect (boxPosX, boxPosY, 50, 50);

  fill(255, 100, 100);
  ellipse(ellipseX, ellipseY, ellipseSize, ellipseSize);
  xOffset = mouseX-ellipseX; 
  yOffset = mouseY-ellipseY;

  //float boxRectDist = dist(rectX, rectY, boxPosX, boxPosY);

  //if (boxRectDist < 20) {
  //rectX = 90;
  //rectY = 320;
  //  }

  if (mouseX < ellipseX + 15 &&
    mouseX > ellipseX-15 && 
    mouseY < ellipseY + 15 && 
    mouseY > ellipseY - 15) {
    ellipseHovering = true;
  } else {
    ellipseHovering = false;
  }
}//draw()

// ---------------------------------------------------------------

void mouseDragged() {
  if (ellipseHovering) {
    ellipseX = mouseX-xOffset; 
    ellipseY = mouseY-yOffset;
  }
}
//

1 Like