Fisica library - exporting results in vector ( PDF or SVG)?

hello all, i am new to processing and Fisica library. i wrote a little program for simulating bodies falling on a static one. ( very simple one), the shapes are geometric SVG that i import ( using lines from an existing example). everything is working fine, at the end of the simulation i am trying to export the result to vector file ( PDF would be perfect). exporting PDF is not working and i can figure out how to do it , is there any possible solution?) i thought about solving the problem by myself, so the solution i thought about is getting the position , rotation , and scale of every existing body in the simulation and redrawing everything as polygons which will get me the same result but with the possibility of exporting to PDF this time. when i use the Fpoly.getX() in the draw loop i get a nullPointerException. so my question is : - is there a way to export to PDF in Fisica? - if not, can you help me with the coordinate thing at the end of my simulation, getX(), getY(),...

to make it simple i wrote the sketch with just one shape.

        import processing.pdf.*;

        import fisica.*;
        import geomerative.*;


        FWorld world;
        FPoly poly;

        String filename = "zel1.svg";
        RShape m_shape;
        RShape fullSvg;
        RShape outline;
        RPoint[] points;

        float xx,yy;


        void setup(){
         size(400,400);
         smooth();
         frameRate(60);
         Fisica.init(this);
         Fisica.setScale(10);
         RG.init(this);
         RG.setPolygonizer(RG.ADAPTATIVE);
         FPoly poly = new FPoly();
         world = new FWorld();
         world.setEdges(this,color(0));
         world.setGravity(0,300);
         RShape fullSvg = RG.loadShape(filename);
         RShape outline = fullSvg.getChild("outline");


         RPoint[] points = outline.getPoints();

         for (int i=0; i<points.length; i++) { poly.vertex(points[i].x, points[i].y);}


          poly.setFill(120, 30, 90);
          poly.setPosition(width/2,50);     
          poly.setDensity(10);
          poly.setRestitution(0.5);



              world.add(poly);
                xx=poly.getX(); // it is working here
                println(xx);

        }

        void draw() {


          background(255);

          world.step();

          world.draw(this);
          yy=poly.getY(); // getting an NPE erro here
          xx= poly.getX();// getting an NPE erro here


        }

Answers

  • Answer ✓

    line 27 redefines the poly in line 8 which means the poly in line 8 isn't initialised. this is why you have problems on line 61/62

    remove the first FPoly on line 27 and that should fix that.

  • edited June 2017 Answer ✓

    (but you've done the same on lines 31 and 32 with other variables)

    (and line 35)

  • thank you Koogs, it was helpful!

  • edited June 2017

    i managed to export to PDF using this little trick

    first in my sketch i declared a table

    Table table; // a table to store all the information i will need
    PShape nom; // variable to import my SVG
    // array list of all the bodies in my world    
    ArrayList<FBody> bodies = new ArrayList<FBody>(); 
    

    give the bodies a name before adding to the world

    obj.setPosition(x, y);
    obj.setRotation(angle+PI/2);
    obj.setVelocity(magnitude*cos(angle), magnitude*sin(angle));
    obj.setDamping(0);
    obj.setRestitution(0.5);
    //in this case i named my bodies zel1,zel2,zel3,....
    obj.setName("zel"+indice);
    world.add(obj);
    

    the parameters of the table

    table = new Table();
      table.addColumn("indice");
      table.addColumn("posx");
      table.addColumn("posy");
      table.addColumn("rotation");
    

    in the draw() function at after world.setup()

     world.step();
     bodies = world.getBodies();
     float taille = bodies.size();
    

    a routine to write to the table

    void keyPressed() {
    // this loop could be done in a more elegant way,but...
    for(int k=0;k<taille;k++){  
    TableRow newRow = table.addRow();
    newRow.setString("indice", bodies.get(k).getName());
    newRow.setFloat("posx", bodies.get(k).getX());
    newRow.setFloat("posy", bodies.get(k).getY());
    newRow.setFloat("rotation", bodies.get(k).getRotation());
    saveTable(table, "coord.csv");
    }
    

    a routine to redraw the result to screen from my SVGs and exporting to PDF

    void twichiya(){
    beginRecord(PDF, "stoune.pdf"); 
    
    fill(0);
    table = loadTable("coord.csv", "header");
    for (TableRow row : table.rows()) {
    
    String indice = row.getString("indice");
    float posx = row.getFloat("posx");
    float posy = row.getFloat("posy");
    float rotation = row.getFloat("rotation");
    
    if(indice!=null){
    nom=loadShape(indice+".svg");
    pushMatrix();
    translate(posx,posy);
    rotate(rotation);
    scale(0.3); // the value of scale should be the same used while importing the SVG
    shape(nom,0,0);
     popMatrix();
    }
    endRecord();
     }}
    

    et voilà working like a charm hope this will be helpful

Sign In or Register to comment.