Change world's gravity using keyPressed (Fisica)

Hi,

I am using Fisica library in which I set gravity (gx, gy). I would like to be able to change the value of the gravity using keyPressed in a range of -100 to 100 so I can control the motion of balls from left to right on the screen. I don’t know hot to reference a default gravity of say 0 and write that if the key is pressed you add 50 to this value (new value = current + 50).

Here’s a simplification of my code:

import fisica.*;
FWorld world;

void setup() {
  size(1500, 2000); 
  smooth();

  Fisica.init(this);
  world = new FWorld ();
  world.setEdges();
    if (keyPressed == true) {
     if (key == 'a'){
        world.setGravity(-100, 100);
     }
     if (key == 'd'){
       world.setGravity(100, 100);
     }
        if (key == 'w'){
       world.setGravity(0, 0);
        }
     if (key == 's'){
       world.setGravity(0, 200);
     }
     else{
       world.setGravity(0, 100);
    }
    }

Thanks!!

Hello,

Your example won’t run since setup() only executes once:
https://processing.org/reference/setup_.html

And there are some errors in logic\flow in the if\else statement.

Consider this:
https://www.processing.org/reference/keyPressed_.html

Keep at it!

I finally figured it out! I had to put world.setGravity in the void draw() section instead of setup. Also, the syntax of the ‘if’ was wrong.

Here is y code if anyone needs (you can move bubbles around using gravity changes with the a-w-s-d keys)

import fisica.*;
FWorld world;
FCircle circle1;
FCircle circle2;
FCircle circle3;
FCircle circleButton1;
FCircle circleButton2;
FCircle circleButton3;
FBox rectangle1;
FBox rectangle2;
FBox rectangle3;
FBox rectangle4;
FBox rectangle5; 
FBox boundary;
boolean numberReached;
PFont f;

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> circles = new ArrayList<FCircle>();

//---------reservoir count--------------------------------
  
//public java.util.ArrayList getBodies (float x,float y){
// if (contactEnded(FContact c)) {
//   if (!c.getBody1().isStatic()) {
//    FCircle b = (FCircle)c.getBody1();
//    if (b.getSize()>5) {
//      b.setSize(b.getSize()*0.9);
//    }
//   }
//  } 

//   if (!c.getBody2().isStatic()) {
//    FCircle b = (FCircle)c.getBody2();
//    if (b.getSize()>5) {
//      b.setSize(b.getSize()*0.9);
//    }
//  }
//}
//-------------------------------------------------------

void setup() {
  size(1500, 2000); 
  smooth();
  f=createFont("Arial", 16, true);

  Fisica.init(this);
  world = new FWorld ();
  world.setEdges();
  //world.setGravity(gx,gy);

//--------------------------------------------------------      
  numberReached = false;
//---rectangles-------------------------------------------

  rectangle1 = new FBox (800, 50);
  rectangle1.setPosition(750, 700);
  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 (400, 100);
  rectangle2.setPosition(200, height-400);
  rectangle2.setVelocity(random(50, 250), random(50, 250));
  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 (550, 100);
  rectangle3.setPosition(725, height-400);
  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 (300, 30);
  rectangle4.setPosition(850, height-800);
  rectangle4.setVelocity(random(50, 250), random(50, 250));
  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 (250, 30);
  rectangle5.setPosition(850, height-1000);
  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);

//------circle buttons----------------------------------------

  circleButton1 = new FCircle(50);
  circleButton1.setPosition(1000/6, height-1800);
  circleButton1.setStatic(true);
  circleButton1.setGrabbable(false);
  circleButton1.setNoFill();
  circleButton1.setStroke(0);
  circleButton1.setGrabbable(false);
  circleButton1.setStrokeWeight(3);
  world.add(circleButton1);

  circleButton2 = new FCircle(50);
  circleButton2.setPosition(1000/2, height-1800);
  circleButton2.setStatic(true);
  circleButton2.setGrabbable(false);
  circleButton2.setNoFill();
  circleButton2.setStroke(0);
  circleButton2.setStrokeWeight(3);
  circleButton2.setGrabbable(false);
  world.add(circleButton2);

  circleButton3 = new FCircle(50);
  circleButton3.setPosition(1000 - 1000/6, height-1800);
  circleButton3.setStatic(true);
  circleButton3.setGrabbable(false);
  circleButton3.setNoFill();
  circleButton3.setStroke(0);
  circleButton3.setStrokeWeight(3);
  circleButton3.setGrabbable(false);
  world.add(circleButton3);

  //----------separation---text---------------------------------
  FBox boundary = new FBox(500, 2000);
  boundary.setPosition(1250,1000);
  boundary.setStatic(true);
  boundary.setFill(0);
  boundary.setStroke(0);
  world.add(boundary);
}

//------change gravity-----------------------------------
    void keyPressed(){
      if (key =='a'){
        gx = (-400); 
        gy = (100);
      }
         else{
        gx=0;
        gy=100;
     }
     
      if (key =='d'){
        gx = (400); 
        gy = (100);
      }
         else{
        gx=0;
        gy=100;
     }
     //if (key == 'd'){
     //  world.setGravity(400, 100);
     //}
     //   if (key == 'w'){
     //  world.setGravity(0, 0);
     //   }
     //if (key == 's'){
     //  world.setGravity(0, 400);
     //}
 
    }

//-----------------------------------------------------------
void draw() {
  background(255);
  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);
      }
  }
  
//add circles automatically
  if (frameCount % 200 == 0) {
    FCircle circle1 = new FCircle(30);
    circle1.setPosition(1000/6, height-1800);
    circle1.setRotation(random(TWO_PI));
    circle1.setVelocity(0, random(200, 500));
    circle1.setNoFill();
    circle1.setStroke(0);
    circle1.setGrabbable(false);
    circle1.setStrokeWeight(2);
    circles1.add(circle1);
    world.add(circle1);

    FCircle circle2 = new FCircle(80);
    circle2.setPosition(1000/2, height-1800);
    circle2.setRotation(random(TWO_PI));
    circle2.setVelocity(0, random(200, 500));
    circle2.setNoFill();
    circle2.setStroke(0);
    circle2.setStrokeWeight(2);
    circle2.setGrabbable(false);
    circles2.add(circle2);
    world.add(circle2);

    FCircle circle3 = new FCircle(20);
    circle3.setPosition(1000 - 1000/6, height-1800);
    circle3.setRotation(random(TWO_PI));
    circle3.setVelocity(0, random(200, 500));
    circle3.setNoFill();
    circle3.setStroke(3);
    circle3.setStrokeWeight(6);
    rectangle1.setGrabbable(false);
    world.add(circle3);
  }
  world.draw();
  world.step();
}

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

void mousePressed() {
  
  //add circles manually
  FBody pressed = world.getBody(mouseX, mouseY);
  if (pressed == circleButton1) {
    FCircle circle1 = new FCircle(30);
    circle1.setPosition(1000/6, height-1800);
    circle1.setRotation(random(TWO_PI));
    circle1.setVelocity(0, random(200, 500));
    circle1.setNoFill();
    circle1.setStroke(0);
    circle1.setGrabbable(false);
    circle1.setStrokeWeight(2);
    circles1.add(circle1);
    world.add(circle1);
  }

  if (pressed == circleButton2) {
    FCircle circle2 = new FCircle(80);
    circle2.setPosition(1000/2, height-1800);
    circle2.setRotation(random(TWO_PI));
    circle2.setVelocity(0, random(200, 500));
    circle2.setNoFill();
    circle2.setStroke(0);
    circle2.setStrokeWeight(2);
    circle2.setGrabbable(false);
    circles2.add(circle2);
    world.add(circle2);
  }
  
    if (pressed == circleButton3) {
    FCircle circle3 = new FCircle(20);
    circle3.setPosition(1000 - 1000/6, height-1800);
    circle3.setRotation(random(TWO_PI));
    circle3.setVelocity(0, random(200, 500));
    circle3.setNoFill();
    circle3.setStroke(3);
    circle3.setStrokeWeight(6);
    rectangle1.setGrabbable(false);
    world.add(circle3);
  }
  
  //scale rectangles
  if (pressed == rectangle5) {
  if (mouseButton == LEFT ){
  rectangle5.setWidth(rectangle5.getWidth()*1.1);
  }
  if (mouseButton == RIGHT ){
  rectangle5.setWidth(rectangle5.getWidth()*0.9);
  }
  }
    if (pressed == rectangle4) {
  if (mouseButton == LEFT ){
  rectangle4.setWidth(rectangle4.getWidth()*1.1);
  }
  if (mouseButton == RIGHT ){
  rectangle4.setWidth(rectangle4.getWidth()*0.9);
  }
  }
  
  if (pressed == rectangle3) {
  if (mouseButton == LEFT ){
  rectangle3.setWidth(rectangle3.getWidth()*1.1);
  }
  if (mouseButton == RIGHT ){
  rectangle3.setWidth(rectangle3.getWidth()*0.9);
  }
  }
  if (pressed == rectangle2) {
  if (mouseButton == LEFT ){
  rectangle2.setWidth(rectangle2.getWidth()*1.1);
  }
  if (mouseButton == RIGHT ){
  rectangle2.setWidth(rectangle2.getWidth()*0.9);
  }
  }
  
  if (pressed == rectangle1) {
  if (mouseButton == LEFT ){
  rectangle1.setWidth(rectangle1.getWidth()*1.1);
  }
  if (mouseButton == RIGHT ){
  rectangle1.setWidth(rectangle1.getWidth()*0.9);
  }
  }
  
}
1 Like

It’s even better to put key handling code in the keyPressed() method, similar to how you’ve put mouse functionality in mousePressed() method.