Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to make an arc body on box2D

$
0
0

Hello, I'm currently working on a program. I have extracted pieces of codes from other box2D libraries and have put together a code displaying arc's when you click onto the canvas. The problem is that when I switched the arc from a circle using a circle c.s fixture, it displayed an arc but had a invisible force field of a circle. I've searched for 'arc' body codes but couldn't find anyting useful. Is there a way to display an arc without this happening and making it a free structure. The problem is located in the last 1/3 of the code. I just need the arc's not to interact like circles. Thanks everyone.

    import shiffman.box2d.*;
    import org.jbox2d.collision.shapes.*;
    import org.jbox2d.common.*;
    import org.jbox2d.dynamics.*;

    // A list for all of our rectangles
    ArrayList<Circle> circles;
    ArrayList<Boundary> boundaries;

    Box2DProcessing box2d;

    int whiteSum = 0; //Variable to sum up all of the white pixels.
    int filledSum = 0; // Varaible to sum up all of the white pixels after having filled the box with black circles
    int firstTime = 1; // Variable allow the initial sum of white pixels to be counted then turned to zero for the rest of the simulation
    float r = 20; // Radius of the circle

    void setup() {
      size(800,600);
      smooth();
      // Initialize and create the Box2D world
      box2d = new Box2DProcessing(this);
      box2d.createWorld();
      box2d.setGravity(0,-20);

      // Add some boundaries
      boundaries = new ArrayList<Boundary>();
      boundaries.add(new Boundary(width/2,height-5,width,10));
      boundaries.add(new Boundary(width/2,5,width,10));
      boundaries.add(new Boundary(width-5,height/2,10,height));
      boundaries.add(new Boundary(5,height/2,10,height));

      // Create ArrayLists
      circles = new ArrayList<Circle>();

    }

    void draw() {
      background(255);

      // We must always step through time!
      box2d.step();

      // Display all the boxes
      for (Circle b: circles) {
        b.display();
      }

      for (Boundary b: boundaries) {
        b.display();}
      }

      // Need to draw some black boxes to make sure there are no edge effects

      //rectMode(CENTER);
      //rect(width/2,height-5-r,width,10+r);
      //rect(width/2,5+r,width,10+r);
      //rect(width-5-r,height/2,10+r,height);
      //rect(5+r,height/2,10+r,height);




    void mousePressed() {
      Circle p = new Circle(mouseX,mouseY,r);
        circles.add(p);
    }

    // The Nature of Code
    // <http://www.shiffman.net/teaching/nature&gt;
    // Spring 2012
    // Box2DProcessing example

    // A fixed boundary class

    class Boundary {

      // A boundary is a simple rectangle with x,y,width,and height
      float x;
      float y;
      float w;
      float h;

      // But we also have to make a body for box2d to know about it
      Body b;

      Boundary(float x_,float y_, float w_, float h_) {
        x = x_;
        y = y_;
        w = w_;
        h = h_;

        // Define the polygon
        PolygonShape sd = new PolygonShape();
        // Figure out the box2d coordinates
        float box2dW = box2d.scalarPixelsToWorld(w/2);
        float box2dH = box2d.scalarPixelsToWorld(h/2);
        // We're just a box
        sd.setAsBox(box2dW, box2dH);


        // Create the body
        BodyDef bd = new BodyDef();
        bd.type = BodyType.STATIC;
        bd.position.set(box2d.coordPixelsToWorld(x,y));
        b = box2d.createBody(bd);

        // Attached the shape to the body using a Fixture
        b.createFixture(sd,1);
      }

      // Draw the boundary
      void display() {
        fill(0);
        stroke(0);
        strokeWeight(0);
        rectMode(CENTER);
        rect(x,y,w,h);
      }

    }

      // This is where my problem occurs

    class Circle {
      //  Instead of any of the usual variables, we will store a reference to a Box2D Body
      Body body;


      Circle(float x, float y, float r) {

        // Build Body
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(box2d.coordPixelsToWorld(x,y));
        body = box2d.createBody(bd);


        CircleShape cs = new CircleShape();
        cs.m_radius = box2d.scalarPixelsToWorld(r);

        // Define a fixture
        FixtureDef fd = new FixtureDef();
        fd.shape = cs;
        // Parameters that affect physics
        fd.density = 0.1;
        fd.friction = 0.3;
        fd.restitution = 0.5;

        // Attach Fixture to Body
        body.createFixture(fd);
      }

      void display() {
        // We need the Body’s location and angle
        Vec2 pos = box2d.getBodyPixelCoord(body);
        float a = body.getAngle();

        pushMatrix();
        translate(pos.x,pos.y);     // Using the Vec2 position and float angle to
        rotate(-a);                 // translate and rotate the rectangle
        noFill();
        stroke(0);
        strokeWeight(15);
        arc(0, 0, 35, 35, 0, PI);
        popMatrix();
      }

    }

Viewing all articles
Browse latest Browse all 2896

Trending Articles