Move up and down rectangle by mousepressed

Hi,

I have a FBox in Fisica (actually more but I simplified the code) that I need to move up or down when clicking in it. It’s meant to be a sliding door, so it represents opening and closing it. I now have a code that scales it, but I would like to replace the void(mousepressed) part with the moving up& down.

How can I do this?

Thanks,

import fisica.*;
FWorld world;

FBox rectangle1;

ArrayList<FBox> rectangles = new ArrayList<FBox>();

void setup() {
  size(1000,1000); 
  smooth();
  Fisica.init(this);
  world = new FWorld ();

  rectangle1 = new FBox (20,height/6);
  rectangle1.setPosition(width/3,height/12);
  rectangle1.setRestitution(1);
  rectangle1.setDamping(0.0);
  rectangle1.setStatic(true);
  rectangle1.setGrabbable(false);
  rectangle1.setStrokeWeight(1);
  rectangle1.setFill(174,174,211);
  rectangle1.setStroke(0);
  rectangles.add(rectangle1);
  world.add(rectangle1);
  
  }
  
  void draw() {
  background(255);
  world.draw();
  world.step();
  }
  
  
void mousePressed() {
  
FBody pressed = world.getBody(mouseX, mouseY);

   if (pressed == rectangle1) {
      if (mouseButton == LEFT ) {
      rectangle1.setHeight(rectangle1.getHeight()*1.1);
      }
      if (mouseButton == RIGHT ) {
      rectangle1.setHeight(rectangle1.getHeight()*0.9);
      }
    }
    
    //replace by if button = left, move up and if = right, move down
    // OR better, when button = left, alternate between up and down (open/close the door)
    
  }
    
1 Like

FBox is an FBody.

http://www.ricardmarxer.com/fisica/reference/fisica/FBox.html

When we look at all the things that it can do, these look potentially useful:

addForce, addImpulse, adjustPosition, adjustVelocity, … setForce, … setVelocity, …

although there are lots.