Help to acced the Vertex of a Body using the Fisica library!

edited February 2017 in Library Questions

Hey, I'm using the Fisica library in one sketch, I have create a FBlob and I need to acced the vertex of this FBlob. In the documentation there is a function getVertexBodies(), It is a java.util.ArrayList ArrayList, but when I call size() it is allways 0. There is also the float getVertexSize() and this return always 8.0 regardless of how many vertex the FBlob had. I supose, that I don't know how to implement it well :( Has anyone implemented it yet? I appreciate all help Mariana

Answers

  • Can you post a sample code? You can always have a look at the source code to see what other methods are available, if any.

    Kf

  • Hey, I did a simple sample code, and the funktion works here! it should be something wrong in the "big" code... Any way, I try to grabb the mouse with .setGrabbable(true); to know if it is on (or inside) a FBody, but it don't works :( Here is the code: /** * FBlobs, Vertex, FBlob.getVertexBodies().size() * * 1° clik the mouse +3 times to create a second FBlob * 2° press SPACE to add new FBlob to world * Now you will see the second FBlob * 3° clik on a vertex or outside to test FBlob.getVertexBodies().size() */

    import fisica.*;
    import java.util.*;
    
    FWorld world;
    FBlob blob1, blob2;
    boolean habemusBlob = false;
    int vertexCount = 0;
    
    void setup() {
      size(400, 400);
    
      Fisica.init(this);
    
      world = new FWorld();
      world.setGravity(0, 50);
      world.setEdges();
      world.setEdgesRestitution(0.5);
      //world.setGrabbable(true);
    
      blob1 = new FBlob();  
      blob1.setStrokeWeight(3);
      blob1.setFill(160, 190, 80);
      blob1.setDensity(10);
      blob1.setRestitution(0.5);
      //blob1.setGrabbable(true);
      // 13 vertex
      blob1.vertex(40, 10);
      blob1.vertex(50, 20);
      blob1.vertex(60, 30);
      blob1.vertex(60, 40);
      blob1.vertex(50, 50);
      blob1.vertex(40, 60);
      blob1.vertex(30, 70);
      blob1.vertex(20, 60);
      blob1.vertex(10, 50);
      blob1.vertex(10, 40);
      blob1.vertex(20, 30);
      blob1.vertex(30, 20);
      blob1.vertex(40, 10);
      world.add(blob1);
    
      blob2 = new FBlob();
      blob2.setStrokeWeight(3);
      blob2.setFill(86, 160, 86);
      blob2.setDensity(10);
      blob2.setRestitution(0.5);
      //blob2.setGrabbable(true);
    }
    
    void draw() {
      background(255);
    
      world.step();
      world.draw(this);
    }
    
    
    void mousePressed() {
      if (habemusBlob == false) {
        blob2.vertex(mouseX, mouseY);
        vertexCount ++;
      }
    }
    
    void mouseReleased() {
      if (habemusBlob == true) {
        println("blob2.getVertexBodies().size():  "+blob2.getVertexBodies().size()+", vertexCount: "+ vertexCount);
        println("blob1.getVertexBodies().size():  "+blob1.getVertexBodies().size()); 
    
        // if (mouseX, mouseY) is a vertex
        if (world.getBody(mouseX, mouseY) != null) {
          FBody vertex = world.getBody(mouseX, mouseY); // if (mouseX, mouseY) is a vertex 
          println("bodyVertex "+ vertex.getX()+",  "+ vertex.getY());
          blob2.setFill(0, 255, 0);
          //println("blob2.getVertexBodies().size():  "+blob2.getVertexBodies().size()+", vertexCount: "+ vertexCount);
          //println("blob1.getVertexBodies().size():  "+blob1.getVertexBodies().size()); 
        }
        else {
          println("no vertex here");
        }
      }
    }
    
    void keyPressed() {
      if (key == ' ') {
        if (habemusBlob == false) {
          if (blob2 != null) {
            world.add(blob2);
            habemusBlob = true;
          }
        }
      }
    } 
    
Sign In or Register to comment.