Object on-off by clicking area

Hi,

I have a code made of rectangles that form the «walls» of a plan. I want to add doors (the same rectangles but they would appear/disappear when I click on the sill). I am using the boxes from the fisica library.

How could I do this simply?

Thanks,

M

1 Like

Try sharing your code so it is easier to visualize.

1 Like

Here it is! It’s a bit long because I have many rectangles but basically what I would like is to replace scaling the rectangle (mousepressed) by turning them on/off.

Thanks!

import fisica.*;
FWorld world;
FCircle circle1;
FCircle circle2;
FCircle circle3;
FCircle circle4;

FCircle circleButton1;
FCircle circleButton2;
FCircle circleButton3;
FCircle circleButton4;
FBox rectangle1;
FBox rectangle2;
FBox rectangle3;
FBox rectangle4;
FBox rectangle5;
FBox rectangle6;
FBox rectangle7;
FBox rectangle8;
FBox rectangle9;
FBox rectangle10;

int gx = 0; // horizontal gravity;
int gy= 0; // vertical gravity;

ArrayList<FBox> rectangles = new ArrayList<FBox>();
ArrayList<FCircle> circles1 = new ArrayList<FCircle>();
ArrayList<FCircle> circles2 = new ArrayList<FCircle>();
ArrayList<FCircle> circles3 = new ArrayList<FCircle>();
ArrayList<FCircle> circles4 = new ArrayList<FCircle>();
ArrayList<FCircle> circles = new ArrayList<FCircle>();

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

void setup() {
  size(1000,1000); 
  smooth();
  Fisica.init(this);
  world = new FWorld ();
  world.setEdges();
  world.setGravity(-300,0);

  // VERTICAL DOORS
  rectangle1 = new FBox (20,height/3);
  rectangle1.setPosition(width/3,height/6);
  //rectangle1.setVelocity(random(50, 250), random(50, 250));
  rectangle1.setRestitution(1);
  rectangle1.setDamping(0.0);
  rectangle1.setStatic(true);
  rectangle1.setGrabbable(false);
  rectangle1.setStrokeWeight(3);
  rectangle1.setFill(0);
  rectangle1.setStroke(0);
  rectangles.add(rectangle1);
  world.add(rectangle1);
  
  rectangle2 = new FBox (20,height/3);
  rectangle2.setPosition((2*width/3),(height/6));
  rectangle2.setRestitution(1);
  rectangle2.setDamping(0.0);
  rectangle2.setStatic(true);
  rectangle2.setGrabbable(false);
  rectangle2.setStrokeWeight(3);
  rectangle2.setFill(0);
  rectangle2.setStroke(0);
  rectangles.add(rectangle2);
  world.add(rectangle2);
  
  rectangle3 = new FBox (20,height/3);
  rectangle3.setPosition(width/3,height/2);
  rectangle3.setVelocity(random(50, 250), random(50, 250));
  rectangle3.setRestitution(1);
  rectangle3.setDamping(0.0);
  rectangle3.setStatic(true);
  rectangle3.setGrabbable(false);
  rectangle3.setStrokeWeight(3);
  rectangle3.setFill(0);
  rectangle3.setStroke(0);
  rectangles.add(rectangle3);
  world.add(rectangle3);
  
  rectangle4 = new FBox (20,height/3);
  rectangle4.setPosition(2*width/3,height/2);
  rectangle4.setRestitution(1);
  rectangle4.setDamping(0.0);
  rectangle4.setStatic(true);
  rectangle4.setGrabbable(false);
  rectangle4.setStrokeWeight(3);
  rectangle4.setFill(0);
  rectangle4.setStroke(0);
  rectangles.add(rectangle4);
  world.add(rectangle4);
  
  rectangle5 = new FBox (20,5*height/6);
  rectangle5.setPosition(width/3,833.33);
  rectangle5.setVelocity(random(50, 250), random(50, 250));
  rectangle5.setRestitution(1);
  rectangle5.setDamping(0.0);
  rectangle5.setStatic(true);
  rectangle5.setGrabbable(false);
  rectangle5.setStrokeWeight(3);
  rectangle5.setFill(0);
  rectangle5.setStroke(0);
  rectangles.add(rectangle5);
  world.add(rectangle5);
  
  //HORIZONTAL DOORS
  
  rectangle6 = new FBox (width/3,20);
  rectangle6.setPosition(width/2,height/3);
  rectangle6.setVelocity(random(50, 250), random(50, 250));
  rectangle6.setRestitution(1);
  rectangle6.setDamping(0.0);
  rectangle6.setStatic(true);
  rectangle6.setGrabbable(false);
  rectangle6.setStrokeWeight(3);
  rectangle6.setFill(0);
  rectangle6.setStroke(0);
  rectangles.add(rectangle6);
  world.add(rectangle6);
  
  rectangle7 = new FBox (width/3, 20);
  rectangle7.setPosition(width/6,2*height/3);
  rectangle7.setVelocity(random(50, 250), random(50, 250));
  rectangle7.setRestitution(1);
  rectangle7.setDamping(0.0);
  rectangle7.setStatic(true);
  rectangle7.setGrabbable(false);
  rectangle7.setStrokeWeight(3);
  rectangle7.setFill(0);
  rectangle7.setStroke(0);
  rectangles.add(rectangle7);
  world.add(rectangle7);

  rectangle8 = new FBox (width/3, 20);
  rectangle8.setPosition(width/2,2*height/3);
  rectangle8.setVelocity(random(50, 250), random(50, 250));
  rectangle8.setRestitution(1);
  rectangle8.setDamping(0.0);
  rectangle8.setStatic(true);
  rectangle8.setGrabbable(false);
  rectangle8.setStrokeWeight(3);
  rectangle8.setFill(0);
  rectangle8.setStroke(0);
  rectangles.add(rectangle8);
  world.add(rectangle8);
  
  //WALL (NO MOUSECLICK)
  
  rectangle9 = new FBox (width/3, 20);
  rectangle9.setPosition(833.33,666.66);
  rectangle9.setVelocity(random(50, 250), random(50, 250));
  rectangle9.setRestitution(1);
  rectangle9.setDamping(0.0);
  rectangle9.setStatic(true);
  rectangle9.setGrabbable(false);
  rectangle9.setStrokeWeight(3);
  rectangle9.setFill(0);
  rectangle9.setStroke(0);
  rectangles.add(rectangle9);
  world.add(rectangle9);
  
  //circle buttons ------------------------
  
  circleButton1 = new FCircle(20);
  circleButton1.setPosition(width/2,40);
  circleButton1.setStatic(true);
  circleButton1.setGrabbable(false);
  circleButton1.setFill(0);
  circleButton1.setStroke(0);
  circleButton1.setGrabbable(false);
  circleButton1.setStrokeWeight(3);
  world.add(circleButton1);
  
  circleButton2 = new FCircle(20);
  circleButton2.setPosition(40, height-40);
  circleButton2.setStatic(true);
  circleButton2.setGrabbable(false);
  circleButton2.setFill(0);
  circleButton2.setStroke(0);
  circleButton2.setStrokeWeight(3);
  circleButton2.setGrabbable(false);
  world.add(circleButton2);
  
 }         //end of voidsetup-----------------------------------
 
  void draw() {
  background(255);
  world.draw();
  world.step();
  
    //gravity control-------------------------------
  world.setGravity(gx, gy);

  if (keyPressed == true) {
    if (key == 'a') {
      gx = (-400); 
      gy = (100);
    }

    if (key == 'd') {
      gx = (400); 
      gy = (100);
    }

    if (key == 's') {
      gx = (0); 
      gy = (400);
    }

    if (key == 'w') {
      gx = (0); 
      gy = (-400);
    }
  } else {
    gx=-1;
    gy=0;
  }
  
  //progression jeu----------------------------
  if (frameCount % 20 == 0) {
    FCircle circle1 = new FCircle(random(4,20));
    circle1.setPosition(40,40);
    circle1.setRotation(random(TWO_PI));
    circle1.setVelocity(0, random(200, 500));
    circle1.setNoFill();
    circle1.setStroke(0);
    circle1.setGrabbable(false);
    circle1.setStrokeWeight(random(2,5));
    circles1.add(circle1);
    world.add(circle1);
  }
  
   if (frameCount % 20 == 0) {
    //ENTRÉE BAS
    FCircle circle1 = new FCircle(random(4,20));
    circle1.setPosition(width-40,height-40);
    circle1.setRotation(random(TWO_PI));
    circle1.setVelocity(0, random(-200, -500));
    circle1.setNoFill();
    circle1.setStroke(0);
    circle1.setGrabbable(false);
    circle1.setStrokeWeight(random(2,5));
    circles1.add(circle1);
    world.add(circle1);
  }      
}          //end of void draw------------------------

  void mousePressed() {
  
//buttons to add circles
FBody pressed = world.getBody(mouseX, mouseY);
   
    if (pressed == circleButton1) {
    FCircle circle1 = new FCircle(5);
    circle1.setPosition(width/2,40);
    circle1.setRotation(random(TWO_PI));
    circle1.setVelocity(0, random(200, 500));
    circle1.setNoFill();
    circle1.setStroke(0);
    circle1.setGrabbable(false);
    circle1.setStrokeWeight(4);
    circles1.add(circle1);
    world.add(circle1);
  }
  
    if (pressed == circleButton2) {
    FCircle circle2 = new FCircle(20);
    circle2.setPosition(166,height-166);
    circle2.setRotation(random(TWO_PI));
    circle2.setVelocity(0, random(200, 500));
    circle2.setNoFill();
    circle2.setStroke(0);
    circle2.setStrokeWeight(1);
    circle2.setGrabbable(false);
    circles2.add(circle2);
    world.add(circle2);
  }
  
  //scaling the vertical rectangles
  
   if (pressed == rectangle1) {
      if (mouseButton == LEFT ) {
      rectangle1.setHeight(rectangle1.getHeight()*1.1);
      }
      if (mouseButton == RIGHT ) {
      rectangle1.setHeight(rectangle1.getHeight()*0.9);
      }
    }
    
     if (pressed == rectangle2) {
      if (mouseButton == LEFT ) {
      rectangle2.setHeight(rectangle2.getHeight()*1.1);
      }
      if (mouseButton == RIGHT ) {
      rectangle2.setHeight(rectangle2.getHeight()*0.9);
      }
    }
    
     if (pressed == rectangle3) {
      if (mouseButton == LEFT ) {
      rectangle3.setHeight(rectangle3.getHeight()*1.1);
      }
      if (mouseButton == RIGHT ) {
      rectangle3.setHeight(rectangle3.getHeight()*0.9);
      }
    }
    
     if (pressed == rectangle4) {
      if (mouseButton == LEFT ) {
      rectangle4.setHeight(rectangle4.getHeight()*1.1);
      }
      if (mouseButton == RIGHT ) {
      rectangle4.setHeight(rectangle4.getHeight()*0.9);
      }
    }
    
     if (pressed == rectangle5) {
      if (mouseButton == LEFT ) {
      rectangle5.setHeight(rectangle5.getHeight()*1.1);
      }
      if (mouseButton == RIGHT ) {
      rectangle5.setHeight(rectangle5.getHeight()*0.9);
      }
    }
    
 //scaling the horizontal rectangles
 
  if (pressed == rectangle6) {
    if (mouseButton == LEFT ) {
      rectangle6.setWidth(rectangle6.getWidth()*1.1);
    }
    if (mouseButton == RIGHT ) {
      rectangle6.setWidth(rectangle6.getWidth()*0.9);
      }
    }
    
     if (pressed == rectangle7) {
    if (mouseButton == LEFT ) {
      rectangle7.setWidth(rectangle7.getWidth()*1.1);
    }
    if (mouseButton == RIGHT ) {
      rectangle7.setWidth(rectangle7.getWidth()*0.9);
      }
    }
    
     if (pressed == rectangle8) {
    if (mouseButton == LEFT ) {
      rectangle8.setWidth(rectangle8.getWidth()*1.1);
    }
    if (mouseButton == RIGHT ) {
      rectangle8.setWidth(rectangle8.getWidth()*0.9);
      }
    }
    
    }
    
    void keyPressed(){
    if(key=='p'||key=='P')
            saveFrame("capture3-######.png"); 
}
1 Like

So basically replace this section

Sorry, I don’t have experience with libraries maybe someone else can solve it.

@MCGFP I believe (untested) that one approach is to remove the body from the world.

http://www.ricardmarxer.com/fisica/reference/fisica/FWorld.html#remove(fisica.FBody)

If you want, you can getBody first, save the object, remove it from the world, then add it back later. In your case, you already have variables like rectangle1, so you can just world.remove(rectangle1) and later world.add(rectangle1) (untested).

But you can’t “hide” things – that I am aware of.