since I couldn't find so much examples of how to implement a custom rendering method with JBox2d, I've decided to share this code.
here is a link to a simple video where real-time boxes are drawn onto the screen using JBox2d and a custom rendering method :
http://www.vimeo.com/2654808
here is the code. you'll need BoxWrap2D library:
http://jbox2d.org/processing/
Quote:import org.jbox2d.p5.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.common.*;
import org.jbox2d.collision.*;
Physics physics;
float x1, y1, x2, y2;
boolean sizeSet; int count = 0; // to work around threading problems with size
void setup() {
size(600, 300, P3D);
frameRate(60);
sizeSet = false;
}
void draw() {
// hack to make sure that the sketch size is fully initialized
++count;
if ( (!sizeSet && count > 10) ) {
initScene();
}
}
void mouseDragged() {
if (mouseButton == LEFT) {
x2 = mouseX;
y2 = mouseY;
}
}
void mouseReleased() {
if (mouseButton == LEFT) {
Body randomBod = physics.createRect(x1, y1, x2, y2);
x2 = 0; y2 = 0;
}
}
void mousePressed() {
if (mouseButton == LEFT) {
x1 = mouseX;
y1 = mouseY;
x2 = x1;
y2 = y1;
}
}
void initScene() {
physics = new Physics(this, width, height);
physics.setDensity(1.0f);
physics.setCustomRenderingMethod(this, "sketchBlocks");
sizeSet = true;
}
void sketchBlocks(World w) {
if (frameCount%10 != 0) return;
background(255);
noFill(); stroke(0);
for (Body body = physics.getWorld().getBodyList(); body != null; body = body.getNext()) {
org.jbox2d.collision.Shape shape;
for (shape = body.getShapeList(); shape != null; shape = shape.getNext()) {
beginShape();
if (shape.getType() == ShapeType.POLYGON_SHAPE) {
PolygonShape poly = (PolygonShape)shape;
int count = poly.getVertexCount();
Vec2[] verts = poly.getVertices();
for(int i = 0; i < count; i++) {
Vec2 vert = physics.worldToScreen(body.getWorldPoint(verts[i]));
vertex(vert.x, vert.y);
}
Vec2 firstVert = physics.worldToScreen(body.getWorldPoint(verts[0]));
vertex(firstVert.x, firstVert.y);
for(int i = 0; i < count; i++) {
Vec2 vert = physics.worldToScreen(body.getWorldPoint(verts[i]));
vertex(vert.x-2+int(random(4)), vert.y-2+int(random(4)));
}
firstVert = physics.worldToScreen(body.getWorldPoint(verts[0]));
vertex(firstVert.x, firstVert.y);
}
endShape();
}
}
if (mousePressed && mouseButton == LEFT) {
rect(x1, y1, x2-x1, y2-y1);
}
}