Lorick’s Final project
https://www.dropbox.com/s/h7ls3jya1blhzh9/Game_Program.exe?dl=0
import shiffman.box2d.*; import org.jbox2d.collision.shapes.*; import org.jbox2d.common.*; import org.jbox2d.dynamics.*; import org.jbox2d.dynamics.joints.*; import org.jbox2d.collision.shapes.Shape; import org.jbox2d.dynamics.contacts.*; Box2DProcessing box2d;
// An ArrayList of particles that will fall on the surface ArrayList<Particle> particles;
// An object to store information about the uneven surface Surface surface; // A list we'll use to track fixed objects
Blob blob; ArrayList<Box> boxes; void setup() { size(800,500); smooth(); // Initialize box2d physics and create the world box2d = new Box2DProcessing(this); box2d.createWorld();
// Add some boundaries
box2d.listenForCollisions();
// Make a new blob blob = new Blob();
// We are setting a custom gravity box2d.setGravity(0, -20);
// Create the empty list particles = new ArrayList<Particle>(); // Create the surface surface = new Surface(); boxes = new ArrayList<Box>();
}
void draw() { // If the mouse is pressed, we make new particles if (random(1) < 0.5) { float sz = random(2,6); //particles.add(new Particle(width/2,10,sz)); } // Update location blob.update(); // Wrape edges blob.wrapEdges(); // Draw ship
// We must always step through time! box2d.step();
background(255);
// We must always step through time! box2d.step();
// Show the blob! blob.display();
if (random(1) < 0.1) { Box p = new Box (random(width),10); boxes.add(p); if (p.done()) { boxes.remove(p); } }
if (mousePressed) { for (Box b: boxes) { Vec2 wind = new Vec2(100,0); b.applyForce(wind); } }
// Show the boundaries!
// Draw the surface surface.display();
if (keyPressed) { if (key == CODED && keyCode == LEFT) { blob.move(-0.03); } else if (key == CODED && keyCode == RIGHT) { blob.move(0.03); }else if (key == 'z' || key == 'Z') { blob.thrust(); } } // Draw all particles //for (Particle p: particles) { // p.display(); // }
// Just drawing the framerate to see how many particles it can handle fill(0); text("framerate: " + (int)frameRate,12,16);
}
// An uneven surface boundary
class Surface { //keep track of all of the surface points ArrayList<Vec2> surface;
Surface() { surface = new ArrayList<Vec2>(); // keep track of the screen coordinates of the chain surface.add(new Vec2(width,height/6)); surface.add(new Vec2(width/2,height/2+50)); surface.add(new Vec2(-1000,height/2+50));
// put the surface in its world ChainShape chain = new ChainShape();
// We can add 3 vertices by making an array of 3 Vec2 objects Vec2[] vertices = new Vec2[surface.size()]; for (int i = 0; i < vertices.length; i++) { vertices[i] = box2d.coordPixelsToWorld(surface.get(i)); }
chain.createChain(vertices,vertices.length);
// The edge chain is now a body BodyDef bd = new BodyDef(); Body body = box2d.world.createBody(bd); // Shortcut, we could define a fixture if we // want to specify frictions, restitution, etc. body.createFixture(chain,1); }
// draw the edge chain as a series of vertex points void display() { strokeWeight(1); stroke(0); fill(200); beginShape(); for (Vec2 v: surface) { vertex(v.x,v.y); } vertex(0,height); vertex(width,height); endShape(); } }
class Blob { PVector velocity; PVector location; PVector acceleration; // A list to keep track of all the points in our blob ArrayList<Body> skeleton; float damping = 0.995; float topspeed = 6; 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 float heading = 0; boolean thrusting = false;
Blob() {
// Create the empty skeleton = new ArrayList<Body>();
ConstantVolumeJointDef cvjd = new ConstantVolumeJointDef();
// Where and how big is the blob location = new PVector(width/2,height/2); radius = 100; totalPoints = 50; bodyRadius = 0; acceleration = new PVector(); velocity = new PVector(); // 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 = location.x + radius * sin(theta); float y = location.y + radius * cos(theta);
// Make each individual body BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC;
bd.fixedRotation = true; // no rotation! bd.position.set(box2d.coordPixelsToWorld(x, y)); Body body = box2d.createBody(bd);
// The body is a circle CircleShape cs = new CircleShape(); cs.m_radius = box2d.scalarPixelsToWorld(bodyRadius);
// Define a fixture FixtureDef fd = new FixtureDef(); fd.shape = cs;
// For filtering out collisions fd.filter.groupIndex = -2;
// Parameters that affect physics fd.density = 1;
// Finalize the body body.createFixture(fd); // Add it to the volume cvjd.addBody(body);
// Store our copy skeleton.add(body);
velocity.add(acceleration); velocity.mult(damping); velocity.limit(topspeed); location.add(velocity); acceleration.mult(0); }
// These parameters control how stiff vs. jiggly the blob is cvjd.frequencyHz = 10.0f; cvjd.dampingRatio = 1.0f;
// Put the joint thing in our world! box2d.world.createJoint(cvjd); } void update() {
}
// draw the blob void applyForce(PVector force) { PVector f = force.get(); //f.div(mass); // ignoring mass right now acceleration.add(f); } void move(float radius){ heading += radius; } void thrust() { // Offset the angle float angle = heading - PI/2; // Polar to cartesian for force vector! PVector force = new PVector(cos(angle),sin(angle)); force.mult(10); applyForce(force); // To draw booster thrusting = true; } void wrapEdges() { float buffer = radius*2; if (location.x > width + buffer) location.x = -buffer; else if (location.x < -buffer) location.x = width+buffer; if (location.y > height + buffer) location.y = -buffer; else if (location.y < -buffer) location.y = height+buffer; } void display() {
// Draw the outline beginShape(); noFill(); stroke(0); strokeWeight(1); for (Body b: skeleton) { Vec2 location = box2d.getBodyPixelCoord(b); vertex(location.x, location.y); } endShape(CLOSE);
// Draw the individual circles for (Body b: skeleton) { // each body and get its screen position Vec2 location = box2d.getBodyPixelCoord(b); // Get its angle of rotation //float a = b.getAngle(); pushMatrix(); translate (location.x, location.y); rotate(heading); fill(175); stroke(0); strokeWeight(1); ellipse(0, 0, bodyRadius*2, bodyRadius*2); popMatrix(); thrusting = false; } }
}
// A rectangular box class Box{
// keep track of a Body and a width and height Body body; float w; float h;
// Constructor Box(float x, float y) { w = 1; //random(8, 16); h = w; // Add the box to the box2d world makeBody(new Vec2(x, y), w, h); }
// This function removes the particle from the box2d world void killBody() { box2d.destroyBody(body); }
// Is the particle ready for deletion? boolean done() { // screen position of the particle Vec2 pos = box2d.getBodyPixelCoord(body); // Is it off the bottom of the screen? if (pos.y > height+w*h) { killBody(); return true; } return false; }
void applyForce(Vec2 force) { Vec2 pos = body.getWorldCenter(); body.applyForce(force, pos); }
// Drawing the box void display() { // each body and get its screen position Vec2 pos = box2d.getBodyPixelCoord(body); // Get its angle of rotation float a = body.getAngle();
rectMode(CENTER); pushMatrix(); translate(pos.x, pos.y); rotate(-a); fill(175); stroke(0); rect(0, 0, w, h); popMatrix(); }
// This function adds the rectangle to the box2d world void makeBody(Vec2 center, float w_, float h_) {
// Define a polygon PolygonShape sd = new PolygonShape(); float box2dW = box2d.scalarPixelsToWorld(w_/2); float box2dH = box2d.scalarPixelsToWorld(h_/2); sd.setAsBox(box2dW, box2dH);
// Define a fixture FixtureDef fd = new FixtureDef(); fd.shape = sd; // Parameters that affect physics fd.density = 1; fd.friction = 0.3; fd.restitution = 0.2;
// Define the body and make it from the shape BodyDef bd = new BodyDef(); bd.type = BodyType.DYNAMIC; bd.position.set(box2d.coordPixelsToWorld(center)); bd.angle = random(TWO_PI);
body = box2d.createBody(bd); body.createFixture(fd); }
}
// A circular particle
class Particle {
// keep track of a Body and a radius Body body; float r;
Particle(float x, float y, float r_) { r = r_; // This function puts the particle in the Box2d world makeBody(x,y,r); }
// This function removes the particle from the box2d world void killBody() { box2d.destroyBody(body); }
// Is the particle ready for deletion? boolean done() { // Let's find the screen position of the particle Vec2 pos = box2d.getBodyPixelCoord(body); // Is it off the bottom of the screen? if (pos.y > 5/height+r*2) { killBody(); return true; } return false; }
// void display() { // each body and get its screen position Vec2 pos = box2d.getBodyPixelCoord(body); // Get its angle of rotation float a = body.getAngle(); pushMatrix(); translate(pos.x,pos.y); rotate(-a); fill(175); stroke(0); strokeWeight(1); ellipse(0,0,r*2,r*2); //add a line so we can see the rotation line(0,0,r,0); popMatrix(); }
// adds the particle to the Box2D world void makeBody(float x, float y, float r) { // Define a body BodyDef bd = new BodyDef(); // Set its position bd.position = box2d.coordPixelsToWorld(x,y); bd.type = BodyType.DYNAMIC; body = box2d.world.createBody(bd);
// Make the body's shape a circle CircleShape cs = new CircleShape(); cs.m_radius = box2d.scalarPixelsToWorld(r);
FixtureDef fd = new FixtureDef(); fd.shape = cs; // Parameters that affect physics fd.density = 1; fd.friction = 0.01; fd.restitution = 0.3;
// Attach fixture to body body.createFixture(fd);
// Give it a random initial velocity (and angular velocity) body.setLinearVelocity(new Vec2(random(-10f,10f),random(5f,10f))); body.setAngularVelocity(random(-10,10)); } }













