Files
processing4/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/CollisionsAndControlInterface.pde

115 lines
2.3 KiB
Plaintext

// The Nature of Code
// <http://www.shiffman.net/teaching/nature>
// Spring 2010
// PBox2D example
// Basic example of controlling an object with our own motion (by attaching a MouseJoint)
// Also demonstrates how to know which object was hit
import pbox2d.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.contacts.*;
// A reference to our box2d world
PBox2D box2d;
// Just a single box this time
Box box;
// An ArrayList of particles that will fall on the surface
ArrayList<Particle> particles;
// The Spring that will attach to the box from the mouse
Spring spring;
// Perlin noise values
float xoff = 0;
float yoff = 1000;
void setup() {
size(400,300);
smooth();
// Initialize box2d physics and create the world
box2d = new PBox2D(this);
box2d.createWorld();
// Add a listener to listen for collisions!
box2d.world.setContactListener(new CustomListener());
// Make the box
box = new Box(width/2,height/2);
// Make the spring (it doesn't really get initialized until the mouse is clicked)
spring = new Spring();
spring.bind(width/2,height/2,box);
// Create the empty list
particles = new ArrayList<Particle>();
}
void draw() {
background(255);
if (random(1) < 0.2) {
float sz = random(4,8);
particles.add(new Particle(width/2,-20,sz));
}
// We must always step through time!
box2d.step();
// Make an x,y coordinate out of perlin noise
float x = noise(xoff)*width;
float y = noise(yoff)*height;
xoff += 0.01;
yoff += 0.01;
// This is tempting but will not work!
// box.body.setXForm(box2d.screenToWorld(x,y),0);
// Instead update the spring which pulls the mouse along
if (mousePressed) {
spring.update(mouseX,mouseY);
} else {
spring.update(x,y);
}
//box.body.setAngularVelocity(0);
// Look at all particles
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.display();
// Particles that leave the screen, we delete them
// (note they have to be deleted from both the box2d world and our list
if (p.done()) {
particles.remove(i);
}
}
// Draw the box
box.display();
// Draw the spring
// spring.display();
}