Accessing dynamically created bodies

This example cycles through all bodied, get a reference of each and deletes them but it can be used to operate on existing bodies in general.

//import the physics library
import fisica.*;
 
//declare a global variable: our newtonian world
FWorld world;
 
void setup() {
  //sketch size
  size(800, 600);
 
  //initialize the library
  Fisica.init(this);
 
  //create a new Fisica world
  world = new FWorld();
 
  //creates the edges: 4 static bodies at the edge of the sketch
  world.setEdges();
}
 
 
void draw() {
 
   background(255);
 
   if(frameCount%10 == 0)
    {
    //create new circle 30 pix diameter
    FCircle c = new FCircle(30);
 
    //set its center position
    c.setPosition(random(50, width-50), 100);
 
    c.setFill(133, 203, 72);
    c.setNoStroke();
 
    world.add(c);
    }
 
 
  //update the simulation
  world.step();
 
  //display the results
  world.draw();
}
 
void mousePressed()
{
    //get an ArrayList with all the bodies
   //http://www.processing.org/reference/ArrayList.html
   ArrayList< FBody > bodies = world.getBodies();
 
   //cycle thrugh all bodies with a for loop
   for (int i = 0; i < bodies.size(); i++) 
   {
   //get each body and store it in a temporary variable
   FBody myBody = bodies.get(i);
 
   //do anything you would normally do with it
   //http://www.ricardmarxer.com/fisica/reference/fisica/FBody.html
 
   //don't remove certain bodies
   if(myBody != world.left && myBody != world.right && myBody != world.top && myBody != world.bottom)
     world.remove(myBody); 
 
   //*don't use body.removeFromWorld()
 
   }
 
 
}

Naming Objects

I can “tag” certain types of objects to access them selectively late.

In this example I’m naming all the boxes “box” when I create them:

b.setName("box");

…so I can find tell them apart later and change their fill at the mouse click:

if(myBody.getName() == "box")

The rest is almost the same as the examples above:

//import the physics library
import fisica.*;
 
//declare a global variable: our newtonian world
FWorld world;
 
void setup() {
  //sketch size
  size(800, 600);
 
  //initialize the library
  Fisica.init(this);
 
  //create a new Fisica world
  world = new FWorld();
 
  //creates the edges: 4 static bodies at the edge of the sketch
  world.setEdges();
}
 
 
void draw() {
 
   background(255);
 
   if(frameCount%10 == 0)
    {
    //create new circle 30 pix diameter
    FCircle c = new FCircle(30);
 
    //set its center position
    c.setPosition(random(50, width-50), 100);
 
    c.setFill(133, 203, 72);
    c.setNoStroke();
 
    world.add(c);
    }
 
   if(frameCount%10 == 5)
    {
    //create new circle 30 pix diameter
    FBox b = new FBox(30, 30);
 
    //set its center position
    b.setPosition(random(50, width-50), 100);
 
    b.setFill(133, 203, 72);
    b.setNoStroke();
 
    //tagging all the boxes with a name
    b.setName("box");
 
    world.add(b);
    }
 
 
  //update the simulation
  world.step();
 
  //display the results
  world.draw();
}
 
void mousePressed()
{
   //get an ArrayList with all the bodies
   //http://www.processing.org/reference/ArrayList.html
   ArrayList < FBody > bodies = world.getBodies();
 
   //cycle thrugh all bodies with a for loop
   for (int i = 0; i < bodies.size(); i++) 
   {
   //get each body and store it in a temporary variable
   FBody myBody = bodies.get(i);
 
   //do anything you would normally do with it
   //http://www.ricardmarxer.com/fisica/reference/fisica/FBody.html
 
   //perform an action only on bodies with a certain name
   if(myBody.getName() == "box")
     {
      myBody.setFill(random(255), random(255), random(255));
     }
 
   }
}
Posted in

Post a comment