Hi every one!
I’m starting with JBox2d, with the help of the book Nature of Code, by Daniel Shiffman.
Currently I´m making a sketch where boxes appear on the screen when you click the mouse an they just fall. The thing is, it never stop calculating the position of the boxex wich are outside the screen. So, I intruduces a function (killBox) in order to destroy the body object so the calculation will stop… in the future I will get it remove from the Array list too.
The real problem is; when it have to destroy de body object de proces call box2d.step() crash, somthing called “AssertionError”.
Any suggestion?
import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
Box2DProcessing box2d;
ArrayList<Box> boxes;
void setup() {
size(800, 800);
boxes = new ArrayList<Box>();
box2d = new Box2DProcessing(this);
box2d.createWorld();
}
void draw() {
background(0);
//here, in this function (box2d.step()) is were the error happen
box2d.step();
if (mousePressed) {
Box p = new Box(mouseX, mouseY);
boxes.add(p);
}
for (Box b : boxes) {
b.display();
b.killBox();
}
}
//Here starts the code for the Box class which is refered above
class Box {
float w;
float h;
Body body;
BodyDef bd= new BodyDef();
Box(float x, float y) {
w = random(5, 16);
h = random(5, 16);
bd.type = BodyType.DYNAMIC;
bd.position.set(box2d.coordPixelsToWorld(x, y));
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();
noFill();
strokeWeight(1);
stroke(255);
rectMode(CENTER);
pushMatrix();
translate(pos.x, pos.y);
rotate(-a);
rect(0, 0, w, h);
popMatrix();
}
//here is de killing function
void killBox() {
//we ask for the position of the body
Vec2 pos=box2d.getBodyPixelCoord(body);
//if the position of the body is 100 pixels outside the screen we call box2d to kill it
if (pos.y > (height+100)) {
box2d.destroyBody(body);
}
}
}
Not a native english user, sorry for any mistake I could have comited. Also First post… yay!