Advertisement
Guest User

Blobby.java

a guest
Jun 9th, 2010
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.43 KB | None | 0 0
  1. package Blobby;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.jbox2d.collision.shapes.CircleDef;
  6. import org.jbox2d.collision.shapes.PolygonDef;
  7. import org.jbox2d.common.Vec2;
  8. import org.jbox2d.dynamics.Body;
  9. import org.jbox2d.dynamics.BodyDef;
  10. import org.jbox2d.dynamics.joints.ConstantVolumeJointDef;
  11.  
  12. import pbox2d.PBox2D;
  13. import processing.core.PApplet;
  14. import processing.core.PVector;
  15.  
  16. public class Blobby extends PApplet {
  17.  
  18.     PBox2D box2d = new PBox2D(this);
  19.  
  20.     class Blob {
  21.  
  22.         // A list to keep track of all the points in our blob
  23.         ArrayList skeleton;
  24.  
  25.         float bodyRadius; // The radius of each body that makes up the skeleton
  26.         float radius; // The radius of the entire blob
  27.         float totalPoints; // How many points make up the blob
  28.  
  29.         // We should modify this constructor to receive arguments
  30.         // So that we can make many different types of blobs
  31.         Blob() {
  32.  
  33.             // Create the empty
  34.             skeleton = new ArrayList();
  35.  
  36.             // Let's make a volume of joints!
  37.             ConstantVolumeJointDef cvjd = new ConstantVolumeJointDef();
  38.  
  39.             // Where and how big is the blob
  40.             Vec2 center = new Vec2(width / 2, height / 2);
  41.             radius = 100;
  42.             totalPoints = 20;
  43.             bodyRadius = 12;
  44.  
  45.             // Initialize all the points
  46.             for (int i = 0; i < totalPoints; i++) {
  47.                 // Look polar to cartesian coordinate transformation!
  48.                 float theta = PApplet.map(i, 0, totalPoints, 0, TWO_PI);
  49.                 float x = center.x + radius * sin(theta);
  50.                 float y = center.y + radius * cos(theta);
  51.  
  52.                 // Make each individual body
  53.                 BodyDef bd = new BodyDef();
  54.                 bd.fixedRotation = true; // no rotation!
  55.                 bd.position.set(box2d.screenToWorld(x, y));
  56.                 Body body = box2d.createBody(bd);
  57.  
  58.                 // The body is a circle
  59.                 CircleDef cd = new CircleDef();
  60.                 cd.radius = box2d.scaleScreenToWorld(bodyRadius);
  61.                 cd.density = 1.0f;
  62.                 // For filtering out collisions
  63.                 cd.filter.groupIndex = -2;
  64.  
  65.                 // Finalize the body
  66.                 body.createShape(cd);
  67.                 // Add it to the volume
  68.                 cvjd.addBody(body);
  69.                 // We always do this at the end
  70.                 body.setMassFromShapes();
  71.  
  72.                 // Store our own copy for later rendering
  73.                 skeleton.add(body);
  74.             }
  75.  
  76.             // These parameters control how stiff vs. jiggly the blob is
  77.             cvjd.frequencyHz = 10.0f;
  78.             cvjd.dampingRatio = 1.0f;
  79.  
  80.             // Put the joint thing in our world!
  81.             box2d.world.createJoint(cvjd);
  82.  
  83.         }
  84.  
  85.         // Time to draw the blob!
  86.         // Can you make it a cute character, a la
  87.         // http://postspectacular.com/work/nokia/friends/start
  88.         void display() {
  89.  
  90.             // Draw the outline
  91.             beginShape();
  92.             noFill();
  93.             stroke(0);
  94.             strokeWeight(1);
  95.             for (int i = 0; i < skeleton.size(); i++) {
  96.                 // We look at each body and get its screen position
  97.                 Body b = (Body) skeleton.get(i);
  98.                 Vec2 pos = box2d.getScreenPos(b);
  99.                 vertex(pos.x, pos.y);
  100.             }
  101.             endShape(CLOSE);
  102.  
  103.             // Draw the individual circles
  104.             for (int i = 0; i < skeleton.size(); i++) {
  105.                 Body b = (Body) skeleton.get(i);
  106.                 // We look at each body and get its screen position
  107.                 Vec2 pos = box2d.getScreenPos(b);
  108.                 // Get its angle of rotation
  109.                 float a = b.getAngle();
  110.                 pushMatrix();
  111.                 translate(pos.x, pos.y);
  112.                 rotate(a);
  113.                 fill(175);
  114.                 stroke(0);
  115.                 strokeWeight(1);
  116.                 ellipse(0, 0, bodyRadius * 2, bodyRadius * 2);
  117.                 popMatrix();
  118.             }
  119.  
  120.         }
  121.  
  122.     }
  123.  
  124.     class Boundary {
  125.  
  126.         // A boundary is a simple rectangle with x,y,width,and height
  127.         float x;
  128.         float y;
  129.         float w;
  130.         float h;
  131.         // But we also have to make a body for box2d to know about it
  132.         Body b;
  133.  
  134.         Boundary(float x_, float y_, float w_, float h_) {
  135.             x = x_;
  136.             y = y_;
  137.             w = w_;
  138.             h = h_;
  139.  
  140.             // Figure out the box2d coordinates
  141.             float box2dW = box2d.scaleScreenToWorld(w / 2);
  142.             float box2dH = box2d.scaleScreenToWorld(h / 2);
  143.             Vec2 center = new Vec2(x, y);
  144.  
  145.             // Define the polygon
  146.             PolygonDef sd = new PolygonDef();
  147.             sd.setAsBox(box2dW, box2dH);
  148.             sd.density = 0; // No density means it won't move!
  149.             sd.friction = 0.3f;
  150.  
  151.             // Create the body
  152.             BodyDef bd = new BodyDef();
  153.             bd.position.set(box2d.screenToWorld(center));
  154.             b = box2d.createBody(bd);
  155.             b.createShape(sd);
  156.         }
  157.  
  158.         // Draw the boundary, if it were at an angle we'd have to do something
  159.         // fancier
  160.         void display() {
  161.             fill(0);
  162.             stroke(0);
  163.             rectMode(CENTER);
  164.             rect(x, y, w, h);
  165.         }
  166.  
  167.     }
  168.  
  169.     // A list we'll use to track fixed objects
  170.     ArrayList boundaries;
  171.  
  172.     // Our "blob" object
  173.     Blob blob;
  174.  
  175.     public void setup() {
  176.         size(400, 300);
  177.         smooth();
  178.  
  179.         // Initialize box2d physics and create the world
  180.         box2d = new PBox2D(this);
  181.         box2d.createWorld();
  182.  
  183.         // Add some boundaries
  184.         boundaries = new ArrayList();
  185.         boundaries.add(new Boundary(width / 2, height - 5, width, 10));
  186.         boundaries.add(new Boundary(width / 2, 5, width, 10));
  187.         boundaries.add(new Boundary(width - 5, height / 2, 10, height));
  188.         boundaries.add(new Boundary(5, height / 2, 10, height));
  189.  
  190.         // Make a new blob
  191.         blob = new Blob();
  192.     }
  193.  
  194.     public void draw() {
  195.         background(255);
  196.  
  197.         // We must always step through time!
  198.         box2d.step();
  199.  
  200.         // Show the blob!
  201.         blob.display();
  202.  
  203.         // Show the boundaries!
  204.         for (int i = 0; i < boundaries.size(); i++) {
  205.             Boundary wall = (Boundary) boundaries.get(i);
  206.             wall.display();
  207.         }
  208.  
  209.         // Here we create a dynamic gravity vector based on the location of our
  210.         // mouse
  211.         PVector g = new PVector(mouseX - width / 2, mouseY - height / 2);
  212.         g.normalize();
  213.         g.mult(10);
  214.         box2d.setGravity(g.x, -g.y);
  215.     }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement