I am making a set of algorithms using the box2d library. I made 10 small boxes of p1 to p5. However, if you choose one of the following choices (p1 to p5), I hope it will appear in the middle of the screen, but I have no idea how to do it despite many attempts.
import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
Box2DProcessing box2d;
ArrayList<Boundary> boundaries;
ArrayList<Box> boxes;
int state=0;
void setup() {
size(2000, 500);
smooth();
textAlign(CENTER, BOTTOM);
box2d = new Box2DProcessing(this);
box2d.createWorld();
box2d.setGravity(0, -10);
boxes = new ArrayList<Box>();
boundaries = new ArrayList<Boundary>();
boundaries.add(new Boundary(0, height-5, width*2, 10));
boundaries.add(new Boundary(0, height-50, width*2, 10));
for (int i=0; i<10; i++) {
Box p1 = new Box(i*20+0, height-50);
Box p2 = new Box(i*20+400, height-50);
Box p3 = new Box(i*20+800, height-50);
Box p4 = new Box(i*20+1200, height-50);
Box p5 = new Box(i*20+1600, height-50);
boxes.add(p1);
boxes.add(p2);
boxes.add(p3);
boxes.add(p4);
boxes.add(p5);
}
}
void draw() {
background(255);
for (Boundary wall : boundaries) {
wall.display();
}
for (int i = boxes.size()-1; i >= 0; i--) {
Box b = boxes.get(i);
if (b.done()) {
boxes.remove(i);
}
}
switch(state) {
case 0:
println("0");
background(0, 255, 100);
textSize(72);
text("press spacebar", width/2, height/2+100);
if (key == ' ') {
state = 1;
}
break;
case 1:
println("1");
for (int i=0; i<boxes.size(); i++) {
boxes.get(i).display();
}
box2d.step();
break;
}
}
class Boundary {
float x;
float y;
float w;
float h;
Body b;
Boundary(float x_,float y_, float w_, float h_) {
x = x_;
y = y_;
w = w_;
h = h_;
PolygonShape sd = new PolygonShape();
float box2dW = box2d.scalarPixelsToWorld(w/2);
float box2dH = box2d.scalarPixelsToWorld(h/2);
sd.setAsBox(box2dW, box2dH);
BodyDef bd = new BodyDef();
bd.type = BodyType.STATIC;
bd.position.set(box2d.coordPixelsToWorld(x,y));
b = box2d.createBody(bd);
b.createFixture(sd,1);
}
void display() {
fill(0);
stroke(0);
rectMode(CENTER);
rect(x,y,w,h);
}
}
class Box {
Body body;
float w;
float h;
color c;
Box(float x, float y) {
w = 20;
h = 20;
initColor();
makeBody(new Vec2(x, y), w, h);
}
void initColor() {
int _c = int(random(10));
if (_c == 5) {
c = #87AD65;
}
if (_c == 6) {
c = #65942E;
}
if (_c == 7) {
c = #7E966F;
}
if (_c == 8) {
c = #264300;
}
if (_c == 9) {
c = #446919;
}
if (_c < 5) {
c = #000000;
}
}
void killBody() {
box2d.destroyBody(body);
}
boolean done() {
Vec2 pos = box2d.getBodyPixelCoord(body);
if (pos.y > height+w*h) {
killBody();
return true;
}
return false;
}
void display() {
Vec2 pos = box2d.getBodyPixelCoord(body);
if (green(c)>0) {
rectMode(CENTER);
pushMatrix();
translate(pos.x, pos.y);
fill(c);
//stroke(0);
noStroke();
rect(0, 0, w, h);
popMatrix();
} else {
}
}
void makeBody(Vec2 center, float w_, float h_) {
PolygonShape sd = new PolygonShape();
float box2dW = box2d.scalarPixelsToWorld(w_/2);
float box2dH = box2d.scalarPixelsToWorld(h_/2);
sd.setAsBox(box2dW, box2dH);
FixtureDef fd = new FixtureDef();
fd.shape = sd;
fd.density = 1;
fd.friction = 0.3;
fd.restitution = 0.1;
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(box2d.coordPixelsToWorld(center));
body = box2d.createBody(bd);
body.createFixture(fd);
body.setLinearVelocity(new Vec2(0, 0));
body.setAngularVelocity(0);
}
}