Interaction between "blobs" with Box2D
in
Contributed Library Questions
•
2 years ago
Hey all,
I am happily playing around and trying to further the "
blob" example from Daniel Shiffman's Box2d library for Processing.
What I am trying to do is create two blobs that can interact(collide) with each other. I am able to easily create two different blob objects yet they seem to render on different layers and are not influenced by each other.
My two objects:
- import pbox2d.*;
- import org.jbox2d.collision.shapes.*;
- import org.jbox2d.common.*;
- import org.jbox2d.dynamics.*;
- import org.jbox2d.dynamics.joints.*;
- // A reference to our box2d world
- PBox2D box2d;
- // A list we'll use to track fixed objects
- ArrayList<Boundary> boundaries;
- // Our "blob" objects
- Blob blob;
- Blob blob2;
- void setup() {
- size(400,300);
- smooth();
- // Initialize box2d physics and create the world
- box2d = new PBox2D(this);
- box2d.createWorld();
- // 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));
- // Make new blobs
- blob = new Blob();
- blob2 = new Blob();
- }
- void draw() {
- background(255);
- // We must always step through time!
- box2d.step();
- // Show the blobs!
- blob.display();
- blob2.display();
- // Show the boundaries!
- for (Boundary wall: boundaries) {
- wall.display();
- }
- // Here we create a dynamic gravity vector based on the location of our mouse
- PVector g = new PVector(mouseX-width/2,mouseY-height/2);
- g.normalize();
- g.mult(10);
- }
Blob class:
- // The Nature of Code
- // <http://www.shiffman.net/teaching/nature>
- // Spring 2010
- // PBox2D example
- // A blob skeleton
- // Could be used to create blobbly characters a la Nokia Friends
- // http://postspectacular.com/work/nokia/friends/start
- class Blob {
- // A list to keep track of all the points in our blob
- ArrayList<Body> skeleton;
- float bodyRadius; // The radius of each body that makes up the skeleton
- float radius; // The radius of the entire blob
- float totalPoints; // How many points make up the blob
- // We should modify this constructor to receive arguments
- // So that we can make many different types of blobs
- Blob() {
- // Create the empty
- skeleton = new ArrayList<Body>();
- // Let's make a volume of joints!
- ConstantVolumeJointDef cvjd = new ConstantVolumeJointDef();
- // Where and how big is the blob
- Vec2 center = new Vec2(random(100,width-100),random(100,height/2));
- radius = random(30,60);
- totalPoints = 40;
- bodyRadius = 1;
- // Initialize all the points
- for (int i = 0; i < totalPoints; i++) {
- // Look polar to cartesian coordinate transformation!
- float theta = PApplet.map(i, 0, totalPoints, 0, TWO_PI);
- float x = center.x + radius * sin(theta);
- float y = center.y + radius * cos(theta);
- // Make each individual body
- BodyDef bd = new BodyDef();
- bd.fixedRotation = true; // no rotation!
- bd.position.set(box2d.coordPixelsToWorld(x,y));
- Body body = box2d.createBody(bd);
- // The body is a circle
- CircleDef cd = new CircleDef();
- cd.radius = box2d.scalarPixelsToWorld(bodyRadius);
- cd.density = 1.0f;
- // For filtering out collisions
- cd.filter.groupIndex = -2;
- // Finalize the body
- body.createShape(cd);
- // Add it to the volume
- cvjd.addBody(body);
- // We always do this at the end
- body.setMassFromShapes();
- // Store our own copy for later rendering
- skeleton.add(body);
- }
- // These parameters control how stiff vs. jiggly the blob is
- cvjd.frequencyHz = 12.0f;
- cvjd.dampingRatio = 2.0f;
- // Put the joint thing in our world!
- box2d.world.createJoint(cvjd);
- }
- // Time to draw the blob!
- // Can you make it a cute character, a la http://postspectacular.com/work/nokia/friends/start
- void display() {
- // Draw the outline
- beginShape();
- fill(150);
- noStroke();
- strokeWeight(1);
- for (Body b: skeleton) {
- Vec2 pos = box2d.getBodyPixelCoord(b);
- vertex(pos.x,pos.y);
- }
- endShape(CLOSE);
- // Draw the individual circles
- /*for (Body b: skeleton) {
- // We look at each body and get its screen position
- Vec2 pos = box2d.getBodyPixelCoord(b);
- // Get its angle of rotation
- float a = b.getAngle();
- pushMatrix();
- translate(pos.x,pos.y);
- rotate(a);
- fill(175);
- stroke(0);
- strokeWeight(1);
- ellipse(0,0,bodyRadius*2,bodyRadius*2);
- popMatrix();
- }*/
- }
Boundary class:
I know it's probably right under my nose, yet if I could get some advice I'd really appreciate it!
Thanks
1