import shiffman.box2d.*;
ArrayList<Box>boxes;
PBox2D box2d;
void setup(){
size(400,300);
boxes = new ArrayList<Box>();
box2d = new PBox2D(this);
box2d.createWorld();
}
void draw(){
box2d.step();
background(255);
if(mousePressed){
Box p = new Box(mouseX,mouseY);
boxes.add(p);
}
for(Box b:boxes){
b.display();
}
}
class Box{
Body body;
float w,h;
Box(){
w=16;
h=16;
BodyDef bd = new BodyDes();
bd.type = BodyType.DYNAMIC;
bd.position.set(box2d.coordPixelsToWorld(mouseX,mouseY));
body = box2d.createBody(bd);
PolygonShape ps = new PolygonShape();
float box2dw = box2d.scalarPixelsToWorld(w/2);
float box2dh = box2d.scalarPixelsToWorld(h/2);
ps.setAsBox(box2dw,box2dh);
FixtureDef fd = new FixtureDef();
fd.shape = ps;
fd.density = 1;
fd.friction = 0.3;
fd.restitution = 0.5;
body.createFixture(fd);
}
void display(){
Vec2 pos = box2d.getBodyPixelCoord(body);
float a = body.getAngle();
pushMatrix();
translate(pos.x,pos.y);
rotate(-a);
fill(175);
stroke(0);
rectMode(CENTER);
rect(0,0,w,h);
popMatrix();
}
void killBody(){
box2d.destroyBody(body);
}
}
1 Like
There is no PBox2D !? Guess you want to use Box2DProcessing.
You can always look at the attached examples in File->Examples->Contributed Libraries->Box2D for Processing…
Oh … and … regarding Box … this should be defined by your sketch … (look at the examples)
Cheers!
2 Likes
Yeah, true.
Did you install the library?
Your usage of Box with mouseX, mouseY looks suspicious because your constructor in the Box class doesn’t take parameters
1 Like
OH,I learned very thanks.
yea .I learned.you helped me
1 Like