Home

Physics simulations

Install fisica

Go to the fisica website and download the ZIP (look for the "Download" section on the front page, left side). Create a folder called "libraries" in your "sketchbook" folder (where all your sketches are), and unzip the fisica library there. Restart Processing.

After installing, you can find fisica examples by clicking the "File" menu, then "Examples", then scrolling to the bottom an expanding the item "Contributed Libraries" and then expanding "Fisica".

Check out the ficisa manual and the Processing Wiki.

Terms

Examples

A circle in a world with gravity

import fisica.*;

FWorld world;
FCircle body;

void setup()
{
  size(800, 600);
  
  Fisica.init(this);
  
  world = new FWorld();
  
  body = new FCircle(50);
  body.setPosition(400, 100);
  body.setFill(255, 0, 0);
  body.setNoStroke();
  
  world.add(body);
  
  // possibly change the gravity (x and y components)
  //world.setGravity(0, 300);
}

void draw()
{
  background(255);
  world.step();
  world.draw();
}

Adding a floor

import fisica.*;

FWorld world;
FCircle body;
FBox floor;

void setup()
{
  size(800, 600);
  
  Fisica.init(this);
  
  world = new FWorld();
  
  body = new FCircle(50);
  body.setPosition(400, 100);
  body.setFill(255, 0, 0);
  body.setNoStroke();
  
  // make the body bouncy!
  body.setRestitution(0.75);
  
  floor = new FBox(800, 50);
  // by default, position mode is CENTER
  floor.setPosition(400, 575);
  floor.setFill(0);
  floor.setNoStroke();
  floor.setStatic(true);
  
  world.add(body);
  world.add(floor);
}

void draw()
{
  background(255);
  world.step();
  world.draw();
}

A slanted floor, with friction

import fisica.*;

FWorld world;
FCircle body;
FPoly floor;

void setup()
{
  size(800, 600);
  
  Fisica.init(this);
  
  world = new FWorld();
  
  body = new FCircle(50);
  body.setPosition(50, 100);
  body.setFill(255, 0, 0);
  body.setNoStroke();
  
  // make the body NOT bouncy!
  body.setRestitution(0);
  
  // are we moving through air, honey, or a vacuum?
  body.setDamping(0.5);
  
  floor = new FPoly();
  floor.vertex(0, 300);
  floor.vertex(0, 0);
  floor.vertex(300, 200);
  floor.vertex(800, 200);
  floor.vertex(800, 300);

  floor.setPosition(0, 300);
  floor.setFill(0);
  floor.setNoStroke();
  floor.setStatic(true);
  
  // how much friction?
  floor.setFriction(0.2);
  
  world.add(body);
  world.add(floor);
}

void draw()
{
  background(255);
  world.step();
  // try this instead of world.draw()
  world.drawDebug();
}

A pendulum

import fisica.*;

FWorld world;
FCircle top;
FCircle bob;
FRevoluteJoint rod;

int tick = 1;
boolean tickedLeft = true;
boolean tickedRight = false;

void setup()
{
  size(800, 600);
  
  Fisica.init(this);
  
  world = new FWorld();
  
  top = new FCircle(10);
  top.setPosition(400, 100);
  top.setFill(0);
  top.setNoStroke();
  top.setStatic(true);
  
  bob = new FCircle(50);
  bob.setPosition(0, 100);
  bob.setFill(255, 0, 0);
  bob.setNoStroke();
  bob.setDamping(0);
  
  rod = new FRevoluteJoint(top, bob, 400, 100);

  world.add(top);
  world.add(bob);
  world.add(rod);
}

void draw()
{
  background(255);
  world.step();
  world.draw();
  
  if(abs(bob.getVelocityX()) < 4)
  {
    if(bob.getX() > 400 && !tickedRight)
    {
      tick++;
      tickedRight = true;
      tickedLeft = false;
    }
    if(bob.getX() < 400 && !tickedLeft)
    {
      tick++;
      tickedRight = false;
      tickedLeft = true;
    }
  }
  
  textSize(32);
  fill(0);
  text(tick, 700, 50);
}

Slingshot (i.e., spring + letting go)

import fisica.*;

FWorld world;
FCircle top;
FCircle bob;
FDistanceJoint spring;

void setup()
{
  size(800, 600);
  
  Fisica.init(this);
  
  world = new FWorld();
  
  top = new FCircle(10);
  top.setPosition(400, 300);
  top.setFill(0);
  top.setNoStroke();
  top.setStatic(true);
  top.setSensor(true);
  
  bob = new FCircle(50);
  bob.setPosition(400, 330);
  bob.setFill(255, 0, 0);
  bob.setNoStroke();
  bob.setDamping(0);
  
  spring = new FDistanceJoint(top, bob);
  spring.setDamping(0.1);
  spring.setFrequency(0.8);
  spring.setLength(30);

  world.add(top);
  world.add(bob);
  world.add(spring);
}

void draw()
{
  background(255);
  world.step();
  world.drawDebug();
  
  // let go!
  if(bob.isTouchingBody(top))
  {
    world.remove(spring);
  }
}

Angry Birds, sort of

import fisica.*;

FWorld world;
FBox bird;
FBox pig;
FBox box1;
FBox box1a;
FBox box1b;
FBox box1c;
FBox box2;
FBox box3;

void setup()
{
  size(900, 500);

  Fisica.init(this);

  world = new FWorld();

  bird = new FBox(50, 50);
  PImage birdimg = loadImage("bird.png");
  bird.attachImage(birdimg);
  bird.setDensity(300);

  pig = new FBox(50, 50);
  PImage pigimg = loadImage("pig.png");
  pig.attachImage(pigimg);

  box1 = new FBox(30, 200);
  box1.setFill(160, 94, 24);
  box1.setNoStroke();
  
  box1a = new FBox(30, 60);
  box1a.setFill(160, 94, 24);
  box1a.setNoStroke();
  
  box1b = new FBox(30, 60);
  box1b.setFill(160, 94, 24);
  box1b.setNoStroke();
  
  box1c = new FBox(30, 60);
  box1c.setFill(160, 94, 24);
  box1c.setNoStroke();
  
  box2 = new FBox(30, 200);
  box2.setFill(160, 94, 24);
  box2.setNoStroke();
  
  box3 = new FBox(200, 30);
  box3.setFill(250, 188, 121);
  box3.setNoStroke();

  bird.setPosition(100, 250);
  box1.setPosition(700, 360);
  box1a.setPosition(700, 360);
  box1b.setPosition(700, 420);
  box1c.setPosition(700, 480);
  box2.setPosition(850, 360);
  box3.setPosition(770, 260);
  pig.setPosition(780, 460);
  
  world.setEdges();
  
  world.add(bird);
  world.add(box1);
  world.add(box2);
  world.add(box3);
  world.add(pig);
}

void draw()
{
  background(255);
  
  world.step();
  world.draw();
  
  if(pig.isTouchingBody(box3))
  {
    noLoop();
    textSize(32);
    fill(0);
    text("Win!", 420, 200);
  }
  
  if(bird.isTouchingBody(box1))
  {
    world.remove(box1);
    world.add(box1a);
    world.add(box1b);
    world.add(box1c);
  }
}

void mouseClicked()
{
  // click the bird to throw it again
  if(abs(mouseX - bird.getX()) < 75 && abs(mouseY - bird.getY()) < 75)
  {
    // make sure we undo noLoop() from above, if it ever occurred
    loop();
    bird.setPosition(100, 250);
    bird.setVelocity(700, 0);
  }
}
CSE 1211 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.