├── .gitignore ├── Box2D-for-Processing ├── .classpath ├── .gitignore ├── .project ├── dist │ ├── box2d_processing.zip │ └── box2d_processing │ │ ├── examples │ │ ├── ApplyForceAttractMouse │ │ │ ├── ApplyForceAttractMouse.pde │ │ │ ├── Boundary.pde │ │ │ └── Box.pde │ │ ├── ApplyForceSimpleWind │ │ │ ├── ApplyForceSimpleWind.pde │ │ │ ├── Boundary.pde │ │ │ └── Box.pde │ │ ├── AttractionApplyForce │ │ │ ├── AttractionApplyForce.pde │ │ │ ├── Attractor.pde │ │ │ └── Mover.pde │ │ ├── Blobby │ │ │ ├── Blob.pde │ │ │ ├── Blobby.pde │ │ │ └── Boundary.pde │ │ ├── Boxes │ │ │ ├── Boundary.pde │ │ │ ├── Box.pde │ │ │ └── Boxes.pde │ │ ├── BridgeExample │ │ │ ├── Box.pde │ │ │ ├── Bridge.pde │ │ │ ├── BridgeExample.pde │ │ │ └── Particle.pde │ │ ├── BumpySurfaceNoise │ │ │ ├── BumpySurfaceNoise.pde │ │ │ ├── Particle.pde │ │ │ └── Surface.pde │ │ ├── BumpySurfaceSine │ │ │ ├── BumpySurfaceSine.pde │ │ │ ├── Particle.pde │ │ │ └── Surface.pde │ │ ├── BumpySurfaceStraightLineDemo │ │ │ ├── BumpySurfaceStraightLineDemo.pde │ │ │ ├── Particle.pde │ │ │ └── Surface.pde │ │ ├── CollisionListening │ │ │ ├── Boundary.pde │ │ │ ├── CollisionListening.pde │ │ │ └── Particle.pde │ │ ├── CollisionListeningDeletionExercise │ │ │ ├── Boundary.pde │ │ │ ├── CollisionListeningDeletionExercise.pde │ │ │ └── Particle.pde │ │ ├── CollisionsAndControl │ │ │ ├── Box.pde │ │ │ ├── CollisionsAndControl.pde │ │ │ ├── Particle.pde │ │ │ └── Spring.pde │ │ ├── CollisionsAndControlInterface │ │ │ ├── Box.pde │ │ │ ├── CollisionsAndControlInterface.pde │ │ │ ├── ContactListener.pde │ │ │ ├── Particle.pde │ │ │ └── Spring.pde │ │ ├── DistanceJointExample │ │ │ ├── Boundary.pde │ │ │ ├── DistanceJointExample.pde │ │ │ ├── Pair.pde │ │ │ └── Particle.pde │ │ ├── Liquidy │ │ │ ├── Boundary.pde │ │ │ ├── Liquidy.pde │ │ │ ├── Particle.pde │ │ │ └── ParticleSystem.pde │ │ ├── Mouse │ │ │ ├── Boundary.pde │ │ │ ├── Box.pde │ │ │ ├── Mouse.pde │ │ │ └── Spring.pde │ │ ├── MultiShapes │ │ │ ├── Boundary.pde │ │ │ ├── Lollipop.pde │ │ │ └── MultiShapes.pde │ │ ├── Polygons │ │ │ ├── Boundary.pde │ │ │ ├── CustomShape.pde │ │ │ └── Polygons.pde │ │ ├── RevoluteJointExample │ │ │ ├── Box.pde │ │ │ ├── Particle.pde │ │ │ ├── RevoluteJointExample.pde │ │ │ └── Windmill.pde │ │ ├── VectorStuff │ │ │ └── VectorStuff.pde │ │ ├── box2d_exercise │ │ │ ├── Box.pde │ │ │ └── box2d_exercise.pde │ │ └── box2d_exercise_solved │ │ │ ├── Box.pde │ │ │ └── box2d_exercise_solved.pde │ │ ├── library.properties │ │ ├── library │ │ ├── box2d_processing.jar │ │ └── jbox2d-library-2.3.1-SNAPSHOT.jar │ │ └── src │ │ ├── readme.txt │ │ └── shiffman │ │ └── box2d │ │ ├── Box2DContactListener.java │ │ └── Box2DProcessing.java ├── javadoc │ ├── QuickTest.html │ ├── allclasses-frame.html │ ├── allclasses-noframe.html │ ├── class-use │ │ └── QuickTest.html │ ├── constant-values.html │ ├── deprecated-list.html │ ├── help-doc.html │ ├── index-files │ │ ├── index-1.html │ │ ├── index-10.html │ │ ├── index-11.html │ │ ├── index-12.html │ │ ├── index-13.html │ │ ├── index-14.html │ │ ├── index-2.html │ │ ├── index-3.html │ │ ├── index-4.html │ │ ├── index-5.html │ │ ├── index-6.html │ │ ├── index-7.html │ │ ├── index-8.html │ │ └── index-9.html │ ├── index.html │ ├── overview-frame.html │ ├── overview-summary.html │ ├── overview-tree.html │ ├── package-frame.html │ ├── package-list │ ├── package-summary.html │ ├── package-tree.html │ ├── package-use.html │ ├── resources │ │ ├── background.gif │ │ ├── tab.gif │ │ ├── titlebar.gif │ │ └── titlebar_end.gif │ ├── shiffman │ │ └── box2d │ │ │ ├── Box2DContactListener.html │ │ │ ├── Box2DProcessing.html │ │ │ ├── class-use │ │ │ ├── Box2DContactListener.html │ │ │ └── Box2DProcessing.html │ │ │ ├── package-frame.html │ │ │ ├── package-summary.html │ │ │ ├── package-tree.html │ │ │ └── package-use.html │ └── stylesheet.css ├── lib │ ├── core.jar │ └── jbox2d-library-2.3.1-SNAPSHOT.jar └── src │ ├── QuickTest.java │ └── shiffman │ └── box2d │ ├── Box2DContactListener.java │ └── Box2DProcessing.java ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_STORE 2 | *.class 3 | .idea -------------------------------------------------------------------------------- /Box2D-for-Processing/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Box2D-for-Processing/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Box2D-for-Processing/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Box2D-for-Processing 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiffman/Box2D-for-Processing/43d58c546bf0b95c4dcda9f7a7d171c19477f120/Box2D-for-Processing/dist/box2d_processing.zip -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/ApplyForceAttractMouse/ApplyForceAttractMouse.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Basic example of falling rectangles 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.collision.shapes.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.*; 12 | 13 | // A reference to our box2d world 14 | Box2DProcessing box2d; 15 | 16 | // A list we'll use to track fixed objects 17 | ArrayList boundaries; 18 | // A list for all of our rectangles 19 | ArrayList boxes; 20 | 21 | void setup() { 22 | size(640,360); 23 | smooth(); 24 | 25 | // Initialize box2d physics and create the world 26 | box2d = new Box2DProcessing(this); 27 | box2d.createWorld(); 28 | // We are setting a custom gravity 29 | box2d.setGravity(0, -20); 30 | 31 | // Create ArrayLists 32 | boxes = new ArrayList(); 33 | boundaries = new ArrayList(); 34 | 35 | // Add a bunch of fixed boundaries 36 | boundaries.add(new Boundary(width/4,height-5,width/2-50,10)); 37 | boundaries.add(new Boundary(3*width/4,height-5,width/2-50,10)); 38 | boundaries.add(new Boundary(width-5,height/2,10,height)); 39 | boundaries.add(new Boundary(5,height/2,10,height)); 40 | } 41 | 42 | void draw() { 43 | background(255); 44 | 45 | // We must always step through time! 46 | box2d.step(); 47 | 48 | // When the mouse is clicked, add a new Box object 49 | if (random(1) < 0.1) { 50 | Box p = new Box(random(width),10); 51 | boxes.add(p); 52 | } 53 | 54 | if (mousePressed) { 55 | for (Box b: boxes) { 56 | b.attract(mouseX,mouseY); 57 | } 58 | } 59 | 60 | // Display all the boundaries 61 | for (Boundary wall: boundaries) { 62 | wall.display(); 63 | } 64 | 65 | // Display all the boxes 66 | for (Box b: boxes) { 67 | b.display(); 68 | } 69 | 70 | // Boxes that leave the screen, we delete them 71 | // (note they have to be deleted from both the box2d world and our list 72 | for (int i = boxes.size()-1; i >= 0; i--) { 73 | Box b = boxes.get(i); 74 | if (b.done()) { 75 | boxes.remove(i); 76 | } 77 | } 78 | 79 | fill(0); 80 | text("Click mouse to attract boxes",20,20); 81 | } 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/ApplyForceAttractMouse/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | 16 | // But we also have to make a body for box2d to know about it 17 | Body b; 18 | 19 | Boundary(float x_,float y_, float w_, float h_) { 20 | x = x_; 21 | y = y_; 22 | w = w_; 23 | h = h_; 24 | 25 | // Define the polygon 26 | PolygonShape sd = new PolygonShape(); 27 | // Figure out the box2d coordinates 28 | float box2dW = box2d.scalarPixelsToWorld(w/2); 29 | float box2dH = box2d.scalarPixelsToWorld(h/2); 30 | // We're just a box 31 | sd.setAsBox(box2dW, box2dH); 32 | 33 | 34 | // Create the body 35 | BodyDef bd = new BodyDef(); 36 | bd.type = BodyType.STATIC; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | } 43 | 44 | // Draw the boundary, if it were at an angle we'd have to do something fancier 45 | void display() { 46 | fill(0); 47 | stroke(0); 48 | rectMode(CENTER); 49 | rect(x,y,w,h); 50 | } 51 | 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/ApplyForceAttractMouse/Box.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A rectangular box 7 | class Box { 8 | 9 | // We need to keep track of a Body and a width and height 10 | Body body; 11 | float w; 12 | float h; 13 | 14 | // Constructor 15 | Box(float x, float y) { 16 | w = random(8,16); 17 | h = w; 18 | // Add the box to the box2d world 19 | makeBody(new Vec2(x,y),w,h); 20 | } 21 | 22 | // This function removes the particle from the box2d world 23 | void killBody() { 24 | box2d.destroyBody(body); 25 | } 26 | 27 | // Is the particle ready for deletion? 28 | boolean done() { 29 | // Let's find the screen position of the particle 30 | Vec2 pos = box2d.getBodyPixelCoord(body); 31 | // Is it off the bottom of the screen? 32 | if (pos.y > height+w*h) { 33 | killBody(); 34 | return true; 35 | } 36 | return false; 37 | } 38 | 39 | void attract(float x,float y) { 40 | // From BoxWrap2D example 41 | Vec2 worldTarget = box2d.coordPixelsToWorld(x,y); 42 | Vec2 bodyVec = body.getWorldCenter(); 43 | // First find the vector going from this body to the specified point 44 | worldTarget.subLocal(bodyVec); 45 | // Then, scale the vector to the specified force 46 | worldTarget.normalize(); 47 | worldTarget.mulLocal((float) 50); 48 | // Now apply it to the body's center of mass. 49 | body.applyForce(worldTarget, bodyVec); 50 | } 51 | 52 | 53 | // Drawing the box 54 | void display() { 55 | // We look at each body and get its screen position 56 | Vec2 pos = box2d.getBodyPixelCoord(body); 57 | // Get its angle of rotation 58 | float a = body.getAngle(); 59 | 60 | rectMode(CENTER); 61 | pushMatrix(); 62 | translate(pos.x,pos.y); 63 | rotate(-a); 64 | fill(175); 65 | stroke(0); 66 | rect(0,0,w,h); 67 | popMatrix(); 68 | } 69 | 70 | // This function adds the rectangle to the box2d world 71 | void makeBody(Vec2 center, float w_, float h_) { 72 | 73 | // Define a polygon (this is what we use for a rectangle) 74 | PolygonShape sd = new PolygonShape(); 75 | float box2dW = box2d.scalarPixelsToWorld(w_/2); 76 | float box2dH = box2d.scalarPixelsToWorld(h_/2); 77 | sd.setAsBox(box2dW, box2dH); 78 | 79 | // Define a fixture 80 | FixtureDef fd = new FixtureDef(); 81 | fd.shape = sd; 82 | // Parameters that affect physics 83 | fd.density = 1; 84 | fd.friction = 0.3; 85 | fd.restitution = 0.5; 86 | 87 | // Define the body and make it from the shape 88 | BodyDef bd = new BodyDef(); 89 | bd.type = BodyType.DYNAMIC; 90 | bd.position.set(box2d.coordPixelsToWorld(center)); 91 | 92 | body = box2d.createBody(bd); 93 | body.createFixture(fd); 94 | } 95 | } 96 | 97 | 98 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/ApplyForceSimpleWind/ApplyForceSimpleWind.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Basic example of falling rectangles 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.collision.shapes.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.*; 12 | 13 | // A reference to our box2d world 14 | Box2DProcessing box2d; 15 | 16 | // A list we'll use to track fixed objects 17 | ArrayList boundaries; 18 | // A list for all of our rectangles 19 | ArrayList boxes; 20 | 21 | void setup() { 22 | size(640,360); 23 | smooth(); 24 | 25 | // Initialize box2d physics and create the world 26 | box2d = new Box2DProcessing(this); 27 | box2d.createWorld(); 28 | // We are setting a custom gravity 29 | box2d.setGravity(0, -20); 30 | 31 | // Create ArrayLists 32 | boxes = new ArrayList(); 33 | boundaries = new ArrayList(); 34 | 35 | // Add a bunch of fixed boundaries 36 | boundaries.add(new Boundary(width/4,height-5,width/2-100,10)); 37 | boundaries.add(new Boundary(3*width/4,height-5,width/2-100,10)); 38 | boundaries.add(new Boundary(width-5,height/2,10,height)); 39 | boundaries.add(new Boundary(5,height/2,10,height)); 40 | } 41 | 42 | void draw() { 43 | background(255); 44 | 45 | // We must always step through time! 46 | box2d.step(); 47 | 48 | // When the mouse is clicked, add a new Box object 49 | if (random(1) < 0.1) { 50 | Box p = new Box(random(width),10); 51 | boxes.add(p); 52 | } 53 | 54 | if (mousePressed) { 55 | for (Box b: boxes) { 56 | Vec2 wind = new Vec2(20,0); 57 | b.applyForce(wind); 58 | } 59 | } 60 | 61 | // Display all the boundaries 62 | for (Boundary wall: boundaries) { 63 | wall.display(); 64 | } 65 | 66 | // Display all the boxes 67 | for (Box b: boxes) { 68 | b.display(); 69 | } 70 | 71 | // Boxes that leave the screen, we delete them 72 | // (note they have to be deleted from both the box2d world and our list 73 | for (int i = boxes.size()-1; i >= 0; i--) { 74 | Box b = boxes.get(i); 75 | if (b.done()) { 76 | boxes.remove(i); 77 | } 78 | } 79 | 80 | fill(0); 81 | text("Click mouse to apply a wind force.",20,20); 82 | } 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/ApplyForceSimpleWind/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | 16 | // But we also have to make a body for box2d to know about it 17 | Body b; 18 | 19 | Boundary(float x_,float y_, float w_, float h_) { 20 | x = x_; 21 | y = y_; 22 | w = w_; 23 | h = h_; 24 | 25 | // Define the polygon 26 | PolygonShape sd = new PolygonShape(); 27 | // Figure out the box2d coordinates 28 | float box2dW = box2d.scalarPixelsToWorld(w/2); 29 | float box2dH = box2d.scalarPixelsToWorld(h/2); 30 | // We're just a box 31 | sd.setAsBox(box2dW, box2dH); 32 | 33 | 34 | // Create the body 35 | BodyDef bd = new BodyDef(); 36 | bd.type = BodyType.STATIC; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | } 43 | 44 | // Draw the boundary, if it were at an angle we'd have to do something fancier 45 | void display() { 46 | fill(0); 47 | stroke(0); 48 | rectMode(CENTER); 49 | rect(x,y,w,h); 50 | } 51 | 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/ApplyForceSimpleWind/Box.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A rectangular box 7 | class Box { 8 | 9 | // We need to keep track of a Body and a width and height 10 | Body body; 11 | float w; 12 | float h; 13 | 14 | // Constructor 15 | Box(float x, float y) { 16 | w = random(8, 16); 17 | h = w; 18 | // Add the box to the box2d world 19 | makeBody(new Vec2(x, y), w, h); 20 | } 21 | 22 | // This function removes the particle from the box2d world 23 | void killBody() { 24 | box2d.destroyBody(body); 25 | } 26 | 27 | // Is the particle ready for deletion? 28 | boolean done() { 29 | // Let's find the screen position of the particle 30 | Vec2 pos = box2d.getBodyPixelCoord(body); 31 | // Is it off the bottom of the screen? 32 | if (pos.y > height+w*h) { 33 | killBody(); 34 | return true; 35 | } 36 | return false; 37 | } 38 | 39 | void applyForce(Vec2 force) { 40 | Vec2 pos = body.getWorldCenter(); 41 | body.applyForce(force, pos); 42 | } 43 | 44 | // Drawing the box 45 | void display() { 46 | // We look at each body and get its screen position 47 | Vec2 pos = box2d.getBodyPixelCoord(body); 48 | // Get its angle of rotation 49 | float a = body.getAngle(); 50 | 51 | rectMode(CENTER); 52 | pushMatrix(); 53 | translate(pos.x, pos.y); 54 | rotate(-a); 55 | fill(175); 56 | stroke(0); 57 | rect(0, 0, w, h); 58 | popMatrix(); 59 | } 60 | 61 | // This function adds the rectangle to the box2d world 62 | void makeBody(Vec2 center, float w_, float h_) { 63 | 64 | // Define a polygon (this is what we use for a rectangle) 65 | PolygonShape sd = new PolygonShape(); 66 | float box2dW = box2d.scalarPixelsToWorld(w_/2); 67 | float box2dH = box2d.scalarPixelsToWorld(h_/2); 68 | sd.setAsBox(box2dW, box2dH); 69 | 70 | // Define a fixture 71 | FixtureDef fd = new FixtureDef(); 72 | fd.shape = sd; 73 | // Parameters that affect physics 74 | fd.density = 1; 75 | fd.friction = 0.3; 76 | fd.restitution = 0.2; 77 | 78 | // Define the body and make it from the shape 79 | BodyDef bd = new BodyDef(); 80 | bd.type = BodyType.DYNAMIC; 81 | bd.position.set(box2d.coordPixelsToWorld(center)); 82 | bd.angle = random(TWO_PI); 83 | 84 | body = box2d.createBody(bd); 85 | body.createFixture(fd); 86 | } 87 | } 88 | 89 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/AttractionApplyForce/AttractionApplyForce.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Showing how to use applyForce() with box2d 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.collision.shapes.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.*; 12 | 13 | // A reference to our box2d world 14 | Box2DProcessing box2d; 15 | 16 | // Movers, jsut like before! 17 | Mover[] movers = new Mover[25]; 18 | 19 | // Attractor, just like before! 20 | Attractor a; 21 | 22 | void setup() { 23 | size(640,360); 24 | smooth(); 25 | 26 | box2d = new Box2DProcessing(this); 27 | box2d.createWorld(); 28 | // No global gravity force 29 | box2d.setGravity(0,0); 30 | 31 | for (int i = 0; i < movers.length; i++) { 32 | movers[i] = new Mover(random(8,16),random(width),random(height)); 33 | } 34 | a = new Attractor(32,width/2,height/2); 35 | } 36 | 37 | void draw() { 38 | background(255); 39 | 40 | // We must always step through time! 41 | box2d.step(); 42 | 43 | a.display(); 44 | 45 | for (int i = 0; i < movers.length; i++) { 46 | // Look, this is just like what we had before! 47 | Vec2 force = a.attract(movers[i]); 48 | movers[i].applyForce(force); 49 | movers[i].display(); 50 | } 51 | } 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/AttractionApplyForce/Attractor.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Showing how to use applyForce() with box2d 7 | 8 | // Fixed Attractor (this is redundant with Mover) 9 | 10 | class Attractor { 11 | 12 | // We need to keep track of a Body and a radius 13 | Body body; 14 | float r; 15 | 16 | Attractor(float r_, float x, float y) { 17 | r = r_; 18 | // Define a body 19 | BodyDef bd = new BodyDef(); 20 | bd.type = BodyType.STATIC; 21 | // Set its position 22 | bd.position = box2d.coordPixelsToWorld(x,y); 23 | body = box2d.world.createBody(bd); 24 | 25 | // Make the body's shape a circle 26 | CircleShape cs = new CircleShape(); 27 | cs.m_radius = box2d.scalarPixelsToWorld(r); 28 | 29 | body.createFixture(cs,1); 30 | 31 | } 32 | 33 | 34 | // Formula for gravitational attraction 35 | // We are computing this in "world" coordinates 36 | // No need to convert to pixels and back 37 | Vec2 attract(Mover m) { 38 | float G = 100; // Strength of force 39 | // clone() makes us a copy 40 | Vec2 pos = body.getWorldCenter(); 41 | Vec2 moverPos = m.body.getWorldCenter(); 42 | // Vector pointing from mover to attractor 43 | Vec2 force = pos.sub(moverPos); 44 | float distance = force.length(); 45 | // Keep force within bounds 46 | distance = constrain(distance,1,5); 47 | force.normalize(); 48 | // Note the attractor's mass is 0 because it's fixed so can't use that 49 | float strength = (G * 1 * m.body.m_mass) / (distance * distance); // Calculate gravitional force magnitude 50 | force.mulLocal(strength); // Get force vector --> magnitude * direction 51 | return force; 52 | } 53 | 54 | void display() { 55 | // We look at each body and get its screen position 56 | Vec2 pos = box2d.getBodyPixelCoord(body); 57 | // Get its angle of rotation 58 | float a = body.getAngle(); 59 | pushMatrix(); 60 | translate(pos.x,pos.y); 61 | rotate(a); 62 | fill(0); 63 | stroke(0); 64 | strokeWeight(1); 65 | ellipse(0,0,r*2,r*2); 66 | popMatrix(); 67 | } 68 | } 69 | 70 | 71 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/AttractionApplyForce/Mover.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Showing how to use applyForce() with box2d 7 | 8 | class Mover { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | Mover(float r_, float x, float y) { 15 | r = r_; 16 | // Define a body 17 | BodyDef bd = new BodyDef(); 18 | bd.type = BodyType.DYNAMIC; 19 | 20 | // Set its position 21 | bd.position = box2d.coordPixelsToWorld(x,y); 22 | body = box2d.world.createBody(bd); 23 | 24 | // Make the body's shape a circle 25 | CircleShape cs = new CircleShape(); 26 | cs.m_radius = box2d.scalarPixelsToWorld(r); 27 | 28 | // Define a fixture 29 | FixtureDef fd = new FixtureDef(); 30 | fd.shape = cs; 31 | // Parameters that affect physics 32 | fd.density = 1; 33 | fd.friction = 0.3; 34 | fd.restitution = 0.5; 35 | 36 | body.createFixture(fd); 37 | 38 | body.setLinearVelocity(new Vec2(random(-5,5),random(-5,-5))); 39 | body.setAngularVelocity(random(-1,1)); 40 | } 41 | 42 | void applyForce(Vec2 v) { 43 | body.applyForce(v, body.getWorldCenter()); 44 | } 45 | 46 | 47 | void display() { 48 | // We look at each body and get its screen position 49 | Vec2 pos = box2d.getBodyPixelCoord(body); 50 | // Get its angle of rotation 51 | float a = body.getAngle(); 52 | pushMatrix(); 53 | translate(pos.x,pos.y); 54 | rotate(a); 55 | fill(150); 56 | stroke(0); 57 | strokeWeight(1); 58 | ellipse(0,0,r*2,r*2); 59 | // Let's add a line so we can see the rotation 60 | line(0,0,r,0); 61 | popMatrix(); 62 | } 63 | } 64 | 65 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Blobby/Blob.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A blob skeleton 7 | // Could be used to create blobbly characters a la Nokia Friends 8 | // http://postspectacular.com/work/nokia/friends/start 9 | 10 | class Blob { 11 | 12 | // A list to keep track of all the points in our blob 13 | ArrayList skeleton; 14 | 15 | float bodyRadius; // The radius of each body that makes up the skeleton 16 | float radius; // The radius of the entire blob 17 | float totalPoints; // How many points make up the blob 18 | 19 | 20 | // We should modify this constructor to receive arguments 21 | // So that we can make many different types of blobs 22 | Blob() { 23 | 24 | // Create the empty 25 | skeleton = new ArrayList(); 26 | 27 | // Let's make a volume of joints! 28 | ConstantVolumeJointDef cvjd = new ConstantVolumeJointDef(); 29 | 30 | // Where and how big is the blob 31 | Vec2 center = new Vec2(width/2, height/2); 32 | radius = 100; 33 | totalPoints = 20; 34 | bodyRadius = 12; 35 | 36 | 37 | // Initialize all the points 38 | for (int i = 0; i < totalPoints; i++) { 39 | // Look polar to cartesian coordinate transformation! 40 | float theta = PApplet.map(i, 0, totalPoints, 0, TWO_PI); 41 | float x = center.x + radius * sin(theta); 42 | float y = center.y + radius * cos(theta); 43 | 44 | // Make each individual body 45 | BodyDef bd = new BodyDef(); 46 | bd.type = BodyType.DYNAMIC; 47 | 48 | bd.fixedRotation = true; // no rotation! 49 | bd.position.set(box2d.coordPixelsToWorld(x, y)); 50 | Body body = box2d.createBody(bd); 51 | 52 | // The body is a circle 53 | CircleShape cs = new CircleShape(); 54 | cs.m_radius = box2d.scalarPixelsToWorld(bodyRadius); 55 | 56 | // Define a fixture 57 | FixtureDef fd = new FixtureDef(); 58 | fd.shape = cs; 59 | 60 | // For filtering out collisions 61 | //fd.filter.groupIndex = -2; 62 | 63 | // Parameters that affect physics 64 | fd.density = 1; 65 | 66 | // Finalize the body 67 | body.createFixture(fd); 68 | // Add it to the volume 69 | cvjd.addBody(body); 70 | 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 http://postspectacular.com/work/nokia/friends/start 87 | void display() { 88 | 89 | // Draw the outline 90 | beginShape(); 91 | noFill(); 92 | stroke(0); 93 | strokeWeight(1); 94 | for (Body b: skeleton) { 95 | Vec2 pos = box2d.getBodyPixelCoord(b); 96 | vertex(pos.x, pos.y); 97 | } 98 | endShape(CLOSE); 99 | 100 | // Draw the individual circles 101 | for (Body b: skeleton) { 102 | // We look at each body and get its screen position 103 | Vec2 pos = box2d.getBodyPixelCoord(b); 104 | // Get its angle of rotation 105 | float a = b.getAngle(); 106 | pushMatrix(); 107 | translate(pos.x, pos.y); 108 | rotate(a); 109 | fill(175); 110 | stroke(0); 111 | strokeWeight(1); 112 | ellipse(0, 0, bodyRadius*2, bodyRadius*2); 113 | popMatrix(); 114 | } 115 | } 116 | } 117 | 118 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Blobby/Blobby.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A blob skeleton 7 | // Could be used to create blobbly characters a la Nokia Friends 8 | // http://postspectacular.com/work/nokia/friends/start 9 | 10 | // This seems to be broken with the Box2D 2.1.2 version I'm using 11 | 12 | import shiffman.box2d.*; 13 | 14 | import org.jbox2d.collision.shapes.*; 15 | import org.jbox2d.common.*; 16 | import org.jbox2d.dynamics.*; 17 | import org.jbox2d.dynamics.joints.*; 18 | 19 | // A reference to our box2d world 20 | Box2DProcessing box2d; 21 | 22 | // A list we'll use to track fixed objects 23 | ArrayList boundaries; 24 | 25 | // Our "blob" object 26 | Blob blob; 27 | 28 | void setup() { 29 | size(400,300); 30 | smooth(); 31 | 32 | // Initialize box2d physics and create the world 33 | box2d = new Box2DProcessing(this); 34 | box2d.createWorld(); 35 | 36 | // Add some boundaries 37 | boundaries = new ArrayList(); 38 | boundaries.add(new Boundary(width/2,height-5,width,10)); 39 | boundaries.add(new Boundary(width/2,5,width,10)); 40 | boundaries.add(new Boundary(width-5,height/2,10,height)); 41 | boundaries.add(new Boundary(5,height/2,10,height)); 42 | 43 | // Make a new blob 44 | blob = new Blob(); 45 | } 46 | 47 | void draw() { 48 | background(255); 49 | 50 | // We must always step through time! 51 | box2d.step(); 52 | 53 | // Show the blob! 54 | blob.display(); 55 | 56 | // Show the boundaries! 57 | for (Boundary wall: boundaries) { 58 | wall.display(); 59 | } 60 | 61 | 62 | } 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Blobby/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | 16 | // But we also have to make a body for box2d to know about it 17 | Body b; 18 | 19 | Boundary(float x_,float y_, float w_, float h_) { 20 | x = x_; 21 | y = y_; 22 | w = w_; 23 | h = h_; 24 | 25 | // Define the polygon 26 | PolygonShape sd = new PolygonShape(); 27 | // Figure out the box2d coordinates 28 | float box2dW = box2d.scalarPixelsToWorld(w/2); 29 | float box2dH = box2d.scalarPixelsToWorld(h/2); 30 | // We're just a box 31 | sd.setAsBox(box2dW, box2dH); 32 | 33 | 34 | // Create the body 35 | BodyDef bd = new BodyDef(); 36 | bd.type = BodyType.STATIC; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | } 43 | 44 | // Draw the boundary, if it were at an angle we'd have to do something fancier 45 | void display() { 46 | fill(0); 47 | stroke(0); 48 | rectMode(CENTER); 49 | rect(x,y,w,h); 50 | } 51 | 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Boxes/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | 16 | // But we also have to make a body for box2d to know about it 17 | Body b; 18 | 19 | Boundary(float x_,float y_, float w_, float h_) { 20 | x = x_; 21 | y = y_; 22 | w = w_; 23 | h = h_; 24 | 25 | // Define the polygon 26 | PolygonShape sd = new PolygonShape(); 27 | // Figure out the box2d coordinates 28 | float box2dW = box2d.scalarPixelsToWorld(w/2); 29 | float box2dH = box2d.scalarPixelsToWorld(h/2); 30 | // We're just a box 31 | sd.setAsBox(box2dW, box2dH); 32 | 33 | 34 | // Create the body 35 | BodyDef bd = new BodyDef(); 36 | bd.type = BodyType.STATIC; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | } 43 | 44 | // Draw the boundary, if it were at an angle we'd have to do something fancier 45 | void display() { 46 | fill(0); 47 | stroke(0); 48 | rectMode(CENTER); 49 | rect(x,y,w,h); 50 | } 51 | 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Boxes/Box.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A rectangular box 7 | class Box { 8 | 9 | // We need to keep track of a Body and a width and height 10 | Body body; 11 | float w; 12 | float h; 13 | 14 | // Constructor 15 | Box(float x, float y) { 16 | w = random(4, 16); 17 | h = random(4, 16); 18 | // Add the box to the box2d world 19 | makeBody(new Vec2(x, y), w, h); 20 | } 21 | 22 | // This function removes the particle from the box2d world 23 | void killBody() { 24 | box2d.destroyBody(body); 25 | } 26 | 27 | // Is the particle ready for deletion? 28 | boolean done() { 29 | // Let's find the screen position of the particle 30 | Vec2 pos = box2d.getBodyPixelCoord(body); 31 | // Is it off the bottom of the screen? 32 | if (pos.y > height+w*h) { 33 | killBody(); 34 | return true; 35 | } 36 | return false; 37 | } 38 | 39 | // Drawing the box 40 | void display() { 41 | // We look at each body and get its screen position 42 | Vec2 pos = box2d.getBodyPixelCoord(body); 43 | // Get its angle of rotation 44 | float a = body.getAngle(); 45 | 46 | rectMode(CENTER); 47 | pushMatrix(); 48 | translate(pos.x, pos.y); 49 | rotate(-a); 50 | fill(175); 51 | stroke(0); 52 | rect(0, 0, w, h); 53 | popMatrix(); 54 | } 55 | 56 | // This function adds the rectangle to the box2d world 57 | void makeBody(Vec2 center, float w_, float h_) { 58 | 59 | // Define a polygon (this is what we use for a rectangle) 60 | PolygonShape sd = new PolygonShape(); 61 | float box2dW = box2d.scalarPixelsToWorld(w_/2); 62 | float box2dH = box2d.scalarPixelsToWorld(h_/2); 63 | sd.setAsBox(box2dW, box2dH); 64 | 65 | // Define a fixture 66 | FixtureDef fd = new FixtureDef(); 67 | fd.shape = sd; 68 | // Parameters that affect physics 69 | fd.density = 1; 70 | fd.friction = 0.3; 71 | fd.restitution = 0.5; 72 | 73 | // Define the body and make it from the shape 74 | BodyDef bd = new BodyDef(); 75 | bd.type = BodyType.DYNAMIC; 76 | bd.position.set(box2d.coordPixelsToWorld(center)); 77 | 78 | body = box2d.createBody(bd); 79 | body.createFixture(fd); 80 | 81 | // Give it some initial random velocity 82 | body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); 83 | body.setAngularVelocity(random(-5, 5)); 84 | } 85 | } 86 | 87 | 88 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Boxes/Boxes.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Basic example of falling rectangles 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.collision.shapes.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.*; 12 | 13 | // A reference to our box2d world 14 | Box2DProcessing box2d; 15 | 16 | // A list we'll use to track fixed objects 17 | ArrayList boundaries; 18 | // A list for all of our rectangles 19 | ArrayList boxes; 20 | 21 | void setup() { 22 | size(640,360); 23 | smooth(); 24 | 25 | // Initialize box2d physics and create the world 26 | box2d = new Box2DProcessing(this); 27 | box2d.createWorld(); 28 | // We are setting a custom gravity 29 | box2d.setGravity(0, -10); 30 | 31 | // Create ArrayLists 32 | boxes = new ArrayList(); 33 | boundaries = new ArrayList(); 34 | 35 | // Add a bunch of fixed boundaries 36 | boundaries.add(new Boundary(width/4,height-5,width/2-50,10)); 37 | boundaries.add(new Boundary(3*width/4,height-50,width/2-50,10)); 38 | } 39 | 40 | void draw() { 41 | background(255); 42 | 43 | // We must always step through time! 44 | box2d.step(); 45 | 46 | // Boxes fall from the top every so often 47 | if (random(1) < 0.2) { 48 | Box p = new Box(width/2,30); 49 | boxes.add(p); 50 | } 51 | 52 | // Display all the boundaries 53 | for (Boundary wall: boundaries) { 54 | wall.display(); 55 | } 56 | 57 | // Display all the boxes 58 | for (Box b: boxes) { 59 | b.display(); 60 | } 61 | 62 | // Boxes that leave the screen, we delete them 63 | // (note they have to be deleted from both the box2d world and our list 64 | for (int i = boxes.size()-1; i >= 0; i--) { 65 | Box b = boxes.get(i); 66 | if (b.done()) { 67 | boxes.remove(i); 68 | } 69 | } 70 | } 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BridgeExample/Box.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A rectangular box 7 | class Box { 8 | 9 | // We need to keep track of a Body and a width and height 10 | Body body; 11 | float w; 12 | float h; 13 | 14 | // Constructor 15 | Box(float x, float y) { 16 | w = random(4,16); 17 | h = random(4,16); 18 | // Add the box to the box2d world 19 | makeBody(new Vec2(x,y),w,h); 20 | } 21 | 22 | // This function removes the particle from the box2d world 23 | void killBody() { 24 | box2d.destroyBody(body); 25 | } 26 | 27 | // Is the particle ready for deletion? 28 | boolean done() { 29 | // Let's find the screen position of the particle 30 | Vec2 pos = box2d.getBodyPixelCoord(body); 31 | // Is it off the bottom of the screen? 32 | if (pos.y > height+w*h) { 33 | killBody(); 34 | return true; 35 | } 36 | return false; 37 | } 38 | 39 | // Drawing the box 40 | void display() { 41 | // We look at each body and get its screen position 42 | Vec2 pos = box2d.getBodyPixelCoord(body); 43 | // Get its angle of rotation 44 | float a = body.getAngle(); 45 | 46 | rectMode(CENTER); 47 | pushMatrix(); 48 | translate(pos.x,pos.y); 49 | rotate(-a); 50 | fill(175); 51 | stroke(0); 52 | rect(0,0,w,h); 53 | popMatrix(); 54 | } 55 | 56 | // This function adds the rectangle to the box2d world 57 | void makeBody(Vec2 center, float w_, float h_) { 58 | 59 | // Define a polygon (this is what we use for a rectangle) 60 | PolygonShape sd = new PolygonShape(); 61 | float box2dW = box2d.scalarPixelsToWorld(w_/2); 62 | float box2dH = box2d.scalarPixelsToWorld(h_/2); 63 | sd.setAsBox(box2dW, box2dH); 64 | 65 | // Define a fixture 66 | FixtureDef fd = new FixtureDef(); 67 | fd.shape = sd; 68 | // Parameters that affect physics 69 | fd.density = 1; 70 | fd.friction = 0.3; 71 | fd.restitution = 0.5; 72 | 73 | // Define the body and make it from the shape 74 | BodyDef bd = new BodyDef(); 75 | bd.type = BodyType.DYNAMIC; 76 | bd.position.set(box2d.coordPixelsToWorld(center)); 77 | 78 | body = box2d.createBody(bd); 79 | body.createFixture(fd); 80 | //body.setMassFromShapes(); 81 | 82 | // Give it some initial random velocity 83 | body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); 84 | body.setAngularVelocity(random(-5, 5)); 85 | } 86 | 87 | } 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BridgeExample/Bridge.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Series of Particles connected with distance joints 7 | 8 | class Bridge { 9 | 10 | // Bridge properties 11 | float totalLength; // How long 12 | int numPoints; // How many points 13 | 14 | // Our chain is a list of particles 15 | ArrayList particles; 16 | 17 | // Chain constructor 18 | Bridge(float l, int n) { 19 | 20 | totalLength = l; 21 | numPoints = n; 22 | 23 | particles = new ArrayList(); 24 | 25 | float len = totalLength / numPoints; 26 | 27 | // Here is the real work, go through and add particles to the chain itself 28 | for(int i=0; i < numPoints+1; i++) { 29 | // Make a new particle 30 | Particle p = null; 31 | 32 | // First and last particles are made with density of zero 33 | if (i == 0 || i == numPoints) p = new Particle(i*len,height/2,4,true); 34 | else p = new Particle(i*len,height/2,4,false); 35 | particles.add(p); 36 | 37 | // Connect the particles with a distance joint 38 | if (i > 0) { 39 | DistanceJointDef djd = new DistanceJointDef(); 40 | Particle previous = particles.get(i-1); 41 | // Connection between previous particle and this one 42 | djd.bodyA = previous.body; 43 | djd.bodyB = p.body; 44 | // Equilibrium length 45 | djd.length = box2d.scalarPixelsToWorld(len); 46 | // These properties affect how springy the joint is 47 | djd.frequencyHz = 0; 48 | djd.dampingRatio = 0; 49 | 50 | // Make the joint. Note we aren't storing a reference to the joint ourselves anywhere! 51 | // We might need to someday, but for now it's ok 52 | DistanceJoint dj = (DistanceJoint) box2d.world.createJoint(djd); 53 | } 54 | } 55 | } 56 | 57 | // Draw the bridge 58 | void display() { 59 | for (Particle p: particles) { 60 | p.display(); 61 | } 62 | } 63 | 64 | } 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BridgeExample/BridgeExample.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Example demonstrating distance joints 7 | // A bridge is formed by connected a series of particles with joints 8 | 9 | import shiffman.box2d.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.joints.*; 12 | import org.jbox2d.collision.shapes.*; 13 | import org.jbox2d.collision.shapes.Shape; 14 | import org.jbox2d.common.*; 15 | import org.jbox2d.dynamics.*; 16 | import org.jbox2d.dynamics.contacts.*; 17 | 18 | // A reference to our box2d world 19 | Box2DProcessing box2d; 20 | 21 | // An object to describe a Bridget (a list of particles with joint connections) 22 | Bridge bridge; 23 | 24 | // A list for all of our rectangles 25 | ArrayList boxes; 26 | 27 | void setup() { 28 | size(640,360); 29 | smooth(); 30 | 31 | // Initialize box2d physics and create the world 32 | box2d = new Box2DProcessing(this); 33 | box2d.createWorld(); 34 | 35 | 36 | // Make the bridge 37 | bridge = new Bridge(width,width/10); 38 | 39 | // Create ArrayLists 40 | boxes = new ArrayList(); 41 | 42 | } 43 | 44 | void draw() { 45 | background(255); 46 | 47 | // We must always step through time! 48 | box2d.step(); 49 | 50 | 51 | // When the mouse is clicked, add a new Box object 52 | if (mousePressed) { 53 | Box p = new Box(mouseX,mouseY); 54 | boxes.add(p); 55 | } 56 | 57 | // Display all the boxes 58 | for (Box b: boxes) { 59 | b.display(); 60 | } 61 | 62 | // Boxes that leave the screen, we delete them 63 | // (note they have to be deleted from both the box2d world and our list 64 | for (int i = boxes.size()-1; i >= 0; i--) { 65 | Box b = boxes.get(i); 66 | if (b.done()) { 67 | boxes.remove(i); 68 | } 69 | } 70 | 71 | // Draw the windmill 72 | bridge.display(); 73 | 74 | 75 | fill(0); 76 | text("Click mouse to add boxes.",10,height-10); 77 | 78 | 79 | } 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BridgeExample/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A circular particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | color col; 15 | 16 | Particle(float x, float y, float r_, boolean fixed) { 17 | r = r_; 18 | 19 | // Define a body 20 | BodyDef bd = new BodyDef(); 21 | if (fixed) bd.type = BodyType.STATIC; 22 | else bd.type = BodyType.DYNAMIC; 23 | 24 | // Set its position 25 | bd.position = box2d.coordPixelsToWorld(x,y); 26 | body = box2d.world.createBody(bd); 27 | 28 | // Make the body's shape a circle 29 | // Make the body's shape a circle 30 | CircleShape cs = new CircleShape(); 31 | cs.m_radius = box2d.scalarPixelsToWorld(r); 32 | 33 | FixtureDef fd = new FixtureDef(); 34 | fd.shape = cs; 35 | // Parameters that affect physics 36 | fd.density = 1; 37 | fd.friction = 0.3; 38 | fd.restitution = 0.5; 39 | 40 | body.createFixture(fd); 41 | 42 | col = color(175); 43 | } 44 | 45 | // This function removes the particle from the box2d world 46 | void killBody() { 47 | box2d.destroyBody(body); 48 | } 49 | 50 | // Is the particle ready for deletion? 51 | boolean done() { 52 | // Let's find the screen position of the particle 53 | Vec2 pos = box2d.getBodyPixelCoord(body); 54 | // Is it off the bottom of the screen? 55 | if (pos.y > height+r*2) { 56 | killBody(); 57 | return true; 58 | } 59 | return false; 60 | } 61 | 62 | // 63 | void display() { 64 | // We look at each body and get its screen position 65 | Vec2 pos = box2d.getBodyPixelCoord(body); 66 | // Get its angle of rotation 67 | float a = body.getAngle(); 68 | pushMatrix(); 69 | translate(pos.x,pos.y); 70 | rotate(a); 71 | fill(col); 72 | stroke(0); 73 | strokeWeight(1); 74 | ellipse(0,0,r*2,r*2); 75 | // Let's add a line so we can see the rotation 76 | line(0,0,r,0); 77 | popMatrix(); 78 | } 79 | 80 | 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BumpySurfaceNoise/BumpySurfaceNoise.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // An uneven surface 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.collision.shapes.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.*; 12 | import org.jbox2d.dynamics.*; 13 | 14 | // A reference to our box2d world 15 | Box2DProcessing box2d; 16 | 17 | // An ArrayList of particles that will fall on the surface 18 | ArrayList particles; 19 | 20 | // An object to store information about the uneven surface 21 | Surface surface; 22 | 23 | void setup() { 24 | size(500,300); 25 | smooth(); 26 | 27 | // Initialize box2d physics and create the world 28 | box2d = new Box2DProcessing(this); 29 | box2d.createWorld(); 30 | // We are setting a custom gravity 31 | box2d.setGravity(0, -20); 32 | 33 | // Create the empty list 34 | particles = new ArrayList(); 35 | // Create the surface 36 | surface = new Surface(); 37 | } 38 | 39 | void draw() { 40 | // If the mouse is pressed, we make new particles 41 | if (mousePressed) { 42 | float sz = random(2,6); 43 | particles.add(new Particle(mouseX,mouseY,sz)); 44 | } 45 | 46 | // We must always step through time! 47 | box2d.step(); 48 | 49 | background(255); 50 | 51 | // Draw the surface 52 | surface.display(); 53 | 54 | // Draw all particles 55 | for (Particle p: particles) { 56 | p.display(); 57 | } 58 | 59 | // Particles that leave the screen, we delete them 60 | // (note they have to be deleted from both the box2d world and our list 61 | for (int i = particles.size()-1; i >= 0; i--) { 62 | Particle p = particles.get(i); 63 | if (p.done()) { 64 | particles.remove(i); 65 | } 66 | } 67 | 68 | // Just drawing the framerate to see how many particles it can handle 69 | fill(0); 70 | text("framerate: " + (int)frameRate,12,16); 71 | } 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BumpySurfaceNoise/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A circular particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | Particle(float x, float y, float r_) { 15 | r = r_; 16 | // This function puts the particle in the Box2d world 17 | makeBody(x,y,r); 18 | } 19 | 20 | // This function removes the particle from the box2d world 21 | void killBody() { 22 | box2d.destroyBody(body); 23 | } 24 | 25 | // Is the particle ready for deletion? 26 | boolean done() { 27 | // Let's find the screen position of the particle 28 | Vec2 pos = box2d.getBodyPixelCoord(body); 29 | // Is it off the bottom of the screen? 30 | if (pos.y > height+r*2) { 31 | killBody(); 32 | return true; 33 | } 34 | return false; 35 | } 36 | 37 | // 38 | void display() { 39 | // We look at each body and get its screen position 40 | Vec2 pos = box2d.getBodyPixelCoord(body); 41 | // Get its angle of rotation 42 | float a = body.getAngle(); 43 | pushMatrix(); 44 | translate(pos.x,pos.y); 45 | rotate(-a); 46 | fill(175); 47 | stroke(0); 48 | strokeWeight(1); 49 | ellipse(0,0,r*2,r*2); 50 | // Let's add a line so we can see the rotation 51 | line(0,0,r,0); 52 | popMatrix(); 53 | } 54 | 55 | // Here's our function that adds the particle to the Box2D world 56 | void makeBody(float x, float y, float r) { 57 | // Define a body 58 | BodyDef bd = new BodyDef(); 59 | // Set its position 60 | bd.position = box2d.coordPixelsToWorld(x,y); 61 | bd.type = BodyType.DYNAMIC; 62 | body = box2d.world.createBody(bd); 63 | 64 | // Make the body's shape a circle 65 | CircleShape cs = new CircleShape(); 66 | cs.m_radius = box2d.scalarPixelsToWorld(r); 67 | 68 | FixtureDef fd = new FixtureDef(); 69 | fd.shape = cs; 70 | // Parameters that affect physics 71 | fd.density = 1; 72 | fd.friction = 0.01; 73 | fd.restitution = 0.3; 74 | 75 | // Attach fixture to body 76 | body.createFixture(fd); 77 | 78 | // Give it a random initial velocity (and angular velocity) 79 | body.setLinearVelocity(new Vec2(random(-10f,10f),random(5f,10f))); 80 | body.setAngularVelocity(random(-10,10)); 81 | } 82 | 83 | 84 | 85 | 86 | 87 | 88 | } 89 | 90 | 91 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BumpySurfaceNoise/Surface.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // An uneven surface boundary 7 | 8 | class Surface { 9 | // We'll keep track of all of the surface points 10 | ArrayList surface; 11 | 12 | 13 | Surface() { 14 | surface = new ArrayList(); 15 | 16 | // This is what box2d uses to put the surface in its world 17 | ChainShape chain = new ChainShape(); 18 | 19 | // Perlin noise argument 20 | float xoff = 0.0; 21 | 22 | // This has to go backwards so that the objects bounce off the top of the surface 23 | // This "edgechain" will only work in one direction! 24 | for (float x = width+10; x > -10; x -= 5) { 25 | 26 | // Doing some stuff with perlin noise to calculate a surface that points down on one side 27 | // and up on the other 28 | float y; 29 | if (x > width/2) { 30 | y = 100 + (width - x)*1.1 + map(noise(xoff),0,1,-80,80); 31 | } 32 | else { 33 | y = 100 + x*1.1 + map(noise(xoff),0,1,-80,80); 34 | } 35 | 36 | // Store the vertex in screen coordinates 37 | surface.add(new Vec2(x,y)); 38 | 39 | // Move through perlin noise 40 | xoff += 0.1; 41 | 42 | } 43 | 44 | // Build an array of vertices in Box2D coordinates 45 | // from the ArrayList we made 46 | Vec2[] vertices = new Vec2[surface.size()]; 47 | for (int i = 0; i < vertices.length; i++) { 48 | Vec2 edge = box2d.coordPixelsToWorld(surface.get(i)); 49 | vertices[i] = edge; 50 | } 51 | 52 | // Create the chain! 53 | chain.createChain(vertices,vertices.length); 54 | 55 | // The edge chain is now attached to a body via a fixture 56 | BodyDef bd = new BodyDef(); 57 | bd.position.set(0.0f,0.0f); 58 | Body body = box2d.createBody(bd); 59 | // Shortcut, we could define a fixture if we 60 | // want to specify frictions, restitution, etc. 61 | body.createFixture(chain,1); 62 | 63 | } 64 | 65 | // A simple function to just draw the edge chain as a series of vertex points 66 | void display() { 67 | strokeWeight(2); 68 | stroke(0); 69 | noFill(); 70 | beginShape(); 71 | for (Vec2 v: surface) { 72 | vertex(v.x,v.y); 73 | } 74 | endShape(); 75 | } 76 | 77 | } 78 | 79 | 80 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BumpySurfaceSine/BumpySurfaceSine.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // An uneven surface 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.collision.shapes.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.*; 12 | import org.jbox2d.dynamics.*; 13 | 14 | // A reference to our box2d world 15 | Box2DProcessing box2d; 16 | 17 | // An ArrayList of particles that will fall on the surface 18 | ArrayList particles; 19 | 20 | // An object to store information about the uneven surface 21 | Surface surface; 22 | 23 | void setup() { 24 | size(500,300); 25 | smooth(); 26 | 27 | // Initialize box2d physics and create the world 28 | box2d = new Box2DProcessing(this); 29 | box2d.createWorld(); 30 | // We are setting a custom gravity 31 | box2d.setGravity(0, -10); 32 | 33 | // Create the empty list 34 | particles = new ArrayList(); 35 | // Create the surface 36 | surface = new Surface(); 37 | } 38 | 39 | void draw() { 40 | // If the mouse is pressed, we make new particles 41 | if (random(1) < 0.5) { 42 | float sz = random(2,6); 43 | particles.add(new Particle(width/2,10,sz)); 44 | } 45 | 46 | // We must always step through time! 47 | box2d.step(); 48 | 49 | background(255); 50 | 51 | // Draw the surface 52 | surface.display(); 53 | 54 | // Draw all particles 55 | for (Particle p: particles) { 56 | p.display(); 57 | } 58 | 59 | // Particles that leave the screen, we delete them 60 | // (note they have to be deleted from both the box2d world and our list 61 | for (int i = particles.size()-1; i >= 0; i--) { 62 | Particle p = particles.get(i); 63 | if (p.done()) { 64 | particles.remove(i); 65 | } 66 | } 67 | 68 | // Just drawing the framerate to see how many particles it can handle 69 | fill(0); 70 | text("framerate: " + (int)frameRate,12,16); 71 | } 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BumpySurfaceSine/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A circular particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | Particle(float x, float y, float r_) { 15 | r = r_; 16 | // This function puts the particle in the Box2d world 17 | makeBody(x,y,r); 18 | } 19 | 20 | // This function removes the particle from the box2d world 21 | void killBody() { 22 | box2d.destroyBody(body); 23 | } 24 | 25 | // Is the particle ready for deletion? 26 | boolean done() { 27 | // Let's find the screen position of the particle 28 | Vec2 pos = box2d.getBodyPixelCoord(body); 29 | // Is it off the bottom of the screen? 30 | if (pos.y > height+r*2) { 31 | killBody(); 32 | return true; 33 | } 34 | return false; 35 | } 36 | 37 | // 38 | void display() { 39 | // We look at each body and get its screen position 40 | Vec2 pos = box2d.getBodyPixelCoord(body); 41 | // Get its angle of rotation 42 | float a = body.getAngle(); 43 | pushMatrix(); 44 | translate(pos.x,pos.y); 45 | rotate(-a); 46 | fill(175); 47 | stroke(0); 48 | strokeWeight(1); 49 | ellipse(0,0,r*2,r*2); 50 | // Let's add a line so we can see the rotation 51 | line(0,0,r,0); 52 | popMatrix(); 53 | } 54 | 55 | // Here's our function that adds the particle to the Box2D world 56 | void makeBody(float x, float y, float r) { 57 | // Define a body 58 | BodyDef bd = new BodyDef(); 59 | // Set its position 60 | bd.position = box2d.coordPixelsToWorld(x,y); 61 | bd.type = BodyType.DYNAMIC; 62 | body = box2d.world.createBody(bd); 63 | 64 | // Make the body's shape a circle 65 | CircleShape cs = new CircleShape(); 66 | cs.m_radius = box2d.scalarPixelsToWorld(r); 67 | 68 | FixtureDef fd = new FixtureDef(); 69 | fd.shape = cs; 70 | // Parameters that affect physics 71 | fd.density = 1; 72 | fd.friction = 0.01; 73 | fd.restitution = 0.3; 74 | 75 | // Attach fixture to body 76 | body.createFixture(fd); 77 | 78 | // Give it a random initial velocity (and angular velocity) 79 | body.setLinearVelocity(new Vec2(random(-10f,10f),random(5f,10f))); 80 | body.setAngularVelocity(random(-10,10)); 81 | } 82 | 83 | 84 | 85 | 86 | 87 | 88 | } 89 | 90 | 91 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BumpySurfaceSine/Surface.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // An uneven surface boundary 7 | 8 | class Surface { 9 | // We'll keep track of all of the surface points 10 | ArrayList surface; 11 | 12 | 13 | Surface() { 14 | surface = new ArrayList(); 15 | 16 | // This is what box2d uses to put the surface in its world 17 | ChainShape chain = new ChainShape(); 18 | 19 | float theta = 0; 20 | 21 | // This has to go backwards so that the objects bounce off the top of the surface 22 | // This "edgechain" will only work in one direction! 23 | for (float x = width+10; x > -10; x -= 5) { 24 | 25 | // Doing some stuff with perlin noise to calculate a surface that points down on one side 26 | // and up on the other 27 | float y = map(cos(theta),-1,1,200,height-10); 28 | theta += 0.15; 29 | 30 | // Store the vertex in screen coordinates 31 | surface.add(new Vec2(x,y)); 32 | 33 | } 34 | 35 | // Build an array of vertices in Box2D coordinates 36 | // from the ArrayList we made 37 | Vec2[] vertices = new Vec2[surface.size()]; 38 | for (int i = 0; i < vertices.length; i++) { 39 | Vec2 edge = box2d.coordPixelsToWorld(surface.get(i)); 40 | vertices[i] = edge; 41 | } 42 | 43 | // Create the chain! 44 | chain.createChain(vertices,vertices.length); 45 | 46 | // The edge chain is now attached to a body via a fixture 47 | BodyDef bd = new BodyDef(); 48 | bd.position.set(0.0f,0.0f); 49 | Body body = box2d.createBody(bd); 50 | // Shortcut, we could define a fixture if we 51 | // want to specify frictions, restitution, etc. 52 | body.createFixture(chain,1); 53 | 54 | } 55 | 56 | // A simple function to just draw the edge chain as a series of vertex points 57 | void display() { 58 | strokeWeight(2); 59 | stroke(0); 60 | noFill(); 61 | beginShape(); 62 | for (Vec2 v: surface) { 63 | vertex(v.x,v.y); 64 | } 65 | endShape(); 66 | } 67 | 68 | } 69 | 70 | 71 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BumpySurfaceStraightLineDemo/BumpySurfaceStraightLineDemo.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // An uneven surface 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.collision.shapes.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.*; 12 | import org.jbox2d.dynamics.*; 13 | 14 | // A reference to our box2d world 15 | Box2DProcessing box2d; 16 | 17 | // An ArrayList of particles that will fall on the surface 18 | ArrayList particles; 19 | 20 | // An object to store information about the uneven surface 21 | Surface surface; 22 | 23 | void setup() { 24 | size(500,300); 25 | smooth(); 26 | 27 | // Initialize box2d physics and create the world 28 | box2d = new Box2DProcessing(this); 29 | box2d.createWorld(); 30 | // We are setting a custom gravity 31 | box2d.setGravity(0, -20); 32 | 33 | // Create the empty list 34 | particles = new ArrayList(); 35 | // Create the surface 36 | surface = new Surface(); 37 | } 38 | 39 | void draw() { 40 | // If the mouse is pressed, we make new particles 41 | if (random(1) < 0.5) { 42 | float sz = random(2,6); 43 | particles.add(new Particle(width/2,10,sz)); 44 | } 45 | 46 | // We must always step through time! 47 | box2d.step(); 48 | 49 | background(255); 50 | 51 | // Draw the surface 52 | surface.display(); 53 | 54 | // Draw all particles 55 | for (Particle p: particles) { 56 | p.display(); 57 | } 58 | 59 | // Particles that leave the screen, we delete them 60 | // (note they have to be deleted from both the box2d world and our list 61 | for (int i = particles.size()-1; i >= 0; i--) { 62 | Particle p = particles.get(i); 63 | if (p.done()) { 64 | particles.remove(i); 65 | } 66 | } 67 | 68 | // Just drawing the framerate to see how many particles it can handle 69 | fill(0); 70 | text("framerate: " + (int)frameRate,12,16); 71 | } 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BumpySurfaceStraightLineDemo/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A circular particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | Particle(float x, float y, float r_) { 15 | r = r_; 16 | // This function puts the particle in the Box2d world 17 | makeBody(x,y,r); 18 | } 19 | 20 | // This function removes the particle from the box2d world 21 | void killBody() { 22 | box2d.destroyBody(body); 23 | } 24 | 25 | // Is the particle ready for deletion? 26 | boolean done() { 27 | // Let's find the screen position of the particle 28 | Vec2 pos = box2d.getBodyPixelCoord(body); 29 | // Is it off the bottom of the screen? 30 | if (pos.y > height+r*2) { 31 | killBody(); 32 | return true; 33 | } 34 | return false; 35 | } 36 | 37 | // 38 | void display() { 39 | // We look at each body and get its screen position 40 | Vec2 pos = box2d.getBodyPixelCoord(body); 41 | // Get its angle of rotation 42 | float a = body.getAngle(); 43 | pushMatrix(); 44 | translate(pos.x,pos.y); 45 | rotate(-a); 46 | fill(175); 47 | stroke(0); 48 | strokeWeight(1); 49 | ellipse(0,0,r*2,r*2); 50 | // Let's add a line so we can see the rotation 51 | line(0,0,r,0); 52 | popMatrix(); 53 | } 54 | 55 | // Here's our function that adds the particle to the Box2D world 56 | void makeBody(float x, float y, float r) { 57 | // Define a body 58 | BodyDef bd = new BodyDef(); 59 | // Set its position 60 | bd.position = box2d.coordPixelsToWorld(x,y); 61 | bd.type = BodyType.DYNAMIC; 62 | body = box2d.world.createBody(bd); 63 | 64 | // Make the body's shape a circle 65 | CircleShape cs = new CircleShape(); 66 | cs.m_radius = box2d.scalarPixelsToWorld(r); 67 | 68 | FixtureDef fd = new FixtureDef(); 69 | fd.shape = cs; 70 | // Parameters that affect physics 71 | fd.density = 1; 72 | fd.friction = 0.01; 73 | fd.restitution = 0.3; 74 | 75 | // Attach fixture to body 76 | body.createFixture(fd); 77 | 78 | // Give it a random initial velocity (and angular velocity) 79 | body.setLinearVelocity(new Vec2(random(-10f,10f),random(5f,10f))); 80 | body.setAngularVelocity(random(-10,10)); 81 | } 82 | 83 | 84 | 85 | 86 | 87 | 88 | } 89 | 90 | 91 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/BumpySurfaceStraightLineDemo/Surface.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // An uneven surface boundary 7 | 8 | class Surface { 9 | // We'll keep track of all of the surface points 10 | ArrayList surface; 11 | 12 | 13 | Surface() { 14 | surface = new ArrayList(); 15 | // Here we keep track of the screen coordinates of the chain 16 | surface.add(new Vec2(width,height/2)); 17 | surface.add(new Vec2(width/2,height/2+50)); 18 | surface.add(new Vec2(0,height/2+50)); 19 | 20 | // This is what box2d uses to put the surface in its world 21 | ChainShape chain = new ChainShape(); 22 | 23 | // We can add 3 vertices by making an array of 3 Vec2 objects 24 | Vec2[] vertices = new Vec2[surface.size()]; 25 | for (int i = 0; i < vertices.length; i++) { 26 | vertices[i] = box2d.coordPixelsToWorld(surface.get(i)); 27 | } 28 | 29 | chain.createChain(vertices,vertices.length); 30 | 31 | // The edge chain is now a body! 32 | BodyDef bd = new BodyDef(); 33 | Body body = box2d.world.createBody(bd); 34 | // Shortcut, we could define a fixture if we 35 | // want to specify frictions, restitution, etc. 36 | body.createFixture(chain,1); 37 | } 38 | 39 | // A simple function to just draw the edge chain as a series of vertex points 40 | void display() { 41 | strokeWeight(1); 42 | stroke(0); 43 | fill(200); 44 | beginShape(); 45 | for (Vec2 v: surface) { 46 | vertex(v.x,v.y); 47 | } 48 | vertex(0,height); 49 | vertex(width,height); 50 | endShape(); 51 | } 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionListening/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | 16 | // But we also have to make a body for box2d to know about it 17 | Body b; 18 | 19 | Boundary(float x_,float y_, float w_, float h_) { 20 | x = x_; 21 | y = y_; 22 | w = w_; 23 | h = h_; 24 | 25 | // Define the polygon 26 | PolygonShape sd = new PolygonShape(); 27 | // Figure out the box2d coordinates 28 | float box2dW = box2d.scalarPixelsToWorld(w/2); 29 | float box2dH = box2d.scalarPixelsToWorld(h/2); 30 | // We're just a box 31 | sd.setAsBox(box2dW, box2dH); 32 | 33 | 34 | // Create the body 35 | BodyDef bd = new BodyDef(); 36 | bd.type = BodyType.STATIC; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | 43 | b.setUserData(this); 44 | } 45 | 46 | // Draw the boundary, if it were at an angle we'd have to do something fancier 47 | void display() { 48 | fill(0); 49 | stroke(0); 50 | rectMode(CENTER); 51 | rect(x,y,w,h); 52 | } 53 | 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionListening/CollisionListening.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Basic example of controlling an object with our own motion (by attaching a MouseJoint) 7 | // Also demonstrates how to know which object was hit 8 | 9 | import shiffman.box2d.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.joints.*; 12 | import org.jbox2d.collision.shapes.*; 13 | import org.jbox2d.collision.shapes.Shape; 14 | import org.jbox2d.common.*; 15 | import org.jbox2d.dynamics.*; 16 | import org.jbox2d.dynamics.contacts.*; 17 | 18 | // A reference to our box2d world 19 | Box2DProcessing box2d; 20 | 21 | // An ArrayList of particles that will fall on the surface 22 | ArrayList particles; 23 | 24 | Boundary wall; 25 | 26 | void setup() { 27 | size(400, 300); 28 | smooth(); 29 | 30 | // Initialize box2d physics and create the world 31 | box2d = new Box2DProcessing(this); 32 | box2d.createWorld(); 33 | 34 | // Turn on collision listening! 35 | box2d.listenForCollisions(); 36 | 37 | // Create the empty list 38 | particles = new ArrayList(); 39 | 40 | wall = new Boundary(width/2, height-5, width, 10); 41 | } 42 | 43 | void draw() { 44 | background(255); 45 | 46 | if (random(1) < 0.1) { 47 | float sz = random(4, 8); 48 | particles.add(new Particle(random(width), 20, sz)); 49 | } 50 | 51 | 52 | // We must always step through time! 53 | box2d.step(); 54 | 55 | // Look at all particles 56 | for (int i = particles.size()-1; i >= 0; i--) { 57 | Particle p = particles.get(i); 58 | p.display(); 59 | // Particles that leave the screen, we delete them 60 | // (note they have to be deleted from both the box2d world and our list 61 | if (p.done()) { 62 | particles.remove(i); 63 | } 64 | } 65 | 66 | wall.display(); 67 | } 68 | 69 | 70 | // Collision event functions! 71 | void beginContact(Contact cp) { 72 | // Get both fixtures 73 | Fixture f1 = cp.getFixtureA(); 74 | Fixture f2 = cp.getFixtureB(); 75 | // Get both bodies 76 | Body b1 = f1.getBody(); 77 | Body b2 = f2.getBody(); 78 | 79 | // Get our objects that reference these bodies 80 | Object o1 = b1.getUserData(); 81 | Object o2 = b2.getUserData(); 82 | 83 | if (o1.getClass() == Particle.class && o2.getClass() == Particle.class) { 84 | Particle p1 = (Particle) o1; 85 | p1.change(); 86 | Particle p2 = (Particle) o2; 87 | p2.change(); 88 | } 89 | 90 | } 91 | 92 | // Objects stop touching each other 93 | void endContact(Contact cp) { 94 | } 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionListening/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A circular particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | color col; 15 | 16 | 17 | Particle(float x, float y, float r_) { 18 | r = r_; 19 | // This function puts the particle in the Box2d world 20 | makeBody(x, y, r); 21 | body.setUserData(this); 22 | col = color(175); 23 | } 24 | 25 | // This function removes the particle from the box2d world 26 | void killBody() { 27 | box2d.destroyBody(body); 28 | } 29 | 30 | // Change color when hit 31 | void change() { 32 | col = color(255, 0, 0); 33 | } 34 | 35 | // Is the particle ready for deletion? 36 | boolean done() { 37 | // Let's find the screen position of the particle 38 | Vec2 pos = box2d.getBodyPixelCoord(body); 39 | // Is it off the bottom of the screen? 40 | if (pos.y > height+r*2) { 41 | killBody(); 42 | return true; 43 | } 44 | return false; 45 | } 46 | 47 | 48 | // 49 | void display() { 50 | // We look at each body and get its screen position 51 | Vec2 pos = box2d.getBodyPixelCoord(body); 52 | // Get its angle of rotation 53 | float a = body.getAngle(); 54 | pushMatrix(); 55 | translate(pos.x, pos.y); 56 | rotate(a); 57 | fill(col); 58 | stroke(0); 59 | strokeWeight(1); 60 | ellipse(0, 0, r*2, r*2); 61 | // Let's add a line so we can see the rotation 62 | line(0, 0, r, 0); 63 | popMatrix(); 64 | } 65 | 66 | // Here's our function that adds the particle to the Box2D world 67 | void makeBody(float x, float y, float r) { 68 | // Define a body 69 | BodyDef bd = new BodyDef(); 70 | // Set its position 71 | bd.position = box2d.coordPixelsToWorld(x, y); 72 | bd.type = BodyType.DYNAMIC; 73 | body = box2d.createBody(bd); 74 | 75 | // Make the body's shape a circle 76 | CircleShape cs = new CircleShape(); 77 | cs.m_radius = box2d.scalarPixelsToWorld(r); 78 | 79 | FixtureDef fd = new FixtureDef(); 80 | fd.shape = cs; 81 | // Parameters that affect physics 82 | fd.density = 1; 83 | fd.friction = 0.01; 84 | fd.restitution = 0.3; 85 | 86 | // Attach fixture to body 87 | body.createFixture(fd); 88 | 89 | body.setAngularVelocity(random(-10, 10)); 90 | } 91 | } 92 | 93 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionListeningDeletionExercise/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | 16 | // But we also have to make a body for box2d to know about it 17 | Body b; 18 | 19 | Boundary(float x_,float y_, float w_, float h_) { 20 | x = x_; 21 | y = y_; 22 | w = w_; 23 | h = h_; 24 | 25 | // Define the polygon 26 | PolygonShape sd = new PolygonShape(); 27 | // Figure out the box2d coordinates 28 | float box2dW = box2d.scalarPixelsToWorld(w/2); 29 | float box2dH = box2d.scalarPixelsToWorld(h/2); 30 | // We're just a box 31 | sd.setAsBox(box2dW, box2dH); 32 | 33 | 34 | // Create the body 35 | BodyDef bd = new BodyDef(); 36 | bd.type = BodyType.STATIC; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | 43 | b.setUserData(this); 44 | } 45 | 46 | // Draw the boundary, if it were at an angle we'd have to do something fancier 47 | void display() { 48 | fill(0); 49 | stroke(0); 50 | rectMode(CENTER); 51 | rect(x,y,w,h); 52 | } 53 | 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionListeningDeletionExercise/CollisionListeningDeletionExercise.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Basic example of controlling an object with our own motion (by attaching a MouseJoint) 7 | // Also demonstrates how to know which object was hit 8 | 9 | import shiffman.box2d.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.joints.*; 12 | import org.jbox2d.collision.shapes.*; 13 | import org.jbox2d.collision.shapes.Shape; 14 | import org.jbox2d.common.*; 15 | import org.jbox2d.dynamics.*; 16 | import org.jbox2d.dynamics.contacts.*; 17 | 18 | // A reference to our box2d world 19 | Box2DProcessing box2d; 20 | 21 | // An ArrayList of particles that will fall on the surface 22 | ArrayList particles; 23 | 24 | Boundary wall; 25 | 26 | void setup() { 27 | size(400, 300); 28 | smooth(); 29 | 30 | // Initialize box2d physics and create the world 31 | box2d = new Box2DProcessing(this); 32 | box2d.createWorld(); 33 | 34 | // Turn on collision listening! 35 | box2d.listenForCollisions(); 36 | 37 | // Create the empty list 38 | particles = new ArrayList(); 39 | 40 | wall = new Boundary(width/2, height-5, width, 10); 41 | } 42 | 43 | void draw() { 44 | background(255); 45 | 46 | if (random(1) < 0.1) { 47 | float sz = random(4, 8); 48 | particles.add(new Particle(random(width), 20, sz)); 49 | } 50 | 51 | 52 | // We must always step through time! 53 | box2d.step(); 54 | 55 | // Look at all particles 56 | for (int i = particles.size()-1; i >= 0; i--) { 57 | Particle p = particles.get(i); 58 | p.display(); 59 | // Particles that leave the screen, we delete them 60 | // (note they have to be deleted from both the box2d world and our list 61 | if (p.done()) { 62 | particles.remove(i); 63 | } 64 | } 65 | 66 | wall.display(); 67 | } 68 | 69 | 70 | // Collision event functions! 71 | void beginContact(Contact cp) { 72 | // Get both shapes 73 | Fixture f1 = cp.getFixtureA(); 74 | Fixture f2 = cp.getFixtureB(); 75 | // Get both bodies 76 | Body b1 = f1.getBody(); 77 | Body b2 = f2.getBody(); 78 | 79 | // Get our objects that reference these bodies 80 | Object o1 = b1.getUserData(); 81 | Object o2 = b2.getUserData(); 82 | 83 | if (o1.getClass() == Particle.class && o2.getClass() == Particle.class) { 84 | Particle p1 = (Particle) o1; 85 | p1.delete(); 86 | Particle p2 = (Particle) o2; 87 | p2.delete(); 88 | } 89 | 90 | if (o1.getClass() == Boundary.class) { 91 | Particle p = (Particle) o2; 92 | p.change(); 93 | } 94 | if (o2.getClass() == Boundary.class) { 95 | Particle p = (Particle) o1; 96 | p.change(); 97 | } 98 | 99 | 100 | } 101 | 102 | // Objects stop touching each other 103 | void endContact(Contact cp) { 104 | } 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionListeningDeletionExercise/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A circular particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | color col; 15 | 16 | boolean delete = false; 17 | 18 | Particle(float x, float y, float r_) { 19 | r = r_; 20 | // This function puts the particle in the Box2d world 21 | makeBody(x, y, r); 22 | body.setUserData(this); 23 | col = color(175); 24 | } 25 | 26 | // This function removes the particle from the box2d world 27 | void killBody() { 28 | box2d.destroyBody(body); 29 | } 30 | 31 | void delete() { 32 | delete = true; 33 | } 34 | 35 | // Change color when hit 36 | void change() { 37 | col = color(255, 0, 0); 38 | } 39 | 40 | // Is the particle ready for deletion? 41 | // Is the particle ready for deletion? 42 | boolean done() { 43 | // Let's find the screen position of the particle 44 | Vec2 pos = box2d.getBodyPixelCoord(body); 45 | // Is it off the bottom of the screen? 46 | if (pos.y > height+r*2 || delete) { 47 | killBody(); 48 | return true; 49 | } 50 | return false; 51 | } 52 | // 53 | void display() { 54 | // We look at each body and get its screen position 55 | Vec2 pos = box2d.getBodyPixelCoord(body); 56 | // Get its angle of rotation 57 | float a = body.getAngle(); 58 | pushMatrix(); 59 | translate(pos.x, pos.y); 60 | rotate(a); 61 | fill(col); 62 | stroke(0); 63 | strokeWeight(1); 64 | ellipse(0, 0, r*2, r*2); 65 | // Let's add a line so we can see the rotation 66 | line(0, 0, r, 0); 67 | popMatrix(); 68 | } 69 | 70 | // Here's our function that adds the particle to the Box2D world 71 | void makeBody(float x, float y, float r) { 72 | // Define a body 73 | BodyDef bd = new BodyDef(); 74 | // Set its position 75 | bd.position = box2d.coordPixelsToWorld(x, y); 76 | bd.type = BodyType.DYNAMIC; 77 | body = box2d.createBody(bd); 78 | 79 | // Make the body's shape a circle 80 | CircleShape cs = new CircleShape(); 81 | cs.m_radius = box2d.scalarPixelsToWorld(r); 82 | 83 | FixtureDef fd = new FixtureDef(); 84 | fd.shape = cs; 85 | // Parameters that affect physics 86 | fd.density = 1; 87 | fd.friction = 0.01; 88 | fd.restitution = 0.3; 89 | 90 | // Attach fixture to body 91 | body.createFixture(fd); 92 | 93 | body.setAngularVelocity(random(-10, 10)); 94 | } 95 | } 96 | 97 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionsAndControl/Box.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A rectangular box 7 | 8 | class Box { 9 | 10 | // We need to keep track of a Body and a width and height 11 | Body body; 12 | float w; 13 | float h; 14 | 15 | // Constructor 16 | Box(float x_, float y_) { 17 | float x = x_; 18 | float y = y_; 19 | w = 24; 20 | h = 24; 21 | // Add the box to the box2d world 22 | makeBody(new Vec2(x, y), w, h); 23 | body.setUserData(this); 24 | } 25 | 26 | // This function removes the particle from the box2d world 27 | void killBody() { 28 | box2d.destroyBody(body); 29 | } 30 | 31 | boolean contains(float x, float y) { 32 | Vec2 worldPoint = box2d.coordPixelsToWorld(x, y); 33 | Fixture f = body.getFixtureList(); 34 | boolean inside = f.testPoint(worldPoint); 35 | return inside; 36 | } 37 | 38 | // Drawing the box 39 | void display() { 40 | // We look at each body and get its screen position 41 | Vec2 pos = box2d.getBodyPixelCoord(body); 42 | // Get its angle of rotation 43 | float a = body.getAngle(); 44 | 45 | rectMode(PConstants.CENTER); 46 | pushMatrix(); 47 | translate(pos.x, pos.y); 48 | rotate(-a); 49 | fill(175); 50 | stroke(0); 51 | rect(0, 0, w, h); 52 | popMatrix(); 53 | } 54 | 55 | // This function adds the rectangle to the box2d world 56 | void makeBody(Vec2 center, float w_, float h_) { 57 | // Define and create the body 58 | BodyDef bd = new BodyDef(); 59 | bd.type = BodyType.DYNAMIC; 60 | bd.position.set(box2d.coordPixelsToWorld(center)); 61 | body = box2d.createBody(bd); 62 | 63 | // Define a polygon (this is what we use for a rectangle) 64 | PolygonShape sd = new PolygonShape(); 65 | float box2dW = box2d.scalarPixelsToWorld(w_/2); 66 | float box2dH = box2d.scalarPixelsToWorld(h_/2); 67 | sd.setAsBox(box2dW, box2dH); 68 | 69 | // Define a fixture 70 | FixtureDef fd = new FixtureDef(); 71 | fd.shape = sd; 72 | // Parameters that affect physics 73 | fd.density = 1; 74 | fd.friction = 0.3; 75 | fd.restitution = 0.5; 76 | 77 | body.createFixture(fd); 78 | //body.setMassFromShapes(); 79 | 80 | // Give it some initial random velocity 81 | body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); 82 | body.setAngularVelocity(random(-5, 5)); 83 | } 84 | } 85 | 86 | 87 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionsAndControl/CollisionsAndControl.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Basic example of controlling an object with our own motion (by attaching a MouseJoint) 7 | // Also demonstrates how to know which object was hit 8 | 9 | import shiffman.box2d.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.joints.*; 12 | import org.jbox2d.collision.shapes.*; 13 | import org.jbox2d.collision.shapes.Shape; 14 | import org.jbox2d.common.*; 15 | import org.jbox2d.dynamics.*; 16 | import org.jbox2d.dynamics.contacts.*; 17 | 18 | // A reference to our box2d world 19 | Box2DProcessing box2d; 20 | 21 | // Just a single box this time 22 | Box box; 23 | 24 | // An ArrayList of particles that will fall on the surface 25 | ArrayList particles; 26 | 27 | // The Spring that will attach to the box from the mouse 28 | Spring spring; 29 | 30 | // Perlin noise values 31 | float xoff = 0; 32 | float yoff = 1000; 33 | 34 | 35 | void setup() { 36 | size(400,300); 37 | smooth(); 38 | 39 | // Initialize box2d physics and create the world 40 | box2d = new Box2DProcessing(this); 41 | box2d.createWorld(); 42 | 43 | // Turn on collision listening! 44 | box2d.listenForCollisions(); 45 | 46 | // Make the box 47 | box = new Box(width/2,height/2); 48 | 49 | // Make the spring (it doesn't really get initialized until the mouse is clicked) 50 | spring = new Spring(); 51 | spring.bind(width/2,height/2,box); 52 | 53 | // Create the empty list 54 | particles = new ArrayList(); 55 | 56 | 57 | } 58 | 59 | void draw() { 60 | background(255); 61 | 62 | if (random(1) < 0.2) { 63 | float sz = random(4,8); 64 | particles.add(new Particle(width/2,-20,sz)); 65 | } 66 | 67 | 68 | // We must always step through time! 69 | box2d.step(); 70 | 71 | // Make an x,y coordinate out of perlin noise 72 | float x = noise(xoff)*width; 73 | float y = noise(yoff)*height; 74 | xoff += 0.01; 75 | yoff += 0.01; 76 | 77 | // This is tempting but will not work! 78 | // box.body.setXForm(box2d.screenToWorld(x,y),0); 79 | 80 | // Instead update the spring which pulls the mouse along 81 | if (mousePressed) { 82 | spring.update(mouseX,mouseY); 83 | spring.display(); 84 | } else { 85 | spring.update(x,y); 86 | } 87 | box.body.setAngularVelocity(0); 88 | 89 | // Look at all particles 90 | for (int i = particles.size()-1; i >= 0; i--) { 91 | Particle p = particles.get(i); 92 | p.display(); 93 | // Particles that leave the screen, we delete them 94 | // (note they have to be deleted from both the box2d world and our list 95 | if (p.done()) { 96 | particles.remove(i); 97 | } 98 | } 99 | 100 | // Draw the box 101 | box.display(); 102 | 103 | // Draw the spring 104 | // spring.display(); 105 | } 106 | 107 | 108 | // Collision event functions! 109 | void beginContact(Contact cp) { 110 | // Get both fixtures 111 | Fixture f1 = cp.getFixtureA(); 112 | Fixture f2 = cp.getFixtureB(); 113 | // Get both bodies 114 | Body b1 = f1.getBody(); 115 | Body b2 = f2.getBody(); 116 | // Get our objects that reference these bodies 117 | Object o1 = b1.getUserData(); 118 | Object o2 = b2.getUserData(); 119 | 120 | // If object 1 is a Box, then object 2 must be a particle 121 | // Note we are ignoring particle on particle collisions 122 | if (o1.getClass() == Box.class) { 123 | Particle p = (Particle) o2; 124 | p.change(); 125 | } 126 | // If object 2 is a Box, then object 1 must be a particle 127 | else if (o2.getClass() == Box.class) { 128 | Particle p = (Particle) o1; 129 | p.change(); 130 | } 131 | } 132 | 133 | 134 | // Objects stop touching each other 135 | void endContact(Contact cp) { 136 | } 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionsAndControl/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A circular particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | color col; 15 | 16 | 17 | Particle(float x, float y, float r_) { 18 | r = r_; 19 | // This function puts the particle in the Box2d world 20 | makeBody(x, y, r); 21 | body.setUserData(this); 22 | col = color(175); 23 | } 24 | 25 | // This function removes the particle from the box2d world 26 | void killBody() { 27 | box2d.destroyBody(body); 28 | } 29 | 30 | // Change color when hit 31 | void change() { 32 | col = color(255, 0, 0); 33 | } 34 | 35 | // Is the particle ready for deletion? 36 | boolean done() { 37 | // Let's find the screen position of the particle 38 | Vec2 pos = box2d.getBodyPixelCoord(body); 39 | // Is it off the bottom of the screen? 40 | if (pos.y > height+r*2) { 41 | killBody(); 42 | return true; 43 | } 44 | return false; 45 | } 46 | 47 | 48 | // 49 | void display() { 50 | // We look at each body and get its screen position 51 | Vec2 pos = box2d.getBodyPixelCoord(body); 52 | // Get its angle of rotation 53 | float a = body.getAngle(); 54 | pushMatrix(); 55 | translate(pos.x, pos.y); 56 | rotate(a); 57 | fill(col); 58 | stroke(0); 59 | strokeWeight(1); 60 | ellipse(0, 0, r*2, r*2); 61 | // Let's add a line so we can see the rotation 62 | line(0, 0, r, 0); 63 | popMatrix(); 64 | } 65 | 66 | // Here's our function that adds the particle to the Box2D world 67 | void makeBody(float x, float y, float r) { 68 | // Define a body 69 | BodyDef bd = new BodyDef(); 70 | // Set its position 71 | bd.position = box2d.coordPixelsToWorld(x, y); 72 | bd.type = BodyType.DYNAMIC; 73 | body = box2d.createBody(bd); 74 | 75 | // Make the body's shape a circle 76 | CircleShape cs = new CircleShape(); 77 | cs.m_radius = box2d.scalarPixelsToWorld(r); 78 | 79 | FixtureDef fd = new FixtureDef(); 80 | fd.shape = cs; 81 | // Parameters that affect physics 82 | fd.density = 1; 83 | fd.friction = 0.01; 84 | fd.restitution = 0.3; 85 | 86 | // Attach fixture to body 87 | body.createFixture(fd); 88 | 89 | body.setAngularVelocity(random(-10, 10)); 90 | } 91 | } 92 | 93 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionsAndControl/Spring.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // Class to describe the spring joint (displayed as a line) 7 | 8 | class Spring { 9 | 10 | // This is the box2d object we need to create 11 | MouseJoint mouseJoint; 12 | 13 | Spring() { 14 | // At first it doesn't exist 15 | mouseJoint = null; 16 | } 17 | 18 | // If it exists we set its target to the mouse location 19 | void update(float x, float y) { 20 | if (mouseJoint != null) { 21 | // Always convert to world coordinates! 22 | Vec2 mouseWorld = box2d.coordPixelsToWorld(x,y); 23 | mouseJoint.setTarget(mouseWorld); 24 | } 25 | } 26 | 27 | void display() { 28 | if (mouseJoint != null) { 29 | // We can get the two anchor points 30 | Vec2 v1 = new Vec2(0,0); 31 | mouseJoint.getAnchorA(v1); 32 | Vec2 v2 = new Vec2(0,0); 33 | mouseJoint.getAnchorB(v2); 34 | // Convert them to screen coordinates 35 | v1 = box2d.coordWorldToPixels(v1); 36 | v2 = box2d.coordWorldToPixels(v2); 37 | // And just draw a line 38 | stroke(0); 39 | strokeWeight(1); 40 | line(v1.x,v1.y,v2.x,v2.y); 41 | } 42 | } 43 | 44 | 45 | // This is the key function where 46 | // we attach the spring to an x,y location 47 | // and the Box object's location 48 | void bind(float x, float y, Box box) { 49 | // Define the joint 50 | MouseJointDef md = new MouseJointDef(); 51 | 52 | // Body A is just a fake ground body for simplicity (there isn't anything at the mouse) 53 | md.bodyA = box2d.getGroundBody(); 54 | // Body 2 is the box's boxy 55 | md.bodyB = box.body; 56 | // Get the mouse location in world coordinates 57 | Vec2 mp = box2d.coordPixelsToWorld(x,y); 58 | // And that's the target 59 | md.target.set(mp); 60 | // Some stuff about how strong and bouncy the spring should be 61 | md.maxForce = 1000.0 * box.body.m_mass; 62 | md.frequencyHz = 5.0; 63 | md.dampingRatio = 0.9; 64 | 65 | // Wake up body! 66 | //box.body.wakeUp(); 67 | 68 | // Make the joint! 69 | mouseJoint = (MouseJoint) box2d.world.createJoint(md); 70 | } 71 | 72 | void destroy() { 73 | // We can get rid of the joint when the mouse is released 74 | if (mouseJoint != null) { 75 | box2d.world.destroyJoint(mouseJoint); 76 | mouseJoint = null; 77 | } 78 | } 79 | 80 | } 81 | 82 | 83 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionsAndControlInterface/Box.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A rectangular box 7 | 8 | class Box { 9 | 10 | // We need to keep track of a Body and a width and height 11 | Body body; 12 | float w; 13 | float h; 14 | 15 | // Constructor 16 | Box(float x_, float y_) { 17 | float x = x_; 18 | float y = y_; 19 | w = 24; 20 | h = 24; 21 | // Add the box to the box2d world 22 | makeBody(new Vec2(x, y), w, h); 23 | body.setUserData(this); 24 | } 25 | 26 | // This function removes the particle from the box2d world 27 | void killBody() { 28 | box2d.destroyBody(body); 29 | } 30 | 31 | boolean contains(float x, float y) { 32 | Vec2 worldPoint = box2d.coordPixelsToWorld(x, y); 33 | Fixture f = body.getFixtureList(); 34 | boolean inside = f.testPoint(worldPoint); 35 | return inside; 36 | } 37 | 38 | // Drawing the box 39 | void display() { 40 | // We look at each body and get its screen position 41 | Vec2 pos = box2d.getBodyPixelCoord(body); 42 | // Get its angle of rotation 43 | float a = body.getAngle(); 44 | 45 | rectMode(PConstants.CENTER); 46 | pushMatrix(); 47 | translate(pos.x, pos.y); 48 | rotate(-a); 49 | fill(175); 50 | stroke(0); 51 | rect(0, 0, w, h); 52 | popMatrix(); 53 | } 54 | 55 | // This function adds the rectangle to the box2d world 56 | void makeBody(Vec2 center, float w_, float h_) { 57 | // Define and create the body 58 | BodyDef bd = new BodyDef(); 59 | bd.type = BodyType.DYNAMIC; 60 | bd.position.set(box2d.coordPixelsToWorld(center)); 61 | body = box2d.createBody(bd); 62 | 63 | // Define a polygon (this is what we use for a rectangle) 64 | PolygonShape sd = new PolygonShape(); 65 | float box2dW = box2d.scalarPixelsToWorld(w_/2); 66 | float box2dH = box2d.scalarPixelsToWorld(h_/2); 67 | sd.setAsBox(box2dW, box2dH); 68 | 69 | // Define a fixture 70 | FixtureDef fd = new FixtureDef(); 71 | fd.shape = sd; 72 | // Parameters that affect physics 73 | fd.density = 1; 74 | fd.friction = 0.3; 75 | fd.restitution = 0.5; 76 | 77 | body.createFixture(fd); 78 | //body.setMassFromShapes(); 79 | 80 | // Give it some initial random velocity 81 | body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); 82 | body.setAngularVelocity(random(-5, 5)); 83 | } 84 | } 85 | 86 | 87 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionsAndControlInterface/CollisionsAndControlInterface.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // Basic example of controlling an object with our own motion (by attaching a MouseJoint) 7 | // Also demonstrates how to know which object was hit 8 | 9 | import shiffman.box2d.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.joints.*; 12 | import org.jbox2d.collision.shapes.*; 13 | import org.jbox2d.collision.shapes.Shape; 14 | import org.jbox2d.common.*; 15 | import org.jbox2d.dynamics.*; 16 | import org.jbox2d.dynamics.contacts.*; 17 | 18 | // A reference to our box2d world 19 | Box2DProcessing box2d; 20 | 21 | // Just a single box this time 22 | Box box; 23 | 24 | // An ArrayList of particles that will fall on the surface 25 | ArrayList particles; 26 | 27 | // The Spring that will attach to the box from the mouse 28 | Spring spring; 29 | 30 | // Perlin noise values 31 | float xoff = 0; 32 | float yoff = 1000; 33 | 34 | 35 | void setup() { 36 | size(400,300); 37 | smooth(); 38 | 39 | // Initialize box2d physics and create the world 40 | box2d = new Box2DProcessing(this); 41 | box2d.createWorld(); 42 | 43 | // Add a listener to listen for collisions! 44 | box2d.world.setContactListener(new CustomListener()); 45 | 46 | // Make the box 47 | box = new Box(width/2,height/2); 48 | 49 | // Make the spring (it doesn't really get initialized until the mouse is clicked) 50 | spring = new Spring(); 51 | spring.bind(width/2,height/2,box); 52 | 53 | // Create the empty list 54 | particles = new ArrayList(); 55 | 56 | 57 | } 58 | 59 | void draw() { 60 | background(255); 61 | 62 | if (random(1) < 0.2) { 63 | float sz = random(4,8); 64 | particles.add(new Particle(width/2,-20,sz)); 65 | } 66 | 67 | 68 | // We must always step through time! 69 | box2d.step(); 70 | 71 | // Make an x,y coordinate out of perlin noise 72 | float x = noise(xoff)*width; 73 | float y = noise(yoff)*height; 74 | xoff += 0.01; 75 | yoff += 0.01; 76 | 77 | // This is tempting but will not work! 78 | // box.body.setXForm(box2d.screenToWorld(x,y),0); 79 | 80 | // Instead update the spring which pulls the mouse along 81 | if (mousePressed) { 82 | spring.update(mouseX,mouseY); 83 | } else { 84 | spring.update(x,y); 85 | } 86 | //box.body.setAngularVelocity(0); 87 | 88 | // Look at all particles 89 | for (int i = particles.size()-1; i >= 0; i--) { 90 | Particle p = particles.get(i); 91 | p.display(); 92 | // Particles that leave the screen, we delete them 93 | // (note they have to be deleted from both the box2d world and our list 94 | if (p.done()) { 95 | particles.remove(i); 96 | } 97 | } 98 | 99 | // Draw the box 100 | box.display(); 101 | 102 | // Draw the spring 103 | // spring.display(); 104 | } 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionsAndControlInterface/ContactListener.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // ContactListener to listen for collisions! 7 | 8 | import org.jbox2d.callbacks.ContactImpulse; 9 | import org.jbox2d.callbacks.ContactListener; 10 | import org.jbox2d.collision.Manifold; 11 | import org.jbox2d.dynamics.contacts.Contact; 12 | 13 | class CustomListener implements ContactListener { 14 | CustomListener() { 15 | } 16 | 17 | // This function is called when a new collision occurs 18 | void beginContact(Contact cp) { 19 | // Get both fixtures 20 | Fixture f1 = cp.getFixtureA(); 21 | Fixture f2 = cp.getFixtureB(); 22 | // Get both bodies 23 | Body b1 = f1.getBody(); 24 | Body b2 = f2.getBody(); 25 | // Get our objects that reference these bodies 26 | Object o1 = b1.getUserData(); 27 | Object o2 = b2.getUserData(); 28 | 29 | // If object 1 is a Box, then object 2 must be a particle 30 | // Note we are ignoring particle on particle collisions 31 | if (o1.getClass() == Box.class) { 32 | Particle p = (Particle) o2; 33 | p.change(); 34 | } 35 | // If object 2 is a Box, then object 1 must be a particle 36 | else if (o2.getClass() == Box.class) { 37 | Particle p = (Particle) o1; 38 | p.change(); 39 | } 40 | } 41 | 42 | void endContact(Contact contact) { 43 | // TODO Auto-generated method stub 44 | } 45 | 46 | void preSolve(Contact contact, Manifold oldManifold) { 47 | // TODO Auto-generated method stub 48 | } 49 | 50 | void postSolve(Contact contact, ContactImpulse impulse) { 51 | // TODO Auto-generated method stub 52 | } 53 | } 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionsAndControlInterface/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A circular particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | color col; 15 | 16 | 17 | Particle(float x, float y, float r_) { 18 | r = r_; 19 | // This function puts the particle in the Box2d world 20 | makeBody(x, y, r); 21 | body.setUserData(this); 22 | col = color(175); 23 | } 24 | 25 | // This function removes the particle from the box2d world 26 | void killBody() { 27 | box2d.destroyBody(body); 28 | } 29 | 30 | // Change color when hit 31 | void change() { 32 | col = color(255, 0, 0); 33 | } 34 | 35 | // Is the particle ready for deletion? 36 | boolean done() { 37 | // Let's find the screen position of the particle 38 | Vec2 pos = box2d.getBodyPixelCoord(body); 39 | // Is it off the bottom of the screen? 40 | if (pos.y > height+r*2) { 41 | killBody(); 42 | return true; 43 | } 44 | return false; 45 | } 46 | 47 | 48 | // 49 | void display() { 50 | // We look at each body and get its screen position 51 | Vec2 pos = box2d.getBodyPixelCoord(body); 52 | // Get its angle of rotation 53 | float a = body.getAngle(); 54 | pushMatrix(); 55 | translate(pos.x, pos.y); 56 | rotate(a); 57 | fill(col); 58 | stroke(0); 59 | strokeWeight(1); 60 | ellipse(0, 0, r*2, r*2); 61 | // Let's add a line so we can see the rotation 62 | line(0, 0, r, 0); 63 | popMatrix(); 64 | } 65 | 66 | // Here's our function that adds the particle to the Box2D world 67 | void makeBody(float x, float y, float r) { 68 | // Define a body 69 | BodyDef bd = new BodyDef(); 70 | // Set its position 71 | bd.position = box2d.coordPixelsToWorld(x, y); 72 | bd.type = BodyType.DYNAMIC; 73 | body = box2d.createBody(bd); 74 | 75 | // Make the body's shape a circle 76 | CircleShape cs = new CircleShape(); 77 | cs.m_radius = box2d.scalarPixelsToWorld(r); 78 | 79 | FixtureDef fd = new FixtureDef(); 80 | fd.shape = cs; 81 | // Parameters that affect physics 82 | fd.density = 1; 83 | fd.friction = 0.01; 84 | fd.restitution = 0.3; 85 | 86 | // Attach fixture to body 87 | body.createFixture(fd); 88 | 89 | body.setAngularVelocity(random(-10, 10)); 90 | } 91 | } 92 | 93 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/CollisionsAndControlInterface/Spring.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // Class to describe the spring joint (displayed as a line) 7 | 8 | class Spring { 9 | 10 | // This is the box2d object we need to create 11 | MouseJoint mouseJoint; 12 | 13 | Spring() { 14 | // At first it doesn't exist 15 | mouseJoint = null; 16 | } 17 | 18 | // If it exists we set its target to the mouse location 19 | void update(float x, float y) { 20 | if (mouseJoint != null) { 21 | // Always convert to world coordinates! 22 | Vec2 mouseWorld = box2d.coordPixelsToWorld(x,y); 23 | mouseJoint.setTarget(mouseWorld); 24 | } 25 | } 26 | 27 | void display() { 28 | if (mouseJoint != null) { 29 | // We can get the two anchor points 30 | Vec2 v1 = null; 31 | mouseJoint.getAnchorA(v1); 32 | Vec2 v2 = null; 33 | mouseJoint.getAnchorB(v2); 34 | // Convert them to screen coordinates 35 | v1 = box2d.coordWorldToPixels(v1); 36 | v2 = box2d.coordWorldToPixels(v2); 37 | // And just draw a line 38 | stroke(0); 39 | strokeWeight(1); 40 | line(v1.x,v1.y,v2.x,v2.y); 41 | } 42 | } 43 | 44 | 45 | // This is the key function where 46 | // we attach the spring to an x,y location 47 | // and the Box object's location 48 | void bind(float x, float y, Box box) { 49 | // Define the joint 50 | MouseJointDef md = new MouseJointDef(); 51 | 52 | // Body A is just a fake ground body for simplicity (there isn't anything at the mouse) 53 | md.bodyA = box2d.getGroundBody(); 54 | // Body 2 is the box's boxy 55 | md.bodyB = box.body; 56 | // Get the mouse location in world coordinates 57 | Vec2 mp = box2d.coordPixelsToWorld(x,y); 58 | // And that's the target 59 | md.target.set(mp); 60 | // Some stuff about how strong and bouncy the spring should be 61 | md.maxForce = 1000.0f * box.body.m_mass; 62 | md.frequencyHz = 5.0f; 63 | md.dampingRatio = 0.9f; 64 | 65 | // Wake up body! 66 | //box.body.wakeUp(); 67 | 68 | // Make the joint! 69 | mouseJoint = (MouseJoint) box2d.world.createJoint(md); 70 | } 71 | 72 | void destroy() { 73 | // We can get rid of the joint when the mouse is released 74 | if (mouseJoint != null) { 75 | box2d.world.destroyJoint(mouseJoint); 76 | mouseJoint = null; 77 | } 78 | } 79 | 80 | } 81 | 82 | 83 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/DistanceJointExample/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | 16 | // But we also have to make a body for box2d to know about it 17 | Body b; 18 | 19 | Boundary(float x_,float y_, float w_, float h_) { 20 | x = x_; 21 | y = y_; 22 | w = w_; 23 | h = h_; 24 | 25 | // Define the polygon 26 | PolygonShape sd = new PolygonShape(); 27 | // Figure out the box2d coordinates 28 | float box2dW = box2d.scalarPixelsToWorld(w/2); 29 | float box2dH = box2d.scalarPixelsToWorld(h/2); 30 | // We're just a box 31 | sd.setAsBox(box2dW, box2dH); 32 | 33 | 34 | // Create the body 35 | BodyDef bd = new BodyDef(); 36 | bd.type = BodyType.STATIC; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | } 43 | 44 | // Draw the boundary, if it were at an angle we'd have to do something fancier 45 | void display() { 46 | fill(0); 47 | stroke(0); 48 | rectMode(CENTER); 49 | rect(x,y,w,h); 50 | } 51 | 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/DistanceJointExample/DistanceJointExample.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 201 4 | // Box2DProcessing example 5 | 6 | // Example demonstrating distance joints 7 | // A bridge is formed by connected a series of particles with joints 8 | 9 | import shiffman.box2d.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.joints.*; 12 | import org.jbox2d.collision.shapes.*; 13 | import org.jbox2d.collision.shapes.Shape; 14 | import org.jbox2d.common.*; 15 | import org.jbox2d.dynamics.*; 16 | import org.jbox2d.dynamics.contacts.*; 17 | 18 | // A reference to our box2d world 19 | Box2DProcessing box2d; 20 | 21 | // A list we'll use to track fixed objects 22 | ArrayList boundaries; 23 | 24 | 25 | // A list for all of our rectangles 26 | ArrayList pairs; 27 | 28 | void setup() { 29 | size(400,300); 30 | smooth(); 31 | 32 | // Initialize box2d physics and create the world 33 | box2d = new Box2DProcessing(this); 34 | box2d.createWorld(); 35 | 36 | // Create ArrayLists 37 | pairs = new ArrayList(); 38 | 39 | boundaries = new ArrayList(); 40 | 41 | // Add a bunch of fixed boundaries 42 | boundaries.add(new Boundary(width/4,height-5,width/2-50,10)); 43 | boundaries.add(new Boundary(3*width/4,height-50,width/2-50,10)); 44 | 45 | } 46 | 47 | void draw() { 48 | background(255); 49 | 50 | // We must always step through time! 51 | box2d.step(); 52 | 53 | // When the mouse is clicked, add a new Box object 54 | 55 | // Display all the boxes 56 | for (Pair p: pairs) { 57 | p.display(); 58 | } 59 | 60 | // Display all the boundaries 61 | for (Boundary wall: boundaries) { 62 | wall.display(); 63 | } 64 | 65 | fill(0); 66 | text("Click mouse to add connected particles.",10,20); 67 | } 68 | 69 | void mousePressed() { 70 | Pair p = new Pair(mouseX,mouseY); 71 | pairs.add(p); 72 | } 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/DistanceJointExample/Pair.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Series of Particles connected with distance joints 7 | 8 | class Pair { 9 | 10 | Particle p1; 11 | Particle p2; 12 | 13 | float len; 14 | // Chain constructor 15 | Pair(float x, float y) { 16 | len = 32; 17 | 18 | p1 = new Particle(x,y); 19 | p2 = new Particle(x+random(-1,1),y+random(-1,1)); 20 | 21 | 22 | DistanceJointDef djd = new DistanceJointDef(); 23 | // Connection between previous particle and this one 24 | djd.bodyA = p1.body; 25 | djd.bodyB = p2.body; 26 | // Equilibrium length 27 | djd.length = box2d.scalarPixelsToWorld(len); 28 | 29 | // These properties affect how springy the joint is 30 | djd.frequencyHz = 3; // Try a value less than 5 (0 for no elasticity) 31 | djd.dampingRatio = 0.1; // Ranges between 0 and 1 32 | 33 | // Make the joint. Note we aren't storing a reference to the joint ourselves anywhere! 34 | // We might need to someday, but for now it's ok 35 | DistanceJoint dj = (DistanceJoint) box2d.world.createJoint(djd); 36 | } 37 | 38 | 39 | 40 | // Draw the bridge 41 | void display() { 42 | Vec2 pos1 = box2d.getBodyPixelCoord(p1.body); 43 | Vec2 pos2 = box2d.getBodyPixelCoord(p2.body); 44 | stroke(0); 45 | line(pos1.x,pos1.y,pos2.x,pos2.y); 46 | 47 | p1.display(); 48 | p2.display(); 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/DistanceJointExample/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A circular particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | color col; 15 | 16 | Particle(float x, float y) { 17 | r = 8; 18 | 19 | // Define a body 20 | BodyDef bd = new BodyDef(); 21 | // Set its position 22 | bd.position = box2d.coordPixelsToWorld(x,y); 23 | bd.type = BodyType.DYNAMIC; 24 | body = box2d.world.createBody(bd); 25 | 26 | // Make the body's shape a circle 27 | CircleShape cs = new CircleShape(); 28 | cs.m_radius = box2d.scalarPixelsToWorld(r); 29 | 30 | FixtureDef fd = new FixtureDef(); 31 | fd.shape = cs; 32 | // Parameters that affect physics 33 | fd.density = 1; 34 | fd.friction = 0.01; 35 | fd.restitution = 0.3; 36 | 37 | // Attach fixture to body 38 | body.createFixture(fd); 39 | body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); 40 | 41 | col = color(175); 42 | } 43 | 44 | // This function removes the particle from the box2d world 45 | void killBody() { 46 | box2d.destroyBody(body); 47 | } 48 | 49 | // Is the particle ready for deletion? 50 | boolean done() { 51 | // Let's find the screen position of the particle 52 | Vec2 pos = box2d.getBodyPixelCoord(body); 53 | // Is it off the bottom of the screen? 54 | if (pos.y > height+r*2) { 55 | killBody(); 56 | return true; 57 | } 58 | return false; 59 | } 60 | 61 | void display() { 62 | // We look at each body and get its screen position 63 | Vec2 pos = box2d.getBodyPixelCoord(body); 64 | // Get its angle of rotation 65 | float a = body.getAngle(); 66 | pushMatrix(); 67 | translate(pos.x,pos.y); 68 | rotate(a); 69 | fill(col); 70 | stroke(0); 71 | strokeWeight(1); 72 | ellipse(0,0,r*2,r*2); 73 | // Let's add a line so we can see the rotation 74 | line(0,0,r,0); 75 | popMatrix(); 76 | } 77 | 78 | 79 | } 80 | 81 | 82 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Liquidy/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class (now incorporates angle) 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | // But we also have to make a body for box2d to know about it 16 | Body b; 17 | 18 | Boundary(float x_,float y_, float w_, float h_, float a) { 19 | x = x_; 20 | y = y_; 21 | w = w_; 22 | h = h_; 23 | 24 | // Define the polygon 25 | PolygonShape sd = new PolygonShape(); 26 | // Figure out the box2d coordinates 27 | float box2dW = box2d.scalarPixelsToWorld(w/2); 28 | float box2dH = box2d.scalarPixelsToWorld(h/2); 29 | // We're just a box 30 | sd.setAsBox(box2dW, box2dH); 31 | 32 | 33 | // Create the body 34 | BodyDef bd = new BodyDef(); 35 | bd.type = BodyType.STATIC; 36 | bd.angle = a; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | } 43 | 44 | // Draw the boundary, if it were at an angle we'd have to do something fancier 45 | void display() { 46 | noFill(); 47 | stroke(0); 48 | strokeWeight(1); 49 | rectMode(CENTER); 50 | 51 | float a = b.getAngle(); 52 | 53 | pushMatrix(); 54 | translate(x,y); 55 | rotate(-a); 56 | rect(0,0,w,h); 57 | popMatrix(); 58 | } 59 | 60 | } 61 | 62 | 63 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Liquidy/Liquidy.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Box2D particle system example 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.collision.shapes.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.*; 12 | 13 | 14 | 15 | // A reference to our box2d world 16 | Box2DProcessing box2d; 17 | 18 | // A list we'll use to track fixed objects 19 | ArrayList boundaries; 20 | 21 | // A list for all particle systems 22 | ArrayList systems; 23 | 24 | void setup() { 25 | size(400,300); 26 | smooth(); 27 | 28 | // Initialize box2d physics and create the world 29 | box2d = new Box2DProcessing(this); 30 | box2d.createWorld(); 31 | 32 | // We are setting a custom gravity 33 | box2d.setGravity(0, -20); 34 | 35 | // Create ArrayLists 36 | systems = new ArrayList(); 37 | boundaries = new ArrayList(); 38 | 39 | // Add a bunch of fixed boundaries 40 | boundaries.add(new Boundary(50,100,300,5,-0.3)); 41 | boundaries.add(new Boundary(250,175,300,5,0.5)); 42 | 43 | } 44 | 45 | void draw() { 46 | background(255); 47 | 48 | // We must always step through time! 49 | box2d.step(); 50 | 51 | // Run all the particle systems 52 | for (ParticleSystem system: systems) { 53 | system.run(); 54 | 55 | int n = (int) random(0,2); 56 | system.addParticles(n); 57 | } 58 | 59 | // Display all the boundaries 60 | for (Boundary wall: boundaries) { 61 | wall.display(); 62 | } 63 | } 64 | 65 | 66 | void mousePressed() { 67 | // Add a new Particle System whenever the mouse is clicked 68 | systems.add(new ParticleSystem(0, new PVector(mouseX,mouseY))); 69 | } 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Liquidy/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A Particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body 11 | Body body; 12 | 13 | PVector[] trail; 14 | 15 | // Constructor 16 | Particle(float x_, float y_) { 17 | float x = x_; 18 | float y = y_; 19 | trail = new PVector[6]; 20 | for (int i = 0; i < trail.length; i++) { 21 | trail[i] = new PVector(x,y); 22 | } 23 | 24 | // Add the box to the box2d world 25 | // Here's a little trick, let's make a tiny tiny radius 26 | // This way we have collisions, but they don't overwhelm the system 27 | makeBody(new Vec2(x,y),0.2f); 28 | } 29 | 30 | // This function removes the particle from the box2d world 31 | void killBody() { 32 | box2d.destroyBody(body); 33 | } 34 | 35 | // Is the particle ready for deletion? 36 | boolean done() { 37 | // Let's find the screen position of the particle 38 | Vec2 pos = box2d.getBodyPixelCoord(body); 39 | // Is it off the bottom of the screen? 40 | if (pos.y > height+20) { 41 | killBody(); 42 | return true; 43 | } 44 | return false; 45 | } 46 | 47 | // Drawing the box 48 | void display() { 49 | // We look at each body and get its screen position 50 | Vec2 pos = box2d.getBodyPixelCoord(body); 51 | 52 | // Keep track of a history of screen positions in an array 53 | for (int i = 0; i < trail.length-1; i++) { 54 | trail[i] = trail[i+1]; 55 | } 56 | trail[trail.length-1] = new PVector(pos.x,pos.y); 57 | 58 | // Draw particle as a trail 59 | beginShape(); 60 | noFill(); 61 | strokeWeight(2); 62 | stroke(0,150); 63 | for (int i = 0; i < trail.length; i++) { 64 | vertex(trail[i].x,trail[i].y); 65 | } 66 | endShape(); 67 | } 68 | 69 | // This function adds the rectangle to the box2d world 70 | void makeBody(Vec2 center, float r) { 71 | // Define and create the body 72 | BodyDef bd = new BodyDef(); 73 | bd.type = BodyType.DYNAMIC; 74 | 75 | bd.position.set(box2d.coordPixelsToWorld(center)); 76 | body = box2d.createBody(bd); 77 | 78 | // Give it some initial random velocity 79 | body.setLinearVelocity(new Vec2(random(-1,1),random(-1,1))); 80 | 81 | // Make the body's shape a circle 82 | CircleShape cs = new CircleShape(); 83 | cs.m_radius = box2d.scalarPixelsToWorld(r); 84 | 85 | FixtureDef fd = new FixtureDef(); 86 | fd.shape = cs; 87 | 88 | fd.density = 1; 89 | fd.friction = 0; // Slippery when wet! 90 | fd.restitution = 0.5; 91 | 92 | // We could use this if we want to turn collisions off 93 | //cd.filter.groupIndex = -10; 94 | 95 | // Attach fixture to body 96 | body.createFixture(fd); 97 | 98 | } 99 | 100 | } 101 | 102 | 103 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Liquidy/ParticleSystem.pde: -------------------------------------------------------------------------------- 1 | // Box2D Particle System 2 | // 3 | // Spring 2010 4 | 5 | // A class to describe a group of Particles 6 | // An ArrayList is used to manage the list of Particles 7 | 8 | class ParticleSystem { 9 | 10 | ArrayList particles; // An ArrayList for all the particles 11 | PVector origin; // An origin point for where particles are birthed 12 | 13 | ParticleSystem(int num, PVector v) { 14 | particles = new ArrayList(); // Initialize the ArrayList 15 | origin = v.get(); // Store the origin point 16 | 17 | for (int i = 0; i < num; i++) { 18 | particles.add(new Particle(origin.x,origin.y)); // Add "num" amount of particles to the ArrayList 19 | } 20 | } 21 | 22 | void run() { 23 | // Display all the particles 24 | for (Particle p: particles) { 25 | p.display(); 26 | } 27 | 28 | // Particles that leave the screen, we delete them 29 | // (note they have to be deleted from both the box2d world and our list 30 | for (int i = particles.size()-1; i >= 0; i--) { 31 | Particle p = particles.get(i); 32 | if (p.done()) { 33 | particles.remove(i); 34 | } 35 | } 36 | } 37 | 38 | void addParticles(int n) { 39 | for (int i = 0; i < n; i++) { 40 | particles.add(new Particle(origin.x,origin.y)); 41 | } 42 | } 43 | 44 | // A method to test if the particle system still has particles 45 | boolean dead() { 46 | if (particles.isEmpty()) { 47 | return true; 48 | } 49 | else { 50 | return false; 51 | } 52 | } 53 | 54 | } 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Mouse/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class (now incorporates angle) 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | // But we also have to make a body for box2d to know about it 16 | Body b; 17 | 18 | Boundary(float x_,float y_, float w_, float h_, float a) { 19 | x = x_; 20 | y = y_; 21 | w = w_; 22 | h = h_; 23 | 24 | // Define the polygon 25 | PolygonShape sd = new PolygonShape(); 26 | // Figure out the box2d coordinates 27 | float box2dW = box2d.scalarPixelsToWorld(w/2); 28 | float box2dH = box2d.scalarPixelsToWorld(h/2); 29 | // We're just a box 30 | sd.setAsBox(box2dW, box2dH); 31 | 32 | 33 | // Create the body 34 | BodyDef bd = new BodyDef(); 35 | bd.type = BodyType.STATIC; 36 | bd.angle = a; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | } 43 | 44 | // Draw the boundary, if it were at an angle we'd have to do something fancier 45 | void display() { 46 | noFill(); 47 | stroke(0); 48 | strokeWeight(1); 49 | rectMode(CENTER); 50 | 51 | float a = b.getAngle(); 52 | 53 | pushMatrix(); 54 | translate(x,y); 55 | rotate(-a); 56 | rect(0,0,w,h); 57 | popMatrix(); 58 | } 59 | 60 | } 61 | 62 | 63 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Mouse/Box.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A rectangular box 7 | 8 | class Box { 9 | 10 | // We need to keep track of a Body and a width and height 11 | Body body; 12 | float w; 13 | float h; 14 | 15 | // Constructor 16 | Box(float x_, float y_) { 17 | float x = x_; 18 | float y = y_; 19 | w = 24; 20 | h = 24; 21 | // Add the box to the box2d world 22 | makeBody(new Vec2(x,y),w,h); 23 | } 24 | 25 | // This function removes the particle from the box2d world 26 | void killBody() { 27 | box2d.destroyBody(body); 28 | } 29 | 30 | boolean contains(float x, float y) { 31 | Vec2 worldPoint = box2d.coordPixelsToWorld(x, y); 32 | Fixture f = body.getFixtureList(); 33 | boolean inside = f.testPoint(worldPoint); 34 | return inside; 35 | } 36 | 37 | // Drawing the box 38 | void display() { 39 | // We look at each body and get its screen position 40 | Vec2 pos = box2d.getBodyPixelCoord(body); 41 | // Get its angle of rotation 42 | float a = body.getAngle(); 43 | 44 | rectMode(PConstants.CENTER); 45 | pushMatrix(); 46 | translate(pos.x,pos.y); 47 | rotate(a); 48 | fill(175); 49 | stroke(0); 50 | rect(0,0,w,h); 51 | popMatrix(); 52 | } 53 | 54 | 55 | // This function adds the rectangle to the box2d world 56 | void makeBody(Vec2 center, float w_, float h_) { 57 | // Define and create the body 58 | BodyDef bd = new BodyDef(); 59 | bd.type = BodyType.DYNAMIC; 60 | bd.position.set(box2d.coordPixelsToWorld(center)); 61 | body = box2d.createBody(bd); 62 | 63 | // Define a polygon (this is what we use for a rectangle) 64 | PolygonShape sd = new PolygonShape(); 65 | float box2dW = box2d.scalarPixelsToWorld(w_/2); 66 | float box2dH = box2d.scalarPixelsToWorld(h_/2); 67 | sd.setAsBox(box2dW, box2dH); 68 | 69 | // Define a fixture 70 | FixtureDef fd = new FixtureDef(); 71 | fd.shape = sd; 72 | // Parameters that affect physics 73 | fd.density = 1; 74 | fd.friction = 0.3; 75 | fd.restitution = 0.5; 76 | 77 | body.createFixture(fd); 78 | //body.setMassFromShapes(); 79 | 80 | // Give it some initial random velocity 81 | body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); 82 | body.setAngularVelocity(random(-5, 5)); 83 | } 84 | 85 | } 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Mouse/Mouse.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Basic example of controlling an object with the mouse (by attaching a spring) 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.common.*; 10 | import org.jbox2d.dynamics.joints.*; 11 | import org.jbox2d.collision.shapes.*; 12 | import org.jbox2d.collision.shapes.Shape; 13 | import org.jbox2d.common.*; 14 | import org.jbox2d.dynamics.*; 15 | 16 | // A reference to our box2d world 17 | Box2DProcessing box2d; 18 | 19 | // A list we'll use to track fixed objects 20 | ArrayList boundaries; 21 | 22 | // Just a single box this time 23 | Box box; 24 | 25 | // The Spring that will attach to the box from the mouse 26 | Spring spring; 27 | 28 | void setup() { 29 | size(640,360); 30 | smooth(); 31 | 32 | // Initialize box2d physics and create the world 33 | box2d = new Box2DProcessing(this); 34 | box2d.createWorld(); 35 | 36 | // Make the box 37 | box = new Box(width/2,height/2); 38 | 39 | // Make the spring (it doesn't really get initialized until the mouse is clicked) 40 | spring = new Spring(); 41 | 42 | // Add a bunch of fixed boundaries 43 | boundaries = new ArrayList(); 44 | boundaries.add(new Boundary(width/2,height-5,width,10,0)); 45 | boundaries.add(new Boundary(width/2,5,width,10,0)); 46 | boundaries.add(new Boundary(width-5,height/2,10,height,0)); 47 | boundaries.add(new Boundary(5,height/2,10,height,0)); 48 | } 49 | 50 | // When the mouse is released we're done with the spring 51 | void mouseReleased() { 52 | spring.destroy(); 53 | } 54 | 55 | // When the mouse is pressed we. . . 56 | void mousePressed() { 57 | // Check to see if the mouse was clicked on the box 58 | if (box.contains(mouseX, mouseY)) { 59 | // And if so, bind the mouse location to the box with a spring 60 | spring.bind(mouseX,mouseY,box); 61 | } 62 | } 63 | 64 | void draw() { 65 | background(255); 66 | 67 | // We must always step through time! 68 | box2d.step(); 69 | 70 | // Always alert the spring to the new mouse location 71 | spring.update(mouseX,mouseY); 72 | 73 | // Draw the boundaries 74 | for (int i = 0; i < boundaries.size(); i++) { 75 | Boundary wall = (Boundary) boundaries.get(i); 76 | wall.display(); 77 | } 78 | 79 | // Draw the box 80 | box.display(); 81 | // Draw the spring (it only appears when active) 82 | spring.display(); 83 | } 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Mouse/Spring.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // Class to describe the spring joint (displayed as a line) 7 | 8 | class Spring { 9 | 10 | // This is the box2d object we need to create 11 | MouseJoint mouseJoint; 12 | 13 | Spring() { 14 | // At first it doesn't exist 15 | mouseJoint = null; 16 | } 17 | 18 | // If it exists we set its target to the mouse location 19 | void update(float x, float y) { 20 | if (mouseJoint != null) { 21 | // Always convert to world coordinates! 22 | Vec2 mouseWorld = box2d.coordPixelsToWorld(x,y); 23 | mouseJoint.setTarget(mouseWorld); 24 | } 25 | } 26 | 27 | void display() { 28 | if (mouseJoint != null) { 29 | // We can get the two anchor points 30 | Vec2 v1 = new Vec2(0,0); 31 | mouseJoint.getAnchorA(v1); 32 | Vec2 v2 = new Vec2(0,0); 33 | mouseJoint.getAnchorB(v2); 34 | // Convert them to screen coordinates 35 | v1 = box2d.coordWorldToPixels(v1); 36 | v2 = box2d.coordWorldToPixels(v2); 37 | // And just draw a line 38 | stroke(0); 39 | strokeWeight(1); 40 | line(v1.x,v1.y,v2.x,v2.y); 41 | } 42 | } 43 | 44 | 45 | // This is the key function where 46 | // we attach the spring to an x,y location 47 | // and the Box object's location 48 | void bind(float x, float y, Box box) { 49 | // Define the joint 50 | MouseJointDef md = new MouseJointDef(); 51 | // Body A is just a fake ground body for simplicity (there isn't anything at the mouse) 52 | md.bodyA = box2d.getGroundBody(); 53 | // Body 2 is the box's boxy 54 | md.bodyB = box.body; 55 | // Get the mouse location in world coordinates 56 | Vec2 mp = box2d.coordPixelsToWorld(x,y); 57 | // And that's the target 58 | md.target.set(mp); 59 | // Some stuff about how strong and bouncy the spring should be 60 | md.maxForce = 1000.0 * box.body.m_mass; 61 | md.frequencyHz = 5.0; 62 | md.dampingRatio = 0.9; 63 | 64 | // Make the joint! 65 | mouseJoint = (MouseJoint) box2d.world.createJoint(md); 66 | } 67 | 68 | void destroy() { 69 | // We can get rid of the joint when the mouse is released 70 | if (mouseJoint != null) { 71 | box2d.world.destroyJoint(mouseJoint); 72 | mouseJoint = null; 73 | } 74 | } 75 | 76 | } 77 | 78 | 79 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/MultiShapes/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class (now incorporates angle) 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | // But we also have to make a body for box2d to know about it 16 | Body b; 17 | 18 | Boundary(float x_,float y_, float w_, float h_, float a) { 19 | x = x_; 20 | y = y_; 21 | w = w_; 22 | h = h_; 23 | 24 | // Define the polygon 25 | PolygonShape sd = new PolygonShape(); 26 | // Figure out the box2d coordinates 27 | float box2dW = box2d.scalarPixelsToWorld(w/2); 28 | float box2dH = box2d.scalarPixelsToWorld(h/2); 29 | // We're just a box 30 | sd.setAsBox(box2dW, box2dH); 31 | 32 | 33 | // Create the body 34 | BodyDef bd = new BodyDef(); 35 | bd.type = BodyType.STATIC; 36 | bd.angle = a; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | } 43 | 44 | // Draw the boundary, if it were at an angle we'd have to do something fancier 45 | void display() { 46 | fill(0); 47 | stroke(0); 48 | strokeWeight(1); 49 | rectMode(CENTER); 50 | 51 | float a = b.getAngle(); 52 | 53 | pushMatrix(); 54 | translate(x,y); 55 | rotate(-a); 56 | rect(0,0,w,h); 57 | popMatrix(); 58 | } 59 | 60 | } 61 | 62 | 63 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/MultiShapes/Lollipop.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // A rectangular box 7 | class Lollipop { 8 | 9 | // We need to keep track of a Body and a width and height 10 | Body body; 11 | float w; 12 | float h; 13 | float r; 14 | 15 | // Constructor 16 | Lollipop(float x, float y) { 17 | w = 8; 18 | h = 24; 19 | r = 8; 20 | // Add the box to the box2d world 21 | makeBody(new Vec2(x, y)); 22 | } 23 | 24 | // This function removes the particle from the box2d world 25 | void killBody() { 26 | box2d.destroyBody(body); 27 | } 28 | 29 | // Is the particle ready for deletion? 30 | boolean done() { 31 | // Let's find the screen position of the particle 32 | Vec2 pos = box2d.getBodyPixelCoord(body); 33 | // Is it off the bottom of the screen? 34 | if (pos.y > height+w*h) { 35 | killBody(); 36 | return true; 37 | } 38 | return false; 39 | } 40 | 41 | // Drawing the lollipop 42 | void display() { 43 | // We look at each body and get its screen position 44 | Vec2 pos = box2d.getBodyPixelCoord(body); 45 | // Get its angle of rotation 46 | float a = body.getAngle(); 47 | 48 | rectMode(CENTER); 49 | pushMatrix(); 50 | translate(pos.x, pos.y); 51 | rotate(-a); 52 | fill(175); 53 | stroke(0); 54 | 55 | rect(0,0,w,h); 56 | ellipse(0, h/2, r*2, r*2); 57 | popMatrix(); 58 | } 59 | 60 | // This function adds the rectangle to the box2d world 61 | void makeBody(Vec2 center) { 62 | 63 | // Define the body and make it from the shape 64 | BodyDef bd = new BodyDef(); 65 | bd.type = BodyType.DYNAMIC; 66 | bd.position.set(box2d.coordPixelsToWorld(center)); 67 | body = box2d.createBody(bd); 68 | 69 | CircleShape circle = new CircleShape(); 70 | circle.m_radius = box2d.scalarPixelsToWorld(r); 71 | Vec2 offset = new Vec2(0,h/2); 72 | offset = box2d.vectorPixelsToWorld(offset); 73 | circle.m_p.set(offset.x,offset.y); 74 | 75 | PolygonShape sd = new PolygonShape(); 76 | float box2dW = box2d.scalarPixelsToWorld(w/2); 77 | float box2dH = box2d.scalarPixelsToWorld(h/2); 78 | sd.setAsBox(box2dW, box2dH); 79 | 80 | body.createFixture(sd,1.0); 81 | body.createFixture(circle, 1.0); 82 | 83 | // Give it some initial random velocity 84 | body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); 85 | body.setAngularVelocity(random(-5, 5)); 86 | } 87 | } 88 | 89 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/MultiShapes/MultiShapes.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Basic example of falling rectangles 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.collision.shapes.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.*; 12 | 13 | // A reference to our box2d world 14 | Box2DProcessing box2d; 15 | 16 | // A list we'll use to track fixed objects 17 | ArrayList boundaries; 18 | // A list for all of our rectangles 19 | ArrayList pops; 20 | 21 | void setup() { 22 | size(640,360); 23 | smooth(); 24 | 25 | // Initialize box2d physics and create the world 26 | box2d = new Box2DProcessing(this,20); 27 | box2d.createWorld(); 28 | // We are setting a custom gravity 29 | box2d.setGravity(0, -20); 30 | 31 | // Create ArrayLists 32 | pops = new ArrayList(); 33 | boundaries = new ArrayList(); 34 | 35 | // Add a bunch of fixed boundaries 36 | boundaries.add(new Boundary(width/4,height-5,width/2-50,10,0)); 37 | boundaries.add(new Boundary(3*width/4,height-50,width/2-50,10,0)); 38 | boundaries.add(new Boundary(width-5,height/2,10,height,0)); 39 | boundaries.add(new Boundary(5,height/2,10,height,0)); 40 | } 41 | 42 | void draw() { 43 | background(255); 44 | 45 | // We must always step through time! 46 | box2d.step(); 47 | 48 | // Display all the boundaries 49 | for (Boundary wall: boundaries) { 50 | wall.display(); 51 | } 52 | 53 | // Display all the people 54 | for (Lollipop p: pops) { 55 | p.display(); 56 | } 57 | 58 | // people that leave the screen, we delete them 59 | // (note they have to be deleted from both the box2d world and our list 60 | for (int i = pops.size()-1; i >= 0; i--) { 61 | Lollipop p = pops.get(i); 62 | if (p.done()) { 63 | pops.remove(i); 64 | } 65 | } 66 | } 67 | 68 | void mousePressed() { 69 | Lollipop p = new Lollipop(mouseX,mouseY); 70 | pops.add(p); 71 | } 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Polygons/Boundary.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // A fixed boundary class (now incorporates angle) 7 | 8 | class Boundary { 9 | 10 | // A boundary is a simple rectangle with x,y,width,and height 11 | float x; 12 | float y; 13 | float w; 14 | float h; 15 | // But we also have to make a body for box2d to know about it 16 | Body b; 17 | 18 | Boundary(float x_,float y_, float w_, float h_, float a) { 19 | x = x_; 20 | y = y_; 21 | w = w_; 22 | h = h_; 23 | 24 | // Define the polygon 25 | PolygonShape sd = new PolygonShape(); 26 | // Figure out the box2d coordinates 27 | float box2dW = box2d.scalarPixelsToWorld(w/2); 28 | float box2dH = box2d.scalarPixelsToWorld(h/2); 29 | // We're just a box 30 | sd.setAsBox(box2dW, box2dH); 31 | 32 | 33 | // Create the body 34 | BodyDef bd = new BodyDef(); 35 | bd.type = BodyType.STATIC; 36 | bd.angle = a; 37 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 38 | b = box2d.createBody(bd); 39 | 40 | // Attached the shape to the body using a Fixture 41 | b.createFixture(sd,1); 42 | } 43 | 44 | // Draw the boundary, it doesn't move so we don't have to ask the Body for location 45 | void display() { 46 | fill(0); 47 | stroke(0); 48 | strokeWeight(1); 49 | rectMode(CENTER); 50 | float a = b.getAngle(); 51 | pushMatrix(); 52 | translate(x,y); 53 | rotate(-a); 54 | rect(0,0,w,h); 55 | popMatrix(); 56 | } 57 | 58 | } 59 | 60 | 61 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Polygons/CustomShape.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // A rectangular box 7 | class CustomShape { 8 | 9 | // We need to keep track of a Body and a width and height 10 | Body body; 11 | 12 | // Constructor 13 | CustomShape(float x, float y) { 14 | // Add the box to the box2d world 15 | makeBody(new Vec2(x, y)); 16 | } 17 | 18 | // This function removes the particle from the box2d world 19 | void killBody() { 20 | box2d.destroyBody(body); 21 | } 22 | 23 | // Is the particle ready for deletion? 24 | boolean done() { 25 | // Let's find the screen position of the particle 26 | Vec2 pos = box2d.getBodyPixelCoord(body); 27 | // Is it off the bottom of the screen? 28 | if (pos.y > height) { 29 | killBody(); 30 | return true; 31 | } 32 | return false; 33 | } 34 | 35 | // Drawing the box 36 | void display() { 37 | // We look at each body and get its screen position 38 | Vec2 pos = box2d.getBodyPixelCoord(body); 39 | // Get its angle of rotation 40 | float a = body.getAngle(); 41 | 42 | Fixture f = body.getFixtureList(); 43 | PolygonShape ps = (PolygonShape) f.getShape(); 44 | 45 | 46 | rectMode(CENTER); 47 | pushMatrix(); 48 | translate(pos.x, pos.y); 49 | rotate(-a); 50 | fill(175); 51 | stroke(0); 52 | beginShape(); 53 | //println(vertices.length); 54 | // For every vertex, convert to pixel vector 55 | for (int i = 0; i < ps.getVertexCount(); i++) { 56 | Vec2 v = box2d.vectorWorldToPixels(ps.getVertex(i)); 57 | vertex(v.x, v.y); 58 | } 59 | endShape(CLOSE); 60 | popMatrix(); 61 | } 62 | 63 | // This function adds the rectangle to the box2d world 64 | void makeBody(Vec2 center) { 65 | 66 | // Define a polygon (this is what we use for a rectangle) 67 | PolygonShape sd = new PolygonShape(); 68 | 69 | Vec2[] vertices = new Vec2[4]; 70 | vertices[0] = box2d.vectorPixelsToWorld(new Vec2(-15, 25)); 71 | vertices[1] = box2d.vectorPixelsToWorld(new Vec2(15, 0)); 72 | vertices[2] = box2d.vectorPixelsToWorld(new Vec2(20, -15)); 73 | vertices[3] = box2d.vectorPixelsToWorld(new Vec2(-10, -10)); 74 | 75 | sd.set(vertices, vertices.length); 76 | 77 | // Define the body and make it from the shape 78 | BodyDef bd = new BodyDef(); 79 | bd.type = BodyType.DYNAMIC; 80 | bd.position.set(box2d.coordPixelsToWorld(center)); 81 | body = box2d.createBody(bd); 82 | 83 | body.createFixture(sd, 1.0); 84 | 85 | 86 | // Give it some initial random velocity 87 | body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); 88 | body.setAngularVelocity(random(-5, 5)); 89 | } 90 | } 91 | 92 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/Polygons/Polygons.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Basic example of falling rectangles 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.collision.shapes.*; 10 | import org.jbox2d.common.*; 11 | import org.jbox2d.dynamics.*; 12 | 13 | // A reference to our box2d world 14 | Box2DProcessing box2d; 15 | 16 | // A list we'll use to track fixed objects 17 | ArrayList boundaries; 18 | // A list for all of our rectangles 19 | ArrayList polygons; 20 | 21 | void setup() { 22 | size(640,360); 23 | smooth(); 24 | 25 | // Initialize box2d physics and create the world 26 | box2d = new Box2DProcessing(this); 27 | box2d.createWorld(); 28 | // We are setting a custom gravity 29 | box2d.setGravity(0, -20); 30 | 31 | // Create ArrayLists 32 | polygons = new ArrayList(); 33 | boundaries = new ArrayList(); 34 | 35 | // Add a bunch of fixed boundaries 36 | boundaries.add(new Boundary(width/4,height-5,width/2-50,10,0)); 37 | boundaries.add(new Boundary(3*width/4,height-50,width/2-50,10,0)); 38 | boundaries.add(new Boundary(width-5,height/2,10,height,0)); 39 | boundaries.add(new Boundary(5,height/2,10,height,0)); 40 | } 41 | 42 | void draw() { 43 | background(255); 44 | 45 | // We must always step through time! 46 | box2d.step(); 47 | 48 | // Display all the boundaries 49 | for (Boundary wall: boundaries) { 50 | wall.display(); 51 | } 52 | 53 | // Display all the people 54 | for (CustomShape cs: polygons) { 55 | cs.display(); 56 | } 57 | 58 | // people that leave the screen, we delete them 59 | // (note they have to be deleted from both the box2d world and our list 60 | for (int i = polygons.size()-1; i >= 0; i--) { 61 | CustomShape cs = polygons.get(i); 62 | if (cs.done()) { 63 | polygons.remove(i); 64 | } 65 | } 66 | } 67 | 68 | void mousePressed() { 69 | CustomShape cs = new CustomShape(mouseX,mouseY); 70 | polygons.add(cs); 71 | } 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/RevoluteJointExample/Box.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // A rectangular box 7 | 8 | class Box { 9 | 10 | // We need to keep track of a Body and a width and height 11 | Body body; 12 | float w; 13 | float h; 14 | 15 | // Constructor 16 | Box(float x, float y, float w_, float h_, boolean lock) { 17 | w = w_; 18 | h = h_; 19 | 20 | // Define and create the body 21 | BodyDef bd = new BodyDef(); 22 | bd.position.set(box2d.coordPixelsToWorld(new Vec2(x,y))); 23 | if (lock) bd.type = BodyType.STATIC; 24 | else bd.type = BodyType.DYNAMIC; 25 | 26 | body = box2d.createBody(bd); 27 | 28 | // Define the shape -- a (this is what we use for a rectangle) 29 | PolygonShape sd = new PolygonShape(); 30 | float box2dW = box2d.scalarPixelsToWorld(w/2); 31 | float box2dH = box2d.scalarPixelsToWorld(h/2); 32 | sd.setAsBox(box2dW, box2dH); 33 | 34 | // Define a fixture 35 | FixtureDef fd = new FixtureDef(); 36 | fd.shape = sd; 37 | // Parameters that affect physics 38 | fd.density = 1; 39 | fd.friction = 0.3; 40 | fd.restitution = 0.5; 41 | 42 | body.createFixture(fd); 43 | 44 | // Give it some initial random velocity 45 | body.setLinearVelocity(new Vec2(random(-5,5),random(2,5))); 46 | body.setAngularVelocity(random(-5,5)); 47 | } 48 | 49 | // This function removes the particle from the box2d world 50 | void killBody() { 51 | box2d.destroyBody(body); 52 | } 53 | 54 | // Drawing the box 55 | void display() { 56 | // We look at each body and get its screen position 57 | Vec2 pos = box2d.getBodyPixelCoord(body); 58 | // Get its angle of rotation 59 | float a = body.getAngle(); 60 | 61 | rectMode(PConstants.CENTER); 62 | pushMatrix(); 63 | translate(pos.x,pos.y); 64 | rotate(-a); 65 | fill(175); 66 | stroke(0); 67 | rect(0,0,w,h); 68 | popMatrix(); 69 | } 70 | } 71 | 72 | 73 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/RevoluteJointExample/Particle.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2010 4 | // Box2DProcessing example 5 | 6 | // A circular particle 7 | 8 | class Particle { 9 | 10 | // We need to keep track of a Body and a radius 11 | Body body; 12 | float r; 13 | 14 | color col; 15 | 16 | Particle(float x, float y, float r_) { 17 | r = r_; 18 | // This function puts the particle in the Box2d world 19 | makeBody(x, y, r); 20 | body.setUserData(this); 21 | 22 | col = color(175); 23 | } 24 | 25 | // This function removes the particle from the box2d world 26 | void killBody() { 27 | box2d.destroyBody(body); 28 | } 29 | 30 | // Change color when hit 31 | void change() { 32 | col = color(255, 0, 0); 33 | } 34 | 35 | // Is the particle ready for deletion? 36 | boolean done() { 37 | // Let's find the screen position of the particle 38 | Vec2 pos = box2d.getBodyPixelCoord(body); 39 | // Is it off the bottom of the screen? 40 | if (pos.y > height+r*2) { 41 | killBody(); 42 | return true; 43 | } 44 | return false; 45 | } 46 | 47 | // 48 | void display() { 49 | // We look at each body and get its screen position 50 | Vec2 pos = box2d.getBodyPixelCoord(body); 51 | // Get its angle of rotation 52 | float a = body.getAngle(); 53 | pushMatrix(); 54 | translate(pos.x, pos.y); 55 | 56 | rotate(-a); 57 | fill(col); 58 | stroke(0); 59 | strokeWeight(1); 60 | ellipse(0, 0, r*2, r*2); 61 | // Let's add a line so we can see the rotation 62 | line(0, 0, r, 0); 63 | popMatrix(); 64 | } 65 | 66 | // Here's our function that adds the particle to the Box2D world 67 | void makeBody(float x, float y, float r) { 68 | // Define a body 69 | BodyDef bd = new BodyDef(); 70 | // Set its position 71 | bd.position = box2d.coordPixelsToWorld(x, y); 72 | bd.type = BodyType.DYNAMIC; 73 | 74 | body = box2d.world.createBody(bd); 75 | 76 | // Make the body's shape a circle 77 | CircleShape cs = new CircleShape(); 78 | cs.m_radius = box2d.scalarPixelsToWorld(r); 79 | 80 | FixtureDef fd = new FixtureDef(); 81 | fd.shape = cs; 82 | 83 | fd.density = 2.0; 84 | fd.friction = 0.01; 85 | fd.restitution = 0.3; // Restitution is bounciness 86 | 87 | body.createFixture(fd); 88 | 89 | // Give it a random initial velocity (and angular velocity) 90 | //body.setLinearVelocity(new Vec2(random(-10f,10f),random(5f,10f))); 91 | body.setAngularVelocity(random(-10, 10)); 92 | } 93 | } 94 | 95 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/RevoluteJointExample/RevoluteJointExample.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 201 4 | // Box2DProcessing example 5 | 6 | // Example demonstrating revolute joint 7 | 8 | import shiffman.box2d.*; 9 | import org.jbox2d.common.*; 10 | import org.jbox2d.dynamics.joints.*; 11 | import org.jbox2d.collision.shapes.*; 12 | import org.jbox2d.collision.shapes.Shape; 13 | import org.jbox2d.common.*; 14 | import org.jbox2d.dynamics.*; 15 | import org.jbox2d.dynamics.contacts.*; 16 | 17 | // A reference to our box2d world 18 | Box2DProcessing box2d; 19 | 20 | // An object to describe a Windmill (two bodies and one joint) 21 | Windmill windmill; 22 | 23 | // An ArrayList of particles that will fall on the surface 24 | ArrayList particles; 25 | 26 | void setup() { 27 | size(400,300); 28 | smooth(); 29 | 30 | // Initialize box2d physics and create the world 31 | box2d = new Box2DProcessing(this); 32 | box2d.createWorld(); 33 | 34 | // Make the windmill at an x,y location 35 | windmill = new Windmill(200,200); 36 | 37 | // Create the empty list 38 | particles = new ArrayList(); 39 | 40 | } 41 | 42 | // Click the mouse to turn on or off the motor 43 | void mousePressed() { 44 | windmill.toggleMotor(); 45 | } 46 | 47 | void draw() { 48 | background(255); 49 | 50 | if (random(1) < 0.1) { 51 | float sz = random(4,8); 52 | particles.add(new Particle(random(150,250),-20,sz)); 53 | } 54 | 55 | 56 | // We must always step through time! 57 | box2d.step(); 58 | 59 | // Look at all particles 60 | for (int i = particles.size()-1; i >= 0; i--) { 61 | Particle p = particles.get(i); 62 | p.display(); 63 | // Particles that leave the screen, we delete them 64 | // (note they have to be deleted from both the box2d world and our list 65 | if (p.done()) { 66 | particles.remove(i); 67 | } 68 | } 69 | 70 | // Draw the windmill 71 | windmill.display(); 72 | 73 | String status = "OFF"; 74 | if (windmill.motorOn()) status = "ON"; 75 | 76 | fill(0); 77 | text("Click mouse to toggle motor.\nMotor: " + status,10,270); 78 | 79 | 80 | } 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/RevoluteJointExample/Windmill.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2012 4 | // Box2DProcessing example 5 | 6 | // Class to describe a fixed spinning object 7 | 8 | class Windmill { 9 | 10 | // Our object is two boxes and one joint 11 | // Consider making the fixed box much smaller and not drawing it 12 | RevoluteJoint joint; 13 | Box box1; 14 | Box box2; 15 | 16 | Windmill(float x, float y) { 17 | 18 | // Initialize locations of two boxes 19 | box1 = new Box(x, y-20, 120, 10, false); 20 | box2 = new Box(x, y, 10, 40, true); 21 | 22 | // Define joint as between two bodies 23 | RevoluteJointDef rjd = new RevoluteJointDef(); 24 | 25 | Vec2 offset = box2d.vectorPixelsToWorld(new Vec2(0, 60)); 26 | 27 | rjd.initialize(box1.body, box2.body, box1.body.getWorldCenter()); 28 | 29 | // Turning on a motor (optional) 30 | rjd.motorSpeed = PI*2; // how fast? 31 | rjd.maxMotorTorque = 1000.0; // how powerful? 32 | rjd.enableMotor = true; // is it on? 33 | 34 | // There are many other properties you can set for a Revolute joint 35 | // For example, you can limit its angle between a minimum and a maximum 36 | // See box2d manual for more 37 | 38 | // Create the joint 39 | joint = (RevoluteJoint) box2d.world.createJoint(rjd); 40 | } 41 | 42 | // Turn the motor on or off 43 | void toggleMotor() { 44 | joint.enableMotor(!joint.isMotorEnabled()); 45 | } 46 | 47 | boolean motorOn() { 48 | return joint.isMotorEnabled(); 49 | } 50 | 51 | 52 | void display() { 53 | box2.display(); 54 | box1.display(); 55 | 56 | // Draw anchor just for debug 57 | Vec2 anchor = box2d.coordWorldToPixels(box1.body.getWorldCenter()); 58 | fill(255, 0, 0); 59 | stroke(0); 60 | ellipse(anchor.x, anchor.y, 4, 4); 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/VectorStuff/VectorStuff.pde: -------------------------------------------------------------------------------- 1 | // The Nature of Code 2 | // 3 | // Spring 2011 4 | // Box2DProcessing example 5 | 6 | // Just demo-ing the basics of Vec2 vs. PVector 7 | 8 | import org.jbox2d.common.*; 9 | 10 | void setup() { 11 | size(400,300); 12 | // PVector a = new PVector(1,-1); 13 | // PVector b = new PVector(3,4); 14 | // a.add(b); 15 | // 16 | // PVector a = new PVector(1,-1); 17 | // PVector b = new PVector(3,4); 18 | // PVector c = PVector.add(a,b); 19 | // 20 | // Vec2 a = new Vec2(1,-1); 21 | // Vec2 b = new Vec2(3,4); 22 | // a.addLocal(b); 23 | // 24 | // Vec2 a = new Vec2(1,-1); 25 | // Vec2 b = new Vec2(3,4); 26 | // Vec2 c = a.add(b); 27 | // 28 | // PVector a = new PVector(1,-1); 29 | // float n = 5; 30 | // a.mult(n); 31 | // 32 | // PVector a = new PVector(1,-1); 33 | // float n = 5; 34 | // PVector c = PVector.mult(a,n); 35 | // 36 | // Vec2 a = new Vec2(1,-1); 37 | // float n = 5; 38 | // a.mulLocal(n); 39 | // 40 | // Vec2 a = new Vec2(1,-1); 41 | // float n = 5; 42 | // Vec2 c = a.mul(n); 43 | // 44 | // PVector a = new PVector(1,-1); 45 | // float m = a.mag(); 46 | // a.normalize(); 47 | 48 | Vec2 a = new Vec2(1,-1); 49 | float m = a.length(); 50 | a.normalize(); 51 | println(a.x + "," + a.y); 52 | } 53 | 54 | void draw() { 55 | noLoop(); 56 | } 57 | 58 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/box2d_exercise/Box.pde: -------------------------------------------------------------------------------- 1 | // A rectangular box 2 | class Box { 3 | 4 | float x,y; 5 | float w,h; 6 | 7 | // Constructor 8 | Box(float x_, float y_) { 9 | x = x_; 10 | y = y_; 11 | w = 16; 12 | h = 16; 13 | } 14 | 15 | // Drawing the box 16 | void display() { 17 | fill(175); 18 | stroke(0); 19 | rectMode(CENTER); 20 | rect(x,y,w,h); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/box2d_exercise/box2d_exercise.pde: -------------------------------------------------------------------------------- 1 | // A list for all of our rectangles 2 | ArrayList boxes; 3 | 4 | void setup() { 5 | size(400,300); 6 | smooth(); 7 | // Create ArrayLists 8 | boxes = new ArrayList(); 9 | } 10 | 11 | void draw() { 12 | background(255); 13 | 14 | // When the mouse is clicked, add a new Box object 15 | if (mousePressed) { 16 | Box p = new Box(mouseX,mouseY); 17 | boxes.add(p); 18 | } 19 | 20 | // Display all the boxes 21 | for (Box b: boxes) { 22 | b.display(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/box2d_exercise_solved/Box.pde: -------------------------------------------------------------------------------- 1 | // A rectangular box 2 | class Box { 3 | // Instead of any of the usual variables, we will store a reference to a Box2D Body 4 | Body body; 5 | 6 | float w,h; 7 | 8 | Box(float x, float y) { 9 | w = 16; 10 | h = 16; 11 | 12 | // Build Body 13 | BodyDef bd = new BodyDef(); 14 | bd.type = BodyType.DYNAMIC; 15 | bd.position.set(box2d.coordPixelsToWorld(x,y)); 16 | body = box2d.createBody(bd); 17 | 18 | 19 | // Define a polygon (this is what we use for a rectangle) 20 | PolygonShape sd = new PolygonShape(); 21 | float box2dW = box2d.scalarPixelsToWorld(w/2); 22 | float box2dH = box2d.scalarPixelsToWorld(h/2); // Box2D considers the width and height of a 23 | sd.setAsBox(box2dW, box2dH); // rectangle to be the distance from the 24 | // center to the edge (so half of what we 25 | // normally think of as width or height.) 26 | // Define a fixture 27 | FixtureDef fd = new FixtureDef(); 28 | fd.shape = sd; 29 | // Parameters that affect physics 30 | fd.density = 1; 31 | fd.friction = 0.3; 32 | fd.restitution = 0.5; 33 | 34 | // Attach Fixture to Body 35 | body.createFixture(fd); 36 | } 37 | 38 | void display() { 39 | // We need the Body’s location and angle 40 | Vec2 pos = box2d.getBodyPixelCoord(body); 41 | float a = body.getAngle(); 42 | 43 | pushMatrix(); 44 | translate(pos.x,pos.y); // Using the Vec2 position and float angle to 45 | rotate(-a); // translate and rotate the rectangle 46 | fill(175); 47 | stroke(0); 48 | rectMode(CENTER); 49 | rect(0,0,w,h); 50 | popMatrix(); 51 | } 52 | 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/examples/box2d_exercise_solved/box2d_exercise_solved.pde: -------------------------------------------------------------------------------- 1 | import shiffman.box2d.*; 2 | import org.jbox2d.collision.shapes.*; 3 | import org.jbox2d.common.*; 4 | import org.jbox2d.dynamics.*; 5 | 6 | // A list for all of our rectangles 7 | ArrayList boxes; 8 | 9 | Box2DProcessing box2d; 10 | 11 | void setup() { 12 | size(400,300); 13 | smooth(); 14 | // Initialize and create the Box2D world 15 | box2d = new Box2DProcessing(this); 16 | box2d.createWorld(); 17 | 18 | // Create ArrayLists 19 | boxes = new ArrayList(); 20 | } 21 | 22 | void draw() { 23 | background(255); 24 | 25 | // We must always step through time! 26 | box2d.step(); 27 | 28 | // When the mouse is clicked, add a new Box object 29 | if (mousePressed) { 30 | Box p = new Box(mouseX,mouseY); 31 | boxes.add(p); 32 | } 33 | 34 | // Display all the boxes 35 | for (Box b: boxes) { 36 | b.display(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/library.properties: -------------------------------------------------------------------------------- 1 | # More on this file here: https://github.com/processing/processing/wiki/Library-Basics # UTF-8 supported. # The name of your library as you want it formatted. name = Box2D for Processing # List of authors. Links can be provided using the syntax [author name](url). authorList = [Daniel Shiffman](http://www.shiffman.net/) # A web page for your library, NOT a direct link to where to download it. url = https://github.com/shiffman/Box2D-for-Processing # The category of your library, must be one (or many) of the following: # "3D" "Animation" "Compilations" "Data" # "Fabrication" "Geometry" "GUI" "Hardware" # "I/O" "Language" "Math" "Simulation" # "Sound" "Utilities" "Typography" "Video & Vision" # # If a value other than those listed is used, your library will listed as # "Other". category = Simulation # A short sentence (or fragment) to summarize the library's function. This will # be shown from inside the PDE when the library is being installed. Avoid # repeating the name of your library here. Also, avoid saying anything redundant # like mentioning that it's a library. This should start with a capitalized # letter, and end with a period. sentence = A library and set of examples for 2D physics simulation wrapping some aspects of [JBox2D](http://www.jbox2d.org/), a Java implementation of [Box2D](http://box2d.org/). # Additional information suitable for the Processing website. The value of # 'sentence' always will be prepended, so you should start by writing the # second sentence here. If your library only works on certain operating systems, # mention it here. paragraph = # Links in the 'sentence' and 'paragraph' attributes can be inserted using the # same syntax as for authors. # That is, [here is a link to Processing](http://processing.org/) # A version number that increments once with each release. This is used to # compare different versions of the same library, and check if an update is # available. You should think of it as a counter, counting the total number of # releases you've had. version = 4 # The version as the user will see it. If blank, the version attribute will be # used here. prettyVersion = 0.4 -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/library/box2d_processing.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiffman/Box2D-for-Processing/43d58c546bf0b95c4dcda9f7a7d171c19477f120/Box2D-for-Processing/dist/box2d_processing/library/box2d_processing.jar -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/library/jbox2d-library-2.3.1-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiffman/Box2D-for-Processing/43d58c546bf0b95c4dcda9f7a7d171c19477f120/Box2D-for-Processing/dist/box2d_processing/library/jbox2d-library-2.3.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/src/readme.txt: -------------------------------------------------------------------------------- 1 | See: https://github.com/shiffman/Box2D-for-Processing -------------------------------------------------------------------------------- /Box2D-for-Processing/dist/box2d_processing/src/shiffman/box2d/Box2DContactListener.java: -------------------------------------------------------------------------------- 1 | package shiffman.box2d; 2 | 3 | import java.lang.reflect.Method; 4 | 5 | 6 | import org.jbox2d.callbacks.ContactImpulse; 7 | import org.jbox2d.callbacks.ContactListener; 8 | import org.jbox2d.collision.Manifold; 9 | import org.jbox2d.dynamics.contacts.Contact; 10 | 11 | import processing.core.PApplet; 12 | 13 | public class Box2DContactListener implements ContactListener { 14 | PApplet parent; 15 | 16 | Method beginMethod; 17 | Method endMethod; 18 | Method postMethod; 19 | Method preMethod; 20 | 21 | Box2DContactListener(PApplet p){ 22 | parent = p; 23 | 24 | try { 25 | beginMethod = parent.getClass().getMethod("beginContact", new Class[] { Contact.class }); 26 | } catch (Exception e) { 27 | System.out.println("You are missing the beginContact() method. " + e); 28 | } 29 | 30 | try { 31 | endMethod = parent.getClass().getMethod("endContact", new Class[] { Contact.class }); 32 | } catch (Exception e) { 33 | System.out.println("You are missing the endContact() method. " + e); 34 | } 35 | try { 36 | postMethod = parent.getClass().getMethod("postSolve", new Class[] { Contact.class, ContactImpulse.class }); 37 | } catch (Exception e) { 38 | //System.out.println("You are missing the postSolve() method. " + e); 39 | } 40 | try { 41 | preMethod = parent.getClass().getMethod("preSolve", new Class[] { Contact.class, Manifold.class }); 42 | } catch (Exception e) { 43 | //System.out.println("You are missing the preSolve() method. " + e); 44 | } 45 | } 46 | 47 | 48 | 49 | 50 | 51 | public void beginContact(Contact c) { 52 | if (beginMethod != null) { 53 | try { 54 | beginMethod.invoke(parent, new Object[] { c }); 55 | } catch (Exception e) { 56 | System.out.println("Could not invoke the \"beginContact()\" method for some reason."); 57 | e.printStackTrace(); 58 | beginMethod = null; 59 | } 60 | } 61 | 62 | } 63 | 64 | public void endContact(Contact c) { 65 | if (endMethod != null) { 66 | try { 67 | endMethod.invoke(parent, new Object[] { c }); 68 | } catch (Exception e) { 69 | System.out.println("Could not invoke the \"removeContact()\" method for some reason."); 70 | e.printStackTrace(); 71 | endMethod = null; 72 | } 73 | } 74 | 75 | } 76 | 77 | public void postSolve(Contact c, ContactImpulse ci) { 78 | if (postMethod != null) { 79 | try { 80 | postMethod.invoke(parent, new Object[] { c,ci}); 81 | } catch (Exception e) { 82 | System.out.println("Could not invoke the \"postSolve()\" method for some reason."); 83 | e.printStackTrace(); 84 | postMethod = null; 85 | } 86 | } 87 | } 88 | 89 | 90 | public void preSolve(Contact c, Manifold m) { 91 | if (preMethod != null) { 92 | try { 93 | preMethod.invoke(parent, new Object[] { c,m}); 94 | } catch (Exception e) { 95 | System.out.println("Could not invoke the \"preSolve()\" method for some reason."); 96 | e.printStackTrace(); 97 | preMethod = null; 98 | } 99 | } 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/allclasses-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | All Classes 7 | 8 | 9 | 10 | 11 |

All Classes

12 |
13 | 18 |
19 | 20 | 21 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/allclasses-noframe.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | All Classes 7 | 8 | 9 | 10 | 11 |

All Classes

12 |
13 | 18 |
19 | 20 | 21 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/class-use/QuickTest.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Uses of Class QuickTest 7 | 8 | 9 | 10 | 11 | 17 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 36 |
37 | 64 | 65 |
66 |

Uses of Class
QuickTest

67 |
68 |
No usage of QuickTest
69 | 70 |
71 | 72 | 73 | 74 | 75 | 85 |
86 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/constant-values.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Constant Field Values 7 | 8 | 9 | 10 | 11 | 17 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 36 |
37 | 64 | 65 |
66 |

Constant Field Values

67 |

Contents

68 |
69 | 70 |
71 | 72 | 73 | 74 | 75 | 85 |
86 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/deprecated-list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Deprecated List 7 | 8 | 9 | 10 | 11 | 17 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 36 |
37 | 64 | 65 |
66 |

Deprecated API

67 |

Contents

68 |
69 | 70 |
71 | 72 | 73 | 74 | 75 | 85 |
86 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Generated Documentation (Untitled) 7 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | <noscript> 68 | <div>JavaScript is disabled on your browser.</div> 69 | </noscript> 70 | <h2>Frame Alert</h2> 71 | <p>This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to <a href="overview-summary.html">Non-frame version</a>.</p> 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/overview-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Overview List 7 | 8 | 9 | 10 | 11 | 12 |
13 |

Packages

14 | 18 |
19 |

 

20 | 21 | 22 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/overview-summary.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Overview 7 | 8 | 9 | 10 | 11 | 17 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 36 |
37 | 64 | 65 |
66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 |
Packages 
PackageDescription
shiffman.box2d 
79 |
80 | 81 |
82 | 83 | 84 | 85 | 86 | 96 |
97 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/overview-tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Class Hierarchy 7 | 8 | 9 | 10 | 11 | 17 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 36 |
37 | 64 | 65 |
66 |

Hierarchy For All Packages

67 | Package Hierarchies: 68 | 71 |
72 |
73 |

Class Hierarchy

74 |
    75 |
  • java.lang.Object 76 |
      77 |
    • shiffman.box2d.Box2DContactListener (implements org.jbox2d.callbacks.ContactListener)
    • 78 |
    • shiffman.box2d.Box2DProcessing
    • 79 |
    • processing.core.PApplet (implements processing.core.PConstants) 80 | 83 |
    • 84 |
    85 |
  • 86 |
87 |
88 | 89 |
90 | 91 | 92 | 93 | 94 | 104 |
105 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | &lt;Unnamed&gt; 7 | 8 | 9 | 10 | 11 |

<Unnamed>

12 |
13 |

Classes

14 | 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/package-list: -------------------------------------------------------------------------------- 1 | 2 | shiffman.box2d 3 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/package-summary.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 29 |
30 | 57 | 58 |
59 |

Package <Unnamed>

60 |
61 |
62 |
    63 |
  • 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 |
    Class Summary 
    ClassDescription
    QuickTest 
    77 |
  • 78 |
79 |
80 | 81 |
82 | 83 | 84 | 85 | 86 | 96 |
97 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/package-tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Class Hierarchy 7 | 8 | 9 | 10 | 11 | 17 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 36 |
37 | 64 | 65 |
66 |

Hierarchy For Package <Unnamed>

67 | Package Hierarchies: 68 | 71 |
72 |
73 |

Class Hierarchy

74 |
    75 |
  • java.lang.Object 76 |
      77 |
    • processing.core.PApplet (implements processing.core.PConstants) 78 | 81 |
    • 82 |
    83 |
  • 84 |
85 |
86 | 87 |
88 | 89 | 90 | 91 | 92 | 102 |
103 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/package-use.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Uses of Package 7 | 8 | 9 | 10 | 11 | 17 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 36 |
37 | 64 | 65 |
66 |

Uses of Package

67 |
68 |
No usage of
69 | 70 |
71 | 72 | 73 | 74 | 75 | 85 |
86 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/resources/background.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiffman/Box2D-for-Processing/43d58c546bf0b95c4dcda9f7a7d171c19477f120/Box2D-for-Processing/javadoc/resources/background.gif -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/resources/tab.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiffman/Box2D-for-Processing/43d58c546bf0b95c4dcda9f7a7d171c19477f120/Box2D-for-Processing/javadoc/resources/tab.gif -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/resources/titlebar.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiffman/Box2D-for-Processing/43d58c546bf0b95c4dcda9f7a7d171c19477f120/Box2D-for-Processing/javadoc/resources/titlebar.gif -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/resources/titlebar_end.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiffman/Box2D-for-Processing/43d58c546bf0b95c4dcda9f7a7d171c19477f120/Box2D-for-Processing/javadoc/resources/titlebar_end.gif -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/shiffman/box2d/class-use/Box2DContactListener.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Uses of Class shiffman.box2d.Box2DContactListener 7 | 8 | 9 | 10 | 11 | 17 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 36 |
37 | 64 | 65 |
66 |

Uses of Class
shiffman.box2d.Box2DContactListener

67 |
68 |
No usage of shiffman.box2d.Box2DContactListener
69 | 70 |
71 | 72 | 73 | 74 | 75 | 85 |
86 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/shiffman/box2d/class-use/Box2DProcessing.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Uses of Class shiffman.box2d.Box2DProcessing 7 | 8 | 9 | 10 | 11 | 17 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 36 |
37 | 64 | 65 |
66 |

Uses of Class
shiffman.box2d.Box2DProcessing

67 |
68 |
No usage of shiffman.box2d.Box2DProcessing
69 | 70 |
71 | 72 | 73 | 74 | 75 | 85 |
86 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/shiffman/box2d/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | shiffman.box2d 7 | 8 | 9 | 10 | 11 |

shiffman.box2d

12 |
13 |

Classes

14 | 18 |
19 | 20 | 21 | -------------------------------------------------------------------------------- /Box2D-for-Processing/javadoc/shiffman/box2d/package-use.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Uses of Package shiffman.box2d 7 | 8 | 9 | 10 | 11 | 17 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 36 |
37 | 64 | 65 |
66 |

Uses of Package
shiffman.box2d

67 |
68 |
No usage of shiffman.box2d
69 | 70 |
71 | 72 | 73 | 74 | 75 | 85 |
86 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /Box2D-for-Processing/lib/core.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiffman/Box2D-for-Processing/43d58c546bf0b95c4dcda9f7a7d171c19477f120/Box2D-for-Processing/lib/core.jar -------------------------------------------------------------------------------- /Box2D-for-Processing/lib/jbox2d-library-2.3.1-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shiffman/Box2D-for-Processing/43d58c546bf0b95c4dcda9f7a7d171c19477f120/Box2D-for-Processing/lib/jbox2d-library-2.3.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /Box2D-for-Processing/src/shiffman/box2d/Box2DContactListener.java: -------------------------------------------------------------------------------- 1 | package shiffman.box2d; 2 | 3 | import java.lang.reflect.Method; 4 | 5 | 6 | import org.jbox2d.callbacks.ContactImpulse; 7 | import org.jbox2d.callbacks.ContactListener; 8 | import org.jbox2d.collision.Manifold; 9 | import org.jbox2d.dynamics.contacts.Contact; 10 | 11 | import processing.core.PApplet; 12 | 13 | public class Box2DContactListener implements ContactListener { 14 | PApplet parent; 15 | 16 | Method beginMethod; 17 | Method endMethod; 18 | Method postMethod; 19 | Method preMethod; 20 | 21 | Box2DContactListener(PApplet p){ 22 | parent = p; 23 | 24 | try { 25 | beginMethod = parent.getClass().getMethod("beginContact", new Class[] { Contact.class }); 26 | } catch (Exception e) { 27 | System.out.println("You are missing the beginContact() method. " + e); 28 | } 29 | 30 | try { 31 | endMethod = parent.getClass().getMethod("endContact", new Class[] { Contact.class }); 32 | } catch (Exception e) { 33 | System.out.println("You are missing the endContact() method. " + e); 34 | } 35 | try { 36 | postMethod = parent.getClass().getMethod("postSolve", new Class[] { Contact.class, ContactImpulse.class }); 37 | } catch (Exception e) { 38 | //System.out.println("You are missing the postSolve() method. " + e); 39 | } 40 | try { 41 | preMethod = parent.getClass().getMethod("preSolve", new Class[] { Contact.class, Manifold.class }); 42 | } catch (Exception e) { 43 | //System.out.println("You are missing the preSolve() method. " + e); 44 | } 45 | } 46 | 47 | 48 | 49 | 50 | 51 | public void beginContact(Contact c) { 52 | if (beginMethod != null) { 53 | try { 54 | beginMethod.invoke(parent, new Object[] { c }); 55 | } catch (Exception e) { 56 | System.out.println("Could not invoke the \"beginContact()\" method for some reason."); 57 | e.printStackTrace(); 58 | beginMethod = null; 59 | } 60 | } 61 | 62 | } 63 | 64 | public void endContact(Contact c) { 65 | if (endMethod != null) { 66 | try { 67 | endMethod.invoke(parent, new Object[] { c }); 68 | } catch (Exception e) { 69 | System.out.println("Could not invoke the \"removeContact()\" method for some reason."); 70 | e.printStackTrace(); 71 | endMethod = null; 72 | } 73 | } 74 | 75 | } 76 | 77 | public void postSolve(Contact c, ContactImpulse ci) { 78 | if (postMethod != null) { 79 | try { 80 | postMethod.invoke(parent, new Object[] { c,ci}); 81 | } catch (Exception e) { 82 | System.out.println("Could not invoke the \"postSolve()\" method for some reason."); 83 | e.printStackTrace(); 84 | postMethod = null; 85 | } 86 | } 87 | } 88 | 89 | 90 | public void preSolve(Contact c, Manifold m) { 91 | if (preMethod != null) { 92 | try { 93 | preMethod.invoke(parent, new Object[] { c,m}); 94 | } catch (Exception e) { 95 | System.out.println("Could not invoke the \"preSolve()\" method for some reason."); 96 | e.printStackTrace(); 97 | preMethod = null; 98 | } 99 | } 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Daniel Shiffman 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, this 11 | list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 18 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 21 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Box2D for Processing 2 | 3 | A Processing library wrapping JBox2D (http://www.jbox2d.org/). 4 | 5 | Download library: http://shiffman-archive.netlify.app/p5/libraries/box2d_processing/box2d_processing.zip 6 | 7 | Tutorial and further examples for this library are available in The Nature of Code book: http://natureofcode.com. 8 | --------------------------------------------------------------------------------